Showing posts with label WCF Examples. Show all posts
Showing posts with label WCF Examples. Show all posts

Simplest WCF Example


You need add three projects on your solution. 2 console application, 1 class library.
1 Class library for your Service where one interface and one class will be there,
1 Console App for hosting your service,
1 Console App for consuming your service.
Reference - System.ServiceModel is needed to be added(by add reference option),
use it over using namespace option.

Now have this 3 code example over your solution.

HelloWCFClassLibrary


namespace HelloWCFClassLibrary
{
    [ServiceContract]
    public interface IHelloWCFService
    {
        [OperationContract]
        string SayHello(string msg);
    }
}




namespace HelloWCFClassLibrary
{
    public class HelloWCFService:IHelloWCFService
    {
        public string SayHello(string msg)
        {
            return string.Format("Hello" + msg);
        }
    }
}



HelloWCFServiceApplication


namespace HelloWCFServiceApplication
{
    class HelloWCFServiceHostClass
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(HelloWCFService));
            host.AddServiceEndpoint(typeof(IHelloWCFService),new BasicHttpBinding(),new Uri("http://localhost:3127/"));
            host.Open();
            Console.WriteLine("Server started");
            Console.Read();           

        }
    }
}

HelloWCFClientApp



class Program
    {
        static void Main(string[] args)
        {
            BasicHttpBinding mybinding=new BasicHttpBinding();
            EndpointAddress myaddress= new EndpointAddress("http://localhost:3127/");

            /*Below two lines is require */
            mybinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            mybinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
               
            ChannelFactory<IHelloWCFService> myChannelFactory = new ChannelFactory<IHelloWCFService>(mybinding, myaddress);
            IHelloWCFService proxy = myChannelFactory.CreateChannel();

            ((IClientChannel)proxy).Open();

            Console.WriteLine(proxy.SayHello("Pradip "));

            Console.Read();


        }
    }


That's all required to have this simplest example.

What are the Basic Advantages of Data Contract Serializer over XML serializer ?

Both the DataContract serilizer and XML Serializer serialize object state to XML content to get interoperability done. But while comparing them, we must say that the Data Contract Serializer is much faster and high controllability than XML Serializer.

First question comes in your mind, why and how?

For example if you have 1000 data members and 1000 methods in your class and you want out of these only 47 data members and 690 methods needs to be serialized.

Now if you are using XML Serializer tag over your class, though you don’t want all thousand methods and all thousand data members should serialize and deserialize, it will serialize all 1000 members and 1000 methods. So unnecessarily we are spending time for serialization/de serialization for unwanted members.

If you are in Data Contract Serilizer, you can use specifically [DataMember] tag over required data members in your class. Like

    [DataContract]
    public class Sample
    {
       [DataMember]
       public int MyProperty { get; set; }
    }

So, only 47 data members will be serialized and deserialized, and you can save time for remaining 953 data members serialization.

Same specific tag like [OperationContract] , can be used on the required methods to save time.

Now if you talk about Control, it can be like using XML sterilizer, though some methods should not be explored/get Consumed to the client, you don’t have that control to restrict them. It can be done using DataContract Serilizer.