Call a WCF Service
Setting up WCF Services and consuming them is a little more difficult than the ASP.NET Web Services. Essentially, there are five steps to take, namely:
- Define the service contract
- Implement (or write) the contract
- Configure the endpoint(s)
- Host the service in an application
- Reference and call the service from a client
We will go through these steps every time we create a WCF Service. In my example, I will follow the MSDN tutorial (which does the same process in six steps). I’ll create a console application as a host for the service, and will consume it from ASP.NET. Let’s begin! And please note, if something isn’t explained enough, feel free to change to the MSDN tutorial.
First, we should define the service contract. To do this, let’s create a console application. For practical reasons, change the namespace to whatever you want (I’ll use NovaPrompt.Service for the sake of my company).
The very first step is to add a reference on the System.ServiceModel assembly. It’s a .NET assembly, so look it at that tab. If it’s grayed out, you should change the .NET Framework version of your application to 3.5 or higher, as this assembly was introduced in 3.5. Then set the using directive appropriately. Now define an interface with an attribute of ServiceContract. Let’s do something mathematical: name the interface as ICalculator, and define four methods (all of them doubles) for the four main operations. Each of the methods must have an OperationContract attribute. Your code should look like something similar:
That was easy. The second step to take is to implement our contract (interface). Let’s create a new class called CalculatorService, and implement our ICalculator interface. This task should be done in no time, the result is:
Of course writing the results to the console isn’t necessary, just practical.
Now it is time to host our service. We will create a programmatic endpoint, too. Add a reference on System.ServiceModel.Description, then add the following line of code to your Main method:
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), new Uri(@”http://localhost:8000/ServiceModel/myService”));
You can later refer to the service with this given URL. Now we need to add an endpoint to our service. The method to use is the fascinating AddServiceEndpoint, which takes an interface with the ServiceContract attribute, a binding and an address. Let’s enter the following line of code:
selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), “anEndpoint”);
Endpoints provide client access functionality to your services, so you must include them. It is a better approach to set them up in a config file, but now we just code them. Each endpoint provides information about its address, binding methods and the service contract it belongs to.
Note that the path on which we can access to this service is modified by this line. It become: http:\\localhost:8000\ServiceModel\myServie\anEndpoint.
The next thing to do is allow metadata exchange. To do this, you should create an instance of ServiceMetadataBehavior, set it’s HttpGetEnabled property to true, and add it to the Description.Behaviors property of your ServiceHost class. The code:
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
The last thing to do is the opening of the service, waiting for users, then closing it.
Now to test whether it’s working, run the program as an administrator and type the URL (http://localhost:8000/ServiceModel/myService/anEndPoint to your browser.
The last thing to do is consume the service from a client.
Let our service running, open Visual Studio on an existing ASP.NET Web site (or create one), and select Add Service Reference. Now type in the location of our service, and click Go. You should see the service, and proxy classes should be generated at the same time. Visual Studio also took care for defining the client-side endpoints of your service.
In the example above, we defined an endpoint programmatically. This can also be done via config files. You can set up bindings, endpoints and services in the system.servicemodel section of the given config file. The syntax looks like the following (refer MSDN article for more info):
<!– Specify one or more of the system-provided binding elements, for example, <basicHttpBinding> –> <!– Alternatively, <customBinding> elements. –>
<binding> <!– For example, a <BasicHttpBinding> element. –> </binding>
<behaviors> <!– One or more of the system-provided or custom behavior elements. –>
<behavior> <!– For example, a <throttling> element. –> </behavior> </behaviors>