Call a WCF Service

Setting up WCF Services and consuming them is a little more difficult than the ASP.NET Web Services. Essentially, there are five steps to take, namely:

  1. Define the service contract
  2. Implement (or write) the contract
  3. Configure the endpoint(s)
  4. Host the service in an application
  5. Reference and call the service from a client

We will go through these steps every time we create a WCF Service. In my example, I will follow the MSDN tutorial (which does the same process in six steps). I’ll create a console application as a host for the service, and will consume it from ASP.NET. Let’s begin! And please note, if something isn’t explained enough, feel free to change to the MSDN tutorial.

First, we should define the service contract. To do this, let’s create a console application. For practical reasons, change the namespace to whatever you want (I’ll use NovaPrompt.Service for the sake of my company).

The very first step is to add a reference on the System.ServiceModel assembly. It’s a .NET assembly, so look it at that tab. If it’s grayed out, you should change the .NET Framework version of your application to 3.5 or higher, as this assembly was introduced in 3.5. Then set the using directive appropriately. Now define an interface with an attribute of ServiceContract. Let’s do something mathematical: name the interface as ICalculator, and define four methods (all of them doubles) for the four main operations. Each of the methods must have an OperationContract attribute. Your code should look like something similar:

WCF1

That was easy. The second step to take is to implement our contract (interface). Let’s create a new class called CalculatorService, and implement our ICalculator interface. This task should be done in no time, the result is:

WCF2

Of course writing the results to the console isn’t necessary, just practical.

Now it is time to host our service. We will create a programmatic endpoint, too. Add a reference on System.ServiceModel.Description, then add the following line of code to your Main method:

ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), new Uri(@”http://localhost:8000/ServiceModel/myService”));

You can later refer to the service with this given URL. Now we need to add an endpoint to our service. The method to use is the fascinating AddServiceEndpoint, which takes an interface with the ServiceContract attribute, a binding and an address. Let’s enter the following line of code:

selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), “anEndpoint”);

Endpoints provide client access functionality to your services, so you must include them. It is a better approach to set them up in a config file, but now we just code them. Each endpoint provides information about its address, binding methods and the service contract it belongs to.

Note that the path on which we can access to this service is modified by this line. It become: http:\\localhost:8000\ServiceModel\myServie\anEndpoint.

The next thing to do is allow metadata exchange. To do this, you should create an instance of ServiceMetadataBehavior, set it’s HttpGetEnabled property to true, and add it to the Description.Behaviors property of your ServiceHost class. The code:

ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);

The last thing to do is the opening of the service, waiting for users, then closing it.

selfHost.Open();

Console.ReadLine();

selfHost.Close();

Now to test whether it’s working, run the program as an administrator and type the URL (http://localhost:8000/ServiceModel/myService/anEndPoint to your browser.

The last thing to do is consume the service from a client.

Let our service running, open Visual Studio on an existing ASP.NET Web site (or create one), and select Add Service Reference. Now type in the location of our service, and click Go. You should see the service, and proxy classes should be generated at the same time. Visual Studio also took care for defining the client-side endpoints of your service.

In the example above, we defined an endpoint programmatically. This can also be done via config files. You can set up bindings, endpoints and services in the system.servicemodel section of the given config file. The syntax looks like the following (refer MSDN article for more info):

<system.ServiceModel>

    <services>

        <service>

             <endpoint/>

         </service>

    </services>

    <bindings>

        <!– Specify one or more of the system-provided binding elements, for example, <basicHttpBinding> –> <!– Alternatively, <customBinding> elements. –>

        <binding> <!– For example, a <BasicHttpBinding> element. –>        </binding>

    </bindings>

    <behaviors> <!– One or more of the system-provided or custom behavior elements. –>

     <behavior> <!– For example, a <throttling> element. –> </behavior>   </behaviors>

</system.ServiceModel>

Further Readings:

Getting Started with WCF

Configuring Services in Configuration Files

WCF Endpoints

Advertisements

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: