Tuesday 12 July 2011

wcf for beginners

The WCF Service

The WCF Interface you expose can not be particularly complex if you want to use the Service Moniker – I’ve found it best to keep top primitive types, and arrays of primitive types.  Data Contracts seem to be a no-go.
Start by creating a new WCF Service using File|New|Project and selecting WCF Service Library:
Double-click on IService1.cs in the Solution Explorer, and replace the default contents with this simple interface:
using System;
using System.ServiceModel;

namespace WcfService1
    public interface IService1
        string GetData(int value);

        object[] GetSomeObjects();
Next replace the contents of the service implementation, Service1.cs, with the following:
using System;

namespace WcfService1
    public class Service1 : IService1
        public string GetData(int value)
            return string.Format("You entered: {0}", value);

        public object[] GetSomeObjects()
            return new object[] { "String", 123, 44.55, DateTime.Now };

Configuring the WCF Service

By default the WCF Service is configured to use HTTP as the transport protocol.  I generally switch it to use TCP, because I am operating within a corporate intranet, and HTTP seems like overkill.
You’ll also find that by default your WCF Service exposes two endpoints.  The first exposes as you’d expect, the IService1 interface you defined above.  The second exposes Metadata about your service, which the Service Moniker uses to know what operations are available on your service.  You’ll need both.
Right-click on the App.config file in the Solution Explorer, and select Edit WCF Configuration:
Switch the first endpoint to use TCP:
Also change the second to use mexTcpBinding:
Change the base address that the service will use, to use a TCP address instead of a HTTP Address by selecting the Host node on the left hand tree, and then selecting the base address and clicking on Edit, and changing the text to be net.tcp://localhost:7891/Test/WcfService1/Service1/
Finally, because you are using TCP instead of HTTP, change the MetataData service to not expect to expose the metadata via HTTP, by changing HttpGetEnabled to False under Advanced|Service Behaviours…
Save the changes and exit the WCF Editor.  Your App.config should look like this:
<?xml version="1.0" encoding="utf-8" ?>
    <compilation debug="true" />
      <service behaviorConfiguration="WcfService1.Service1Behavior"
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
            <dns value="localhost" />
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
            <add baseAddress="net.tcp://localhost:7891/Test/WcfService1/Service1/" />
        <behavior name="WcfService1.Service1Behavior">
          <serviceMetadata httpGetEnabled="False"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
To start running your service you can hit Ctrl-F5 in Visual Studio.  This will start a test service host, and a test client.  We will ignore the client, but you can check that the service host has started by clicking on the message in the notification area:
Eventually you’ll want to host your service somewhere else, such as a Windows Service.

No comments:

Post a Comment