WCF Basics by snig717


More Info

Windows Communication Foundation takes many existing communication technologies, such as Web Services, Windows Remoting, Microsoft Message Queuing, and abstracts them into a single technology. In most cases, this simplifies the way you communicate with other applications. It also allows you to communicate with other applications without being coupled to a specific technology. Therefore, you could use Web Services over SOAP to begin with, and later move to remote procedure calls (RPC) without changing your code, just the configuration of WCF.

There are a few basic tasks when creating a WCF service. The basic tasks that must be performed are, in order: 1. Define the service contract. A service contract specifies the signature of a service, the data it exchanges, and other contractually required data. 2. Implement the contract. To implement a service contract, create the class that implements the contract and specify some custom behaviors that the runtime should have. 3. Configure the service by specifying endpoint information and other behavior information. 4. Host the service in an application. 5. Build a client application. For more information, see http://msdn2.microsoft.com/en-us/library/ms732098.aspx

To display a step-by-step enactment of the above steps, let’s define a web service that reports the current date/time: 1.1 DEFINE A SERVICE CONTRACT First, let’s create a project to house our service contract. Let’s call it WCFDateTime.Service.

1.2 DEFINE A SERVICE CONTRACT Then, let’s define an interface that represents the service we’re going to provide. In this case, let’s call it IDateTimeService. public interface IDateTimeService { }

1.3 DEFINE A SERVICE CONTRACT We need to add a method to our service that will return the current date/time: public interface IDateTimeService { DateTime GetCurrentDateTime(); }

1.4 DEFINE A SERVICE CONTRACT Now, we need to decorate our code with attributes so that WCF will recognize our interface and its methods. Namely, we will add the following attributes:   ServiceContractAttribute o Identifies an interface as a WCF service contract Identifies methods of an interface as WCF service operations, that is, methods that can be called through WCF using System.ServiceModel; [ServiceContract] public interface IDateTimeService { [OperationContract] DateTime GetCurrentDateTime(); } In order to use the above attributes, you’ll need to add a reference to the System.ServiceModel assembly. The service contract is now ready to be used.

OperationContractAttribute o

2.1 IMPLEMENT THE CONTRACT Now that we’ve defined our service contract, we can move on to implementing it. First, let’s create another project to house our server application. Let’s call it WCFDateTime.Server, and make it a Console Application project.

2.2 IMPLEMENT THE CONTRACT Now, let’s implement the IDateTimeService. The implementation is going to run on our server, to provide the date/time to whoever is going to consume our service. In the WCFDateTime.Server project, let’s create a class called DateTimeService and have it implement IDateTimeService: using WCFDateTime.Service; public class DateTimeService : IDateTimeService { } Remember to add a reference to the WCFDateTime.Service project so you can use IDateTimeService.

2.3 IMPLEMENT THE CONTRACT The above code will obviously not compile, because we did not implement the GetCurrentDateTime method: public class DateTimeService : IDateTimeService { public DateTime GetCurrentDateTime() { return DateTime.Now; } } That’s it! The DateTimeService is now ready to be used.

3.1 CONFIGURE THE SERVICE Now we need to configure our service so it can be consumed by client applications. The simplest way to accomplish this is by adding an application configuration file (app.config or web.config) to the WCFDateTime.Server project: <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="WCFDateTime.Server.DateTimeService"> <endpoint address="http://localhost:8081/DateTimeService" binding="wsHttpBinding" contract="WCFDateTime.Service.IDateTimeService" /> </service> </services> </system.serviceModel> </configuration>

When defining an endpoint for WCF, remember your A-B-Cs: 1. Address (where) 2. Binding (how) 3. Contract (what) The address specifies the address where you want the service to be located. The binding specifies the transport you want to use in order to provide the service. The contract specifies what service will be provided 4.1 HOST THE SERVICE IN AN APPLICATION Now that WCF has been configured, the simplest way to host this service is to create a Console Application and use the ServiceHost class. First, add a reference to the System.ServiceModel assembly. Then, In the program.cs file of WCFDateTime.Server, do the following:

using System.ServiceModel; namespace WCFDateTime.Server { class Program { static public void Main(string[] args) { // Get a host for our service ServiceHost serviceHost = new ServiceHost( typeof(DateTimeService) ); // Open the service host to start listening for incoming requests serviceHost.Open(); // The service can now be accessed Console.WriteLine("The service is ready."); Console.WriteLine("Press <ENTER> to terminate service."); Console.ReadLine(); // Close the service host serviceHost.Close(); } } } By simply running this application, we expose our service so that client applications can use it.

5.1 BUILD A CLIENT APPLICATION To build a client application, we must create a new project for it. Let’s call it WCFDateTime.Client, and make it a Console Application as well.

5.2 BUILD A CLIENT APPLICATION Now, we need to add an Application Configuration file, and give it some settings that will allow us to connect to the service we just created: <?xml version="1.0" encoding="utf-8"?> <configuration> <system.serviceModel> <client> <endpoint address="http://localhost:8081/DateTimeService" binding="wsHttpBinding" contract="WCFDateTime.Service.IDateTimeService" name="MyDateTimeService"> </endpoint> </client> </system.serviceModel> </configuration> Now that our we’ve configured our WCF client, we can connect to it. There are two ways to connect to the WCF service we previously created: 1. Client proxy generation 2. Channel factories Most Microsoft articles will point you in the direction of using client proxies; however, for our purposes, channel factories are quicker to implement and more robust in a Model/View/Presenter architecture.

5.3 BUILD A CLIENT APPLICATION Now, add a reference to the System.ServiceModel and WCFDateTime.Service assemblies, and then add the following to the program.cs file: using System.ServiceModel; using WCFDateTime.Service; class Program { public static void Main(string[] args) { // Get a channel factory for our service, // using the configuration for "MyDateTimeService" // in the application configuration file. ChannelFactory<IDateTimeService> channelFactory = new ChannelFactory<IDateTimeService>("MyDateTimeService"); // Get an instance of our service IDateTimeService service = channelFactory.CreateChannel(); // Get the server’s date/time DateTime dt = service.GetCurrentDateTime(); // Write the current server date/time Console.WriteLine("The current server time is " + dt); // Close the connection to our service channelFactory.Close(); } }

5.4 BUILD A CLIENT APPLICATION Your client application is now ready to run! Simply start the WCFDateTime.Server application, and once it’s running, run the WCFDateTime.Client application to see it work!

To top