RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


The Baker's Dozen: A 13-Step Crash Course for Learning Windows Communication Foundation (WCF) : Page 4

WCF is meant to make things easier for developers, but as with any new technology, getting started can be difficult. Follow these thirteen steps to rev up your WCF knowledge quickly.

Step 7: Writing Code Instead of Config Files
Step 4 through Step 6 used configuration files to minimize the amount of code you'd need to write. Perhaps more importantly, you can distribute changes via configuration files without having to recompile and redistribute new software modules. However, there may be times when you'll want to access the WCF object model more directly.

Make sure to add a .NET reference to System.ServiceModel any time you write code using the WCF object model.
Listing 6 and Listing 7 open a ServiceHost object using configuration settings in app.config. If you wanted to write out the code "longhand" you'd do the following:

First, you'd determine the current host address:

   // C# code 
   IPHostEntry ips = Dns.GetHostEntry(
   IPAddress _ipAddress = ips.AddressList[0];
   string urlService = "net.tcp://" +
      _ipAddress.ToString() + 
   ' VB.NET 
   Dim ips As IPHostEntry = Dns.GetHostEntry(
   Dim _ipAddress As IPAddress = ips.AddressList(0)
   Dim urlService As String = "net.tcp://" + _
      _ipAddress.ToString() + ":8228/MyService"
Next, you'd create a new instance of a NetTcpBinding object (or WsHttpBinding, or whatever communication binding object you need), and set any required properties:

   // C# code 
   NetTcpBinding tcpBinding = 
   new NetTcpBinding();
   tcpBinding.Security.Transport.ProtectionLevel =
      System.Net.Security. ProtectionLevel.EncryptAndSign;
   tcpBinding.Security.Mode = SecurityMode.None;
   '  VB.NET code
   Dim tcpBinding As New NetTcpBinding()
   tcpBinding.Security.Transport.ProtectionLevel =
   tcpBinding.Security.Mode = SecurityMode.None;
Finally, create an instance of the ServiceHost object, and use the method AddServicePoint to define the type reference to the service contract (the interface), the binding object, and the address.

   // C# code 
   ServiceHost host = new ServiceHost(
      typeof(ICustomers), tcpBinding, 
   '  VB.NET code 
   Dim host As New ServiceHost( _
   host.AddServiceEndpoint( _
      GetType(ICustomers), _
      tcpBinding, urlService)
Figure 1. Adding a WCF Service: Select the WCF Service template from the new Web Site dialog.
Step 8: The Baker's Dozen Spotlight: Building and Hosting a WCF Service Inside IIS (Part 1 of 2)
If you have built ASMX-style Web services, you may be wondering how to accomplish the same thing in WCF: namely, to build a WCF service that uses the interface and business class, and host it in IIS. The steps are at least similar enough that you'll see some general commonality. In this step, you'll build the WCF Web service, and in the next step you'll build a client piece to access it.

First, create a new Web site project, using the WCF Service template (see Figure 1). Call the project WCFService_IISHosted.

Second, right click in Solution Explorer and add .NET references to the two DLLs for the interface and business class libraries that you created in Step 4 and Step 5 (DemoInterfaces.DLL and DemoCustomerBz.DLL). Once you do that, Solution Explorer will look like Figure 2.

Figure 2. Solution Explorer: Here's what the Solution Explorer in Visual Studio looks like for the IIS-hosted WCF service.
Note that Figure 2 contains a service file called Service.svc. This is similar to the ASMX file you would have used prior to WCF. The contents are as follows:

   <% @ServiceHost Language=C# Debug="true" 
      CodeBehind="~/App_Code/Service.cs" %>
You'll need to change two things in the service file: the service reference is the CustomerBz.DLL file, and there is no code-behind. (Many online demos feature a simple code-behind file that contains both an interface and the class code, but this example shows how to specify an external DLL).

   <% @ServiceHost Language=C# Debug="true" 
Your next step is to test out the service in a browser. If you run the Web project from within Visual Studio 2005 (or make the folder shareable and run it outside of Visual Studio 2005), you're likely to see a browser screen (see Figure 3) stating that "Metadata publishing is currently disabled." You can enable Metadata publishing by adding the following line to web.config, in the serviceBehavior area:

   <serviceMetadata  httpGetEnabled="true"/>
Figure 3. Metadata Publishing Disabled: If metadata publishing is disabled when you try to load the service in the browser, you'll see this message.
Figure 4. IIS Hosting: The figure shows an IIS-hosted WCF service after enabling metadata publishing.
Now if you try to run the service in the browser, you'll see it correctly (see Figure 4). Note that you cannot actually "run" the service and view test results in the browser, which was never a recommended way to test a service in the first place. The next step will create a simple .NET client to consume this IIS-hosted service.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date