In this section, we will see the steps involved in creating a simple WCF service. Since the focus of this section is to get familiar with the service development steps, We will keep the service implementation simple. The service is a simple MathService that implements a service contract named IMathService. This exercise has six development steps:
1. Create the service
2. Choose the service hosting mechanism
3. Create the service configuration file
4. Create the client
5. Create the proxy and configuration file for the client
6. Implement and run the client
1. Create the service
2. Choose the service hosting mechanism
3. Create the service configuration file
4. Create the client
5. Create the proxy and configuration file for the client
6. Implement and run the client
We will see this steps one by one
1. Create the Service
To start with, create a new project named WCFBasics using Visual Studio 2005 as shown in the below screenshot.
To start with, create a new project named WCFBasics using Visual Studio 2005 as shown in the below screenshot.
Once the project is created, add a new WCF Service using the Website->Add New Item menu option and name the service MathService.
After the service is created, you should see a MathService.svc file in your project with the below line of code.
using System;
using System.ServiceModel;
[ServiceContract]
public interface IMathService
{
[OperationContract]
int Add(int x, int y);
[OperationContract]
int Subtract(int x, int y);
[OperationContract]
int Divide(int x, int y);
}
public class MathService : IMathService
{
public int Add(int x, int y)
{ return x + y; }
public int Subtract(int x, int y)
{ return x - y; }
public int Divide(int x, int y)
{ return x / y; }
}
2. Choose the Service Hosting Mechanism
As mentioned before, WCF is flexible because its services can be hosted in different types of applications. For the purposes of this installment, let us use IIS for hosting the WCF services because of its simplified configuration.
As mentioned before, WCF is flexible because its services can be hosted in different types of applications. For the purposes of this installment, let us use IIS for hosting the WCF services because of its simplified configuration.
3. Create the Service Configuration File
Configuration-based development permits part of an application's behavior to be specified in configuration files. You can make such things as addresses, bindings, security details, service behavior, and tracing changeable without having to modify and recompile the program code. By specifying addresses, bindings, and behaviors in a configuration file, you can make deploy-time decisions about services that don't require any modification to the service programs themselves.As mentioned before, by using IIS you can take advantage of IIS features such as service activation, process recycling and so on. To host the MathService in IIS, modify the Web.config in the Web service project to look as follows:
Configuration-based development permits part of an application's behavior to be specified in configuration files. You can make such things as addresses, bindings, security details, service behavior, and tracing changeable without having to modify and recompile the program code. By specifying addresses, bindings, and behaviors in a configuration file, you can make deploy-time decisions about services that don't require any modification to the service programs themselves.As mentioned before, by using IIS you can take advantage of IIS features such as service activation, process recycling and so on. To host the MathService in IIS, modify the Web.config in the Web service project to look as follows:
Through its attributes, the element under \ specifies the name of the service as well as the name of the behavior configuration that will be used to control the behavior of the service. The element also contains another child element named that enables you to specify the address, contract, and binding for the service. In this case, since IIS is the hosting mechanism, the address attribute is set to "". For an IIS-hosted application, the endpoint address should be defined by the physical *.svc file and the name of its hosting virtual directory and should have an empty value unless you would like to give it an extension and assign a different set of bindings to it. For example, in this case if the endpoint address="", then the actual communication endpoint is http://localhost/MyWCF/MathService.svc. Note that the contract attribute is set to the name of the interface, which is IMathService in this case. To expose the service through HTTP, the binding attribute is set to "wsHttpBinding".
4. Create the Client
4. Create the Client
To test the service, let us create an ASP.NET Web site named WCFServiceClient with Visual C# as the language of choice. Once the Web site is created, you now need to create a proxy for the WCF service that was created earlier. In addition, you also need to create a configuration file that contains the configuration settings required to connect to the service. You can accomplish both of these through a new utility called svcutil.exe.
5. Create the Proxy and Configuration file for the Client
The Service Model Metadata Utility (svcutil.exe) builds the proxy and the corresponding configuration file based on the published service's metadata (WSDL + Schema + Policy). Once the service is up, running and listening, you can run svcutil.exe against the service endpoint address. For example, if it were an IIS-hosted service, it would be something like the following:
To use the svcutil utility, open up the command prompt through Start->Programs->Microsoft Visual Studio 2005->Visual Studio Tools->Visual Studio 2005 Command Prompt.
6. Implement the Client
Now that you have created the proxy and the configuration file, you are ready to work with them from the client application. Before that, create a new ASP.NET Web site named WCFBasicsServiceClient using Visual C# as the programming language. Once the Web site is created, add the proxy file MathService.cs to the App_Code folder. Also rename the output.config file created through the svcutil to Web.config and add that to the root of the Web site. If you open up the output.config file, you will notice it contains a number of configuration entries for controlling the invocation behavior of the client. However it is also possible for you to manually generate the Web.config file using the below lines of code:
Now that you have created the proxy and the configuration file, you are ready to work with them from the client application. Before that, create a new ASP.NET Web site named WCFBasicsServiceClient using Visual C# as the programming language. Once the Web site is created, add the proxy file MathService.cs to the App_Code folder. Also rename the output.config file created through the svcutil to Web.config and add that to the root of the Web site. If you open up the output.config file, you will notice it contains a number of configuration entries for controlling the invocation behavior of the client. However it is also possible for you to manually generate the Web.config file using the below lines of code:
Note the use of as the root of the service client configuration section. Inside that you have an element named that specifies that you are dealing with client configuration sections here. Underneath the element, you have an element named that captures the address, binding, and the contract information about the service.
At this point, you are ready to consume the service by writing code against the proxy. To this end, add a new ASP.NET Web page named MathServiceClient.aspx and modify its code to look as follows:
At this point, you are ready to consume the service by writing code against the proxy. To this end, add a new ASP.NET Web page named MathServiceClient.aspx and modify its code to look as follows:
The above implementation is very simple. It invokes the various methods of the MathService using the proxy class (generated through the svcutil utility) from the previous section and displays the results of the invocation through a label control. If you navigate to the page using the browser, you should see an output that is somewhat similar to the following:
No comments:
Post a Comment