Archive

Archive for September, 2010

Difference between WCF and Web service

September 30, 2010 Leave a comment

Web service is a part of WCF. WCF offers much more flexibility and portability to develop a service when comparing to web service.The main feature of WCF is it’s security.
WCF = Web services + .Net Remoting + MSMQ + (COM+)

Features Web Service WCF
Hosting It can be hosted in IIS It can be hosted in IIS,

windows activation service (WAS),

Self-hosting,

Managed Windows service

Programming [WebService] attribute has to be added to the class [ServiceContraact] attribute has to be added to the class
Model [WebMethod] attribute represents the method exposed to client [OperationContract] attribute represents the method exposed to client
Operation One-way, Request- Response are the different operations supported in web service One-Way, Request-Response, Duplex are different type of operations supported in WCF
XML System.Xml.serialization name space is used for serialization System.Runtime.Serialization namespace is used for serialization
Encoding XML 1.0, MTOM(Message Transmission Optimization Mechanism), DIME, Custom XML 1.0, MTOM, Binary, Custom
Transports It Can be accessed only over HTTP Can be accessed through HTTP, TCP, Named pipes, MSMQ,P2P, Custom
Protocols Security Security, Reliable messaging, Transactions

Delegates

September 16, 2010 Leave a comment

Delegates is also known as Type Safe pointer.

To understand type Safe Pointer, you need to understand call back function used in C++. Call back function is generally implemented in Business Tier in 3-tier architecture. Once business logic is implemented, it requires memory address of the function or method to use it. This memory address does not have any information of Method signature, so it can be called as it is not Type Safe.

But Delegates has the feature of callback in Safe way, as it take cares of signature information.

How to define delegate,

Public Delegate Sub MakeDelegate (ByVal EmployeeID As String)

We are declaring public delegate, so it can be accessed from anywhere in the application.

Here we are going to define a Employee class which uses the delegates and method.

Public Class Employee
    Public FirstName As String
    Public LastName As String
 
    Public Sub ValidEmployee (ByVal objDelegate As MakeDelegate, _
                                ByVal EmployeeID As String)
        If EmployeeID.StartsWith ("MKT") Then
            objDelegate.Invoke(EmployeeID)
        End If
    End Sub
End Class

The method ValidEmployee is going to accept the EmployeeID and a Delegate Object of type "MakeDelegate" as the parameters and validate whether it is a Starting with E1 and invoke the Delegate Object accordingly.

Dim objEmployee As Employee = New Employee()

        Dim objDelegate As MakeDelegate
        objDelegate = AddressOf NotifyEmployee
 
        objEmployee .FirstName = txtFirstName.Text
        objEmployee.LastName = txtLastName.Text
 

objEmployee.ValidateEmployee(objDelegate, txtEmployeeID.Text)

We assign the local procedure "NotifyEmployee", which is declared and defined inside the Windows Form Class to the Delegate Object.
Private Sub NotifyEmployee(ByVal EmployeeID As String)

MsgBox("This Employee is from Marketing Department") End Sub

Once we assign the instance of the Delegate, we must provide the address of a method implementation with a matching method signature.

Categories: .Net Framework 3.5 Tags:

Serialization in the .NET Framework

September 16, 2010 Leave a comment

Serialization in .NET allows the programmer to take an instance of an object and convert it into a format that is easily transmittable over the network, or even stored in a database or file system. This object will actually be an instance of a custom type, including any properties or fields you may have set.

The first step in any serialization process is to take the instance of the object and convert it to a memory stream. From there we have the ability to perform any number of operations with (file IO, database IO, etc.).

There are 2 types of serialization. Binary serialization and xml serialization

Core Serialization Methods

#region Binary Serializers

public static System.IO.MemoryStream SerializeBinary(object request) {

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer =

new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

System.IO.MemoryStream memStream = new System.IO.MemoryStream();

serializer.Serialize(memStream, request);

return memStream;

}

public static object DeSerializeBinary(System.IO.MemoryStream memStream) {

memStream.Position=0;

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer =

new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

object newobj = deserializer.Deserialize(memStream);

memStream.Close();

return newobj;

}

#endregion

#region XML Serializers
 
public static System.IO.MemoryStream SerializeSOAP(object request) {
  System.Runtime.Serialization.Formatters.Soap.SoapFormatter serializer = 
  new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
  System.IO.MemoryStream memStream = new System.IO.MemoryStream();
  serializer.Serialize(memStream, request);
  return memStream;
}
 
public static object DeSerializeSOAP(System.IO.MemoryStream memStream) {
  object sr;
  System.Runtime.Serialization.Formatters.Soap.SoapFormatter deserializer = 
  new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
  memStream.Position=0;
  sr = deserializer.Deserialize(memStream);
  memStream.Close();
  return sr;
}
#endregion