Monday, 13 June 2011

Creating WCF Service


The first step is to open up Internet Information Services and expand the Default Web Site node, shown in Figure.

Right-click the Default Web Site and select New > Virtual Directory from the context menu. This will start the Virtual Directory Creation Wizard. The first page of this wizard is the Welcome Screen. Simply click the Next button on this step of the wizard.

This step in the wizard asks you to specify an alias for the virtual directory you are creating. For this example, type in WCFDemoService, then click the Next button.
The next step in the wizard is the Web Site Content Directory page. This page allows you to specify where the files for your web site, or in this case the WCF service, will be stored. At this point you don’t have one, so click the Browse button.

Clicking the Browse button brings up the Browse For Folder dialogwhich lets you browse to your destination directory. In this example you will create a new directory, and this dialog allows for that. Navigate to the Inetpub/wwwroot directory and click the wwwroot node to highlight it, and then select the Make New Folder button. This will create a new folder and let you rename it. Name the new folder WCFServiceDemo and click the OK button.

The next step in the wizard is the Virtual Directory Access Permissions page. You don’t need to make any changes to this page in the wizard, so go ahead and click Next. This brings you to the Finish page. Click Finish to create the virtual directory.
There are a few things you need to configure on this virtual directory to let you access it and make it completely functional, so right-click the WCFDemoService virtual directory and select Properties from the context menu. This will bring up the  WCFServiceDemo Properties shown in Figure.

On this Properties page you need to do two things:
  • First, click the Create button in the Application Settings section
  • Second, set the Execute Permissions to Scripts only
Once you have made these changes, click the Apply button. Your WCFServiceDemo Properties dialog should now look like the one in Figure.
Your work in IIS is now done. Go ahead and click OK on the WCFServiceDemo Properties dialog. You should now see a virtual directory called WCFServiceDemo in your IIS Manager

Fire up Visual Studio .NET 2005 and from the File menu, select New > Web Site. This will bring up the New Web Site dialog. In the New Web Site dialog, under the Visual Studio Installed Templates section, you will see a number of Web Site templates. If you have installed the .NET Framework 3.0 development tools, you will see a template called WCF Service.
Select the ASP.NET Web Service template and set the location to File System. For simplicity, this example uses a directory called WCFDemoService in the root of the C drive, but you can place it anywhere you’d like. Visual Studio will now create a new ASP.NET web service project with all the associated components.

The first step is to take a look in the Solution Explorer for your new project. Figure shows what your Solution Explorer should look like. You will notice that because this is an ASP.NET web service project Visual Studio has provided the Service.asmx page and all the other necessary components.

Because this will soon become a WCF service, you can actually get rid of the .asmx file. So, right click the Service.asmx file and select Delete from the context menu. Everything else can remain the same.
Now that you have deleted the .asmx file, you need to add a couple of files that are important to a WCF service. If you have built any ASP.NET web services, you will be familiar with one of the items you need to add. Go ahead and right-click the WCFDemoService project and select Add New Item. This will bring up the Add New Item dialog. Select the Web Configuration File from the Visual Studio Installed Templates section; make sure it is named Web.config, which should be done by default, and select Add.
The next item you need to add is not listed in the Add New Item dialog, but go ahead and bring up the Add New Item dialog again and this time select Text File from the available templates. However, make sure it is named Service.svc as shown in figure. Select Add.

Your Solution Explorer should now look like figure. You should see a Web.config file and a file called Service.svc, as well as the other service components.

The next step is probably the most vital step in this whole process, and that is to add a reference to the System.ServiceModel library. Right-click the WCFDemoService project again and select Add Reference. If you are using a CTP or RC1 you will probably need to browse to this file. If not it will be listed on the .NET tab. Because this example is using RC1, select the Browse tab and browse to the Windows\Microsoft.NET\Framework\v3.0\ Windows Communication Foundation directory. In that directory is a file called System.ServiceModel.dll as shown in figure. Select this file and click OK.

The System.ServiceModel.dll is the heart and soul of WCF. It provides all the necessary functionality of Windows Communication Foundation.
The first step is to write the service code. In Solution Explorer, double-click the Service.cs file underneath the App_Code folder and modify the contents of the file, replacing the code that is there with the following code:
using System;
using System.ServiceModel;
using System.Runtime.Serialization;

[ServiceContract]
 public interface IMyWCFService
{
          [OperationContract]
          string Operation1(string myvalue);
 }
 public class MyWCFService : IMyWCFService
 {
          public string Operation1(string myvalue)
          {
                    return “Hello: “ + myvalue;
          }
 }
Save the Service.cs file and open up the Service.svc file and add the following to it:
<% @ServiceHost Language=C# Debug=”true” Service=”MyWCFService”
             CodeBehind=”~/App_Code/Service.cs” %>
The last step is to modify the Web.config file. Open up the Web.config and replace the contents of that file with the following:
<?xml version=”1.0”?>
          <configuration xmlns=”http://schemas.microsoft.com/.NetConfiguration/v2.0”>
           <system.serviceModel>
          <services>
                   <service name=”MyWCFService” behaviorConfiguration=”returnFaults”>
<endpoint contract=”IMyWCFService” binding=”wsHttpBinding”
address=”http://localhost/WCFServiceDemo/service.svc”/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name=”returnFaults”>
<serviceMetadata httpGetEnabled=”true”/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug=”true”/>
</system.web>
</configuration>
That is all there is to it. Save all the files, and from the Build menu, select Build Project. The next step is to publish your newly created WCF service to the virtual directory you created earlier. To do this, rightclick the WCFDemoService project and select Publish Web Site from the context menu, as shown in figure.


The next step is to test your web service. This is easily accomplished by opening up Internet Explorer and typing in the following address URL:

Just like the service, the client also needs to “talk” WCF, and in order to do that a reference needs to be added to the System.ServiceModel.dll. So, just like you did for the service, add a reference to the System.ServiceModel.dll.
The second step is to add a reference to the WCF service. This step is a lot like adding a reference to a standard ASP.NET web service, except that instead of selecting a Web Reference, you select a Service Reference. To do this, right-click the References node of the WCFDemoClient project in Solution Explorer (you may need to click the “show all files” button in the Solution Explorer to see the References node), and from the context menu, select Add Service Reference, as shown in figure.


private void button1_Click(object sender, EventArgs e)
{
WCFServiceDemo.MyWCFServiceClient client = new    WCFServiceDemo.MyWCFServiceClient();
          txtResult.Text = client.Operation1(“Ravi”);
}

No comments:

Post a Comment