Monday, April 23, 2012

Programming WCF - Creating a custom WCF Service and a Host

My last post was about Introduction to Windows Communication Foundation. Today I thought about writing something about how to do some nice programming using WCF and I will show you how to create a custom WCF Service and a Service Host using C#. I assume that you all have some knowledge of WCF programming, so I will be not explaining every single line of code here.

First I will start off by creating class library project and I will name it "WCFClassLibrary". In that project, I will create a class named "MyWcfService" and I will also create an Interface which is "IMyWcfService". Now in my "WCFClassLibrary", I will add a reference to the "System.ServiceModel.dll" and I am now going to modify my "IMyWcfService" interface.

I will be using "System.ServiceModel.dll" in my interface and since interfaces that represents WCF contracts needs to have [ServiceContract] attribute, I am modifying the interface as follows.
using System.ServiceModel;
namespace WCFClassLibrary
{
    [ServiceContract]
    interface IMyWcfService
    {
        [OperationContract]
        string MyTestMethod(string sHelloWorld);
    }
}
Then in my "MyWcfService" class, I am going to implement the IMyWcfService interface.
namespace WCFClassLibrary
{
    public class MyWcfService : IMyWcfService
    {
        public string MyTestMethod(string sHelloWorld)
        {
            return sHelloWorld;
        }
    }
}
Now I am done with implementing the service. Next what I have to do is implement the host application to host my service. For that I will create a console application. For that I will add a new Console Application project and I will name it "MyWcfHostConsoleApplication". I will add a reference to the "System.ServiceModel.dll" and "WCFClassLibrary.dll" project. Then I will add a Application configuration file and it's name is "App.config". So it's time modify the "App.config" file. Within,
<configuration></configuration>
I will start writing following code.
<system.serviceModel>
    <services>
        <service name="WCFClassLibrary.MyWcfService">
            <endpoint address="" binding="basicHttpBinding" contract="WCFClassLibrary.IMyWcfService">
            </endpoint>

            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8080/MyWcfService"/>
                </baseAddresses>
            </host>
        </service>
    </services>
</system.serviceModel>
Now, I have to write code for hosting my service from inside my Main method. For that I will modify my Main method as follows.
using System;
using System.ServiceModel;
using WCFClassLibrary;

namespace MyWcfHostConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost serviceHost = new ServiceHost(typeof(MyWcfService)))
            {
                serviceHost.Open();
                Console.ReadLine();
            }
        }
    }
}
Now when I type "http://localhost:8080/MyWcfService" and hit enter in by browser, what I will see is this,

Metadata publising for this service is currently disabled.

You will see that "Metadata  publishing for this service us currently disabled.". To enable Metadata  publishing what you will have to do is again modify the "App.config " as follows.
<system.serviceModel>
    <services>
        <service name="WCFClassLibrary.MyWcfService" behaviorConfiguration="MyWcfServiceMEXBehavior">
            <endpoint address="" binding="basicHttpBinding" contract="WCFClassLibrary.IMyWcfService">
            </endpoint>

            <!--MEX endpoint-->
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
            </endpoint>

            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8080/MyWcfService"/>
                </baseAddresses>
            </host>
        </service>
    </services>

    <!--A behavior definition for MEX-->
    <behaviors>
        <serviceBehaviors>
            <behavior name="MyWcfServiceMEXBehavior">
                <serviceMetadata httpGetEnabled="True"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>
Now again when I host and I type "http://localhost:8080/MyWcfService" and hit enter in by browser, I can see this.

MyWcfService
That's all. Now I can add a Service Reference to this service from my client applications and I can consume this service.

Appreciate your feedback.

Happy Coding.

Regards,
Jaliya

1 comment:

  1. Its very nice to get information on programming in net .There are also some relible sites where you can get more information.Custom Programming Services are healthy and fine urbanized facilities which lend a hand in the midst of Programming Services. They make available filled client approval and pledge and declaration for Software Development Services. The pieces of writing make available brightness on subject like Custom Software Development Services as fighting fit.

    ReplyDelete