An Introduction to WCF (Part 1)

Posted by: Granville Barnett, on 21 Jan 2007 | View original

Before we start this post series will be based on:

  • IIS7
  • .NET 2.0
  • .NET 3.0 (where WCF is!)

To kick things off what we will do is create an ASP.NET web service to expose some dead simple methods then replicate the same example using WCF and talk through the differences and its advantages.  Both implementations will be consumed by a good old console application :-)

It should be no surprise that the method we will define for our first WCF service will return a nice custom greeting followed by the name entered as a parameter to that method - come on folks, did you really think we were going to create something useful in the first post?!

Each WCF service has an address, binding and contract (ABC - we will cover this in Part 2).

We'll just dive in and code something up and then talk about it afterwards rather than hang around.

IFirstService.cs (Service Contract):

   1:  using System;
   2:  using System.ServiceModel;
   3:   
   4:  /// <summary>
   5:  /// Summary description for IFirstService
   6:  /// </summary>
   7:  [ServiceContract(Namespace="http://gbarnett.org/services")]
   8:  public interface IFirstService {
   9:      [OperationContract]
  10:      string GetWelcomeMessage(string name);
  11:  }

FirstService.cs

   1:  using System;
   2:   
   3:  public class FirstService : IFirstService {
   4:      public string GetWelcomeMessage(string name) {
   5:          return string.Format("Hello {0}!!", name);
   6:      }
   7:  }

Web.config

   1:  <?xml version="1.0" encoding="UTF-8"?>
   2:   
   3:  <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
   4:    <system.serviceModel>
   5:      <services>
   6:        <service name="FirstService" behaviorConfiguration="returnFaults">
   7:          <endpoint contract="IFirstService" binding="wsHttpBinding" />
   8:        </service>
   9:      </services>
  10:      <behaviors>
  11:        <serviceBehaviors>
  12:          <behavior name="returnFaults">
  13:            <serviceDebug includeExceptionDetailInFaults="true" />
  14:            <serviceMetadata httpGetEnabled="true" />
  15:          </behavior>
  16:        </serviceBehaviors>
  17:      </behaviors>
  18:    </system.serviceModel>
  19:   
  20:    <system.web>
  21:      <compilation debug="true" />
  22:    </system.web>
  23:    
  24:    <system.webServer>
  25:        <directoryBrowse enabled="true" />
  26:    </system.webServer>
  27:   
  28:  </configuration>

Lets have a look at what we have done.  First we define a contract for our service which uses attributes to indicate it is a service contract, in this contract we specify a single method and we also attribute that with an attribute (OperationContract).

The FirstService.cs simply implements the contract.

The web.config defines the behavior of our service, specifying the services' name, endpoint and contract - we will cover all these in detail in Part 2 - this first part is just to get you up and running quick!

If you browse to your service you will see the following screen:

In order to use the service from a client application we need to create a proxy class, we can do that using the svcutil command line utility tool and point it at our services' wsdl.

Now we have our proxy class generated lets write some code in our client that calls the GetWelcomeMessage method of our service.

Note:  You will need to add the generated proxy class and config settings to your existing project.

   1:  using System;
   2:   
   3:  namespace Org.GBarnett.WCF.SimpleClient {
   4:   
   5:      public class Program {
   6:   
   7:          public static void Main(string[] args) {
   8:              FirstServiceClient proxy = new FirstServiceClient();
   9:              Console.WriteLine(proxy.GetWelcomeMessage("Granville"));
  10:          }
  11:   
  12:      }
  13:  }

If you don't understand things in this post then don't worry it was designed to get you up and running quickly - part 2 will cover WCF in detail.

Until then :-)

Share this post: Email it! | bookmark it! | digg it! | reddit!

Advertisement

Similar Posts

  • [Podcasts] Craig McMurtry Discusses the Windows Communication Foundation (WCF) on Developer Night in Canada (DNIC) more
  • Splitting POP3 Email Attachments in BizTalk 2006 more
  • Web Services and type mapping - again more
  • Service Modeling Language based on XML Schema and Schematron more
  • WCF Workshop Part 6 (Securing your Service Part 2 Message Encryption) more
  • How To Properly Group Partial Class Files in Visual Studio 2005 more
  • WCF Workshop Part 5 (Securing your Service Part 1) more
  • What Exactly Is BizTalk? more
  • Programming is too easy more
  • SOA & Business Process Conference (October 3-6, 2006) more

News Categories

.NET | ADO.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Community Server | dasBlog | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD