Wednesday, November 26, 2008

Extract ISO File

ISO File
Microsoft Visual Studio & their Service Packs releases can be installed either by their Online Installer or Offline Installer. For Offline Installer, the release will be mainly in *.iso extension & it is used to be burn in CD/DVD.
Instead of burning in DVD, we can view the iso contents using MagicDisc a freeware application.

Steps to do
1. Magic Disc
Download & Install MagicDisc Software

2. After Install,in MagicDisc systray menu, Click "Virtual CD/DVD-ROM" > Select a Drive Letter > Mount...

3. Select your ISO file & it will be mounted as a disc(virtual)

4. From this new drive, you can install/browse the iso contents. 
(And also useful for Gaming Applications too, without CD/DVD)

Thank you MagicISO, for your good software.

Happy Installation!



Sunday, November 16, 2008

Silverlight - Policy Server in VB.NET

Silver Light Policy Server
For Socket based communications in Silverlight, we need to run Policy Server at Socket Listening host. Policy Server will send the Policy XML file to silverlight client similar to flash crossdomain.xml file, which Silverlight also uses for its HTTP request. Without Policy Server, Silverlight sockets cannot connect to a Server Application.

Why we need a separate Policy file for sockets ?
The Crossdomain.xml file can be directly accessed by Clients using HTTP request from related web server, where as for sockets based communication server,  web server is not in the picture. So for security purposes, similar to crossdomain file, there needs a policy file to be downloaded & which will be issued by Policy Server.

Ports
Policy Server Listening Port: 943
Silverlight Sockets Allowed Ports: 4502 to 4534

How it will work ?
1. Our Policy Server listens in Port: 943. 
2. Silverlight client will connect to our policy server & send a request "<policy-file-request/>"
3. Once this request is received, Policy Server should send the Policy XML file in that connection & close that connection request.

Sample Policy XML File
clientaccesspolicy.xml

<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from>
                <domain uri="*" />
           </allow-from>
            <grant-to>
                <socket-resource port="4502-4534" protocol="tcp"/>
            </grant-to>
</policy>
   </cross-domain-access>
</access-policy>


domain uri - Allows the domain from where it can be called. "*" will allow all domains.
To allow only a particular domain Ex: uri="http://srimax.com"/>

Socket-Resource port can be used to indicate the allowed ports.
To allow only a particular port: port="4512" ...

Policy Server
PolicyServerService.vb


Imports System
Imports System.IO
Imports System.Net.Sockets
Imports System.Text

Public Class PolicyServerService

    Public sSocket As Socket
    Private PolicyFile As String = String.Empty
    Private Const POLICYREQUEST As String = "<policy-file-request/>"
    Private Const POLICYPORT As Integer = 943
    Private Const POLICYFILENAME As String = "clientaccesspolicy.xml" 

    Public Sub New()
        ' Read Policy File details
        PolicyFile = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) & "\" & POLICYFILENAME

        ' Establish the local endpoint for the socket.
        Dim ipLocal As New System.Net.IPEndPoint(System.Net.IPAddress.Any, POLICYPORT)
        sSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        sSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 0)
sSocket.Bind(ipLocal)

        sSocket.Listen(10)
        'Start an asynchronous socket to listen for connections.
        sSocket.BeginAccept(New AsyncCallback(AddressOf onConnect), Nothing)

    End Sub

 

    Private Sub onConnect(ByVal asyn As IAsyncResult)
        Dim objPolicyConnection As New PolicyConnection
        Try
            With objPolicyConnection
                .cSocket = sSocket.EndAccept(asyn)
                .cSocket.BeginReceive(.buffer, 0, .buffer.Length, SocketFlags.None, New AsyncCallback(AddressOf onDataArrival), objPolicyConnection)
            End With 

            'continue listen for connections.
            sSocket.BeginAccept(New AsyncCallback(AddressOf onConnect), Nothing)

        Catch exNull As System.NullReferenceException
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub 

    Private Sub onDataArrival(ByVal asyn As IAsyncResult)
        Dim strData As String = String.Empty
        Dim policyFile As String = String.Empty
        Dim ibRead As Integer = 0
        Dim objPolicyConnection As PolicyConnection = CType(asyn.AsyncState, PolicyConnection)
        Try
            With objPolicyConnection
                If .cSocket.Connected Then ibRead = .cSocket.EndReceive(asyn)
                If ibRead > 0 Then
receivedText.Append(Encoding.ASCII.GetString(.buffer, 0, ibRead))
                strData = .receivedText.ToString()
                If strData.Length < POLICYREQUEST.Length Then
                    ' if not full request received
                    .cSocket.BeginReceive(.buffer, 0, .buffer.Length, SocketFlags.None, New AsyncCallback(AddressOf onDataArrival), objPolicyConnection)

                    Return
                End If
                If StringComparer.InvariantCultureIgnoreCase.Compare(strData, POLICYREQUEST) <> 0 Then
                    ' if not matching with Policy Request, then exit
                    .cSocket.Close()
                    Return
                End If

                'Sending Policy File
                .cSocket.BeginSendFile(policyFile, New AsyncCallback(AddressOf onSend), objPolicyConnection)
            End With

        Catch ex As Exception
            objPolicyConnection.cSocket.Close()
        End Try

    End Sub 

    Private Sub onSend(ByVal asyn As IAsyncResult)
        Dim objPolicyConnection As PolicyConnection = CType(asyn.AsyncState, PolicyConnection)
        ' Finish File Send & Close Connection since no further communication needed
        Try
            objPolicyConnection.cSocket.EndSendFile(asyn)
        Catch ex As Exception
        Finally
            objPolicyConnection.cSocket.Close()
        End Try
    End Sub
End Class

Public Class PolicyConnection
    Public cSocket As Socket
    Private Const POLICYREQUEST As String ="<policy-file-request/>"
    Public buffer(POLICYREQUEST.Length) As Byte
    Public receivedText As New StringBuilder
End Class

This Policy Server Service class can be called from our server application or can be a standalone PolicyServer application by just creating object.
Private objPolicyServer As PolicyServerService
Private Sub StartPolicyServer()
 objPolicyServer = New PolicyServerService()
End Sub


Download

The source code for the above can be downloaded from here

Conclusion
Thus we can have a stand-alone policy server or integerated policy server created in VB.NET.

Happy Silverlight Sockets Programming!

Thursday, November 13, 2008

ASP.NET - Using OOPs in real life

How to use OOPs
OOPs concepts are effectively using by high level coders...But college students & ordinary developers (like me) well know the OOPs concepts (inheritance, polymorphism, etc etc) , but the problem is where to apply these learned concepts in real life ?

OOPs in ASP.NET
Let me share you the ideas of where to use OOPs in ASP.NET, if you have enough knowlege & not sure where & how to use.

Hope all ASP.NET e-commerce developers would have worked with Payment Processors. (Ex. Paypal / Authorize.net /Google Checkout etc). And also you will have experiences such as your customer may frequently change his payment processors depending on their commission/sales charges. Also you may need to re-use this same payment code for other projects as well & need some touch-up works to work with others.

If you are doing the development works as above, lets follow how OOPs can save your time by creating our own oop components.


Planning your Requirements for all Projects
Before creating a component, plan the properties you have needed. In our example, if we are going to create a Payment Processor Component & some common properties we might needed are
1. MerchantId
2. First Name
3. Last Name
4. Contact Address..
5. Billing Name
6. Billing Address etc
7. Credit Card Number
8. Exp.Date
etc...etc.

Creating OOPs components
Base Class:
Create a Base Class, say named "Mustinherit Class PaymentProcessor" & have all the properties, you think that are essential/common inputs from Customer to charge.

Add MustOverride functions which you think common for all. Ex. ProcessPayment( )

Add Events like Success(transactionId as string) , Failed() etc

Derived Class:
Create a extension class, say "PaypalProcessor" inherits "PaymentProcessor"
Override ProcessPayment function & add the code you needed for Paypal workings. The function use the properties defined in base PaymentProcessor class. Depending on the transaction status, raise the related events.

Similarly you can develop any number of extension classes which inherits base class for all your Payment Processors.

GetInstanceClass:
In your PaymentProcessor component, add a class to provide the right instance required by application layer.
Shared Function GetProcessorInstance(ProcessorName as String) as PaymentProcessor
Select Case ProcessorName:
Case "Paypal"
return New PaypalProcessor()
Case "Authorize.Net"
return New AuthorizeProcessor()
End Function


Application Layer:
Later in Application Layer, depending on the cart customer payment mode selection, get the needed processor instance.

Ex:
Dim pProcessor as PaypalProcessor
pProcessor=GetInstance.GetProcessorInstance("Paypal")
pProcessor.FirstName=txtCustomerFirstName.text
pProcessor.LastName=txtCustomerLastName.text
....
...
....
pProcessor. ProcessPayment()

So now our application layer code is not bind or designed for any particular payment system. We can add/change the payment processors easily & can also reuse the PaymentProcessor code in all other projects too with out any change.

Conclusion
This is just to show a small light on OOPs usage way. You can apply your all known OOPs skills for areas where you regularly copy/paste from other projects + need of dynamic module include without changing Application Code.

Happy OOPs Coding!!!