Dot Net For All

Developing Self Hosted WCF service

In this article I will discuss about the WCF service using C#. Later we will be creating self hosted WCF service from scratch. In this article I will not use the templates provided by the Visual Studio to create WCF service. Instead of that I will create the application from scratch to get the better understanding. You can visit my previous article about WCF contracts here.

WCF Service project Structure

The WCF project structure for my article is as shown below. I will discuss various parts one by one. I have created this structure for my project to keep all the components of the project separate from each other.

 

All of the assemblies in this project are ClassLibrary project apart from Client and ServiceHostApp which are console projects.

WCF expert Miguel Castro writes in one of his article here (under The Critque section) the benefits of going with this approach of creating these parts manually.

If the developer and consumer of the WCF service is same organization it is always better to go this way. We can save ourselves from pain of understanding the proxies generated if we consume the service.

Creating Service Contract Project

I start by creating the Contract project which I have named as MyService.Contract. This will contain both my service contract and data contract. I need to add the System.ServiceModel and System.Runtime.Serialization references to it.

The code for the Service contract and data contract is as shown below. It is generally good to end the DataContract with “Data” suffix as PersonData.

    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        string Hello(PersonData name);
    }

    [DataContract]
    public class PersonData
    {
        [DataMember]
        public string FirstName { get; set; }
    }

Service Project

In this project I have a class named MyServiceImplementation.cs which implements IMyService interface as shown in the code below.This project will have a reference of the service contract’s assembly.

    public class MyServiceImplementation : IMyService
    {       
        public string Hello(PersonData person)
        {
            return "Hello " + person.FirstName;
        }
    }

Hosting Application

ServiceHostApp is the hosting project in my solution. It is a console application. If references both the service contract and service projects. It also need to have System.ServiceModel and System.Runtime.Serialization assemblies. The code for service host is as following.

        static void Main(string[] args)
        {
            ServiceHost serviceHost = new ServiceHost(typeof(MyServiceImplementation));
            serviceHost.Open();
            Console.Read();

            serviceHost.Close();
        }

This project also need to have a app.config file where we should expose the service hosted by this application. To expose the service to the client we need to expose the address. binding and contract. These are generally called the ABC of the WCF service. Once the host is opened the service or the host is listening and the host instantiates the service class.

In the above figure we can see that in serviceModel section we have defined an endpoint. This endpoint is per service. Each service can have many endpoints Endpoint contains the ABC of the service.

The name is the fully qualified name of the Service class. Contract is the fully qualified name of the contract which service implements. I have used the basicHttpBinding. The address of the service is http://localhost:8080/MyService URL. This is the URL where service will be hosted.

The host will keep listening unless we do not press any key as seen in the above code.

Service Proxy Project

In my previous article I have discussed about WCF proxies. Here I have developed a class library project named MyService.Proxies which references the Contract project. Lets have a look at the service proxy code. I will discuss about proxies in details in my next article.

    public class MyServiceProxy : ClientBase<IMyService>, IMyService
    {
        public string Hello(PersonData person)
        {
            return Channel.Hello(person);
        }
    }

Client

To access the service expose by console host, we have to add the address binding and configuration in the Client App.config as shown in the code below. Add the serviceModel tag under configuration.

  <system.serviceModel>
    <client>
      <endpoint address="http://localhost:8080/MyService"
                binding="basicHttpBinding" 
      contract="MyService.Contract.IMyService"/>
    </client>
  </system.serviceModel>

Add the reference for the Proxies project and the Contract project which we have created earlier to client. The contract reference is added to have access to the DataContract. Access the service using proxy as shown in the C# code below.

            PersonData person = new PersonData() { FirstName = "Vikram" };
            MyServiceProxy proxy = new MyServiceProxy();
            Console.WriteLine(proxy.Hello(person));
            Console.Read();

Now we will run the whole solution by setting Multiple Startup project as shown below figure.

After executing the project the output is “Hello Vikram” which is expected from service.

Conclusion:

I have created an WCF service without using the visual studio template. To work with WCF service is very easy of we are clear with basics. Please find the code for this article here which Is developed in VS 2015 . Open the VS with administrator privileges.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview