Thursday, May 15, 2014

Windows Communication Foundation

I like to think of Windows Communication Foundation (WCF) as the portion of the .NET Framework that concerns the hosting and consumption of services.  A "service" can be thought of as a server program that exposes a set of operations that is accessible to client programs over a network.  A service that provides its operations over the internet can be thought of as a "web service".

A WCF service consists of:
  1. An interface that defines both the service and its operations.
  2. A class that provides the implementation of the operations.
To create a WCF service in Visual Studio:
  1. Add an ASP.NET Web Application project (Use the "Empty" template) or a WCF Service Application project.
  2. Then add a WCF Service item.
The WCF service must be hosted.  The obvious choice is to host it in IIS.  However, the service can be hosted in any .NET Windows application as well.

How does the host know to listen for requests?  Setting in the web.config file.  The following configuration indicates to the .NET framework that a WCF service is running:

  <system.serviceModel>
    <services>
      <service name="ProjectName.DayOfTheWeekService">
        <endpoint address="http://localhost:8000/DayOfTheWeekService.svc" binding="basicHttpBinding"
                  name="basicHttpBinding_IDayOfTheWeekService"
                  contract="ProjectName.IDayOfTheWeekService" />
      </service>
    </services>
  </system.serviceModel>

If you wanted to host this in IIS, just publish the files to a site.  If you wanted to host it in a Windows Form app, do something like this:

    Private MyServiceHost As System.ServiceModel.ServiceHost

    Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
            MyServiceHost = New System.ServiceModel.ServiceHost(GetType(ProjectName.DayOfTheWeekService))
            MyServiceHost.Open()
    End Sub

Consuming a web service is pretty straightforward.  Just add a Service Reference in Visual Studio and a proxy class will be generated for you to call the service operations with:

        Dim DayOfWeekProxy As New DayOfTheWeekServiceReference.DayOfTheWeekServiceClient("BasicHttpBinding_IDayOfTheWeekService")

        Console.WriteLine(DayOfWeekProxy.GetDayOfTheWeek(InputDate))


The client configuration is very similar to the server configuration. Essentially to connect to a service, the client must use the same configuration.

    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IDayOfTheWeekService" />
            </basicHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://SERVERCOMPUTER:8000/DayOfTheWeekService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDayOfTheWeekService"
                contract="DayOfTheWeekServiceReference.IDayOfTheWeekService"
                name="BasicHttpBinding_IDayOfTheWeekService" />
        </client>
    </system.serviceModel>

No comments:

Post a Comment