Run Your First WCF Service

Hi Friends
Today is too much cold outside almost 3.0 degree temperature. I am siting inside and thought to post something new and good. So here i am going to tell you something really good and New in market..

WCF is a newly added framework in .NET3.0. Today i am going to teach you to build your first WCF Service.
So heer we Goooooooooo….

Step 1 Install .NET3.0 Framework
Step 2Install dotnetfx3 for running WCF template

Now you are ready to play with it …
(1)Start Visual Studio 2005.
(2)On the File menu, point to New and then click Project.
(3)In the New Project dialog box, expand the Visual C# node in the Project types tree, and then click NET Framework 3.0.
(4)In the Templates pane, select the WCF Service Library template
In the Name field, type MyMathService.


(5)In the Location field, type C:\Documents and Settings\YourName\My Documents\Microsoft Press\WCF Step By Step\ if you are using Windows XP, or C:\Users\YourName\Documents\Mirosoft Press\WCF Step By Step\ if you are using Windows Vista. To save space throughout the rest of this book, I will simply refer to the path “C:\Documents and Settings\YourName\My Documents” or “C:\Users\YourName\Documents as your “\My Documents” folder.

(6)Save it with Name “MyMathService”.

Define the data contract for the WCF service
Comment out the sample code and namespace in the MyMathService.cs file, as you are going to write your own code.

Add the following namespace to the end of the file:

namespace MathService
{
}

Add the MyMathService class shown below to the MathService namespace:

// Data contract describing the details of a functions
[DataContract]
public class math
{
[DataMember]
public int var1;

[DataMember]
public int var2;

[DataMember]
public int result;

}

Define the service contract for the WCF service

Add the IMyMathService interface shown below to the math namespace, underneath the MyMathService class:

// Service contract describing the operations provided by the WCF service
[ServiceContract]
public interface IMyMathService
{
[OperationContract]
int Add(int a, int b);

[OperationContract]
int Multiply(int a , int b)
}

Implement the WCF service
Add the following class to the MathService namespace, underneath the IMyMathService contract:

// WCF service class that implements the service contract
public class MyMathServiceImpl : IMyMathService
{
}

Notice that a class that provides a WCF service should indicate that it implements a service contract, in this case, IMyMathService , by using standard C# inheritance notation.

Adding the Add and Multiply method to the MyMathServiceImpl class:

public int Add(int a, int b)
{
return a+b;
}
public int Multiply (int a, int b)
{
return a*b;
}

Configure the WCF service
On the Project menu, click MyMathService Properties to display the Property pages for the project.
Click the Build tab. In the Output section of the page, change the Output path property to bin.
On the File menu, click Save All.
On the Project menu, click Add New Item.
In the Add New Item dialog box, select the Text File template. Change the name of the file to MyMathService.svc, and then click Add. This is the service definition file for the Web service.
Important The service definition file must have the same name as the Web service, and have the .svc suffix.

Add the following code to the MyMathService.svc file displayed in the code view window:


The Service attribute of the ServiceHost directive specifies the namespace (MathService) and the class (MyMathServiceImpl) that implements the service. The Assembly directive specifies the name of the assembly (MyMathService) containing this namespace and class.

Edit the Web.config file for the project in the Configuration tag:

set attribute value as follows

Service name=”MathService.MyMathServiceImpl”
endpoint address=”” binding=”basicHttpBinding” contract=”MathService.IMyMathService”

Now Save all the project and your Service is ready to run…

Now Next thing yoo need to do is Host the service on the IIS and browse the service .
Copy the URL of the service and add it to your TestClient code reference folder(By Add Reference->Add Service Reference)
Once you have added it now you can access it as

MathService.MyMathService objService= new MathService.MyMathService();
int addresult=objService.Add(2,5);
int multiplyresult=objService.Multiply(2,5);

THATS ALL …

It seems quite lengthy but once you will through you will fall in Love with it..
Take care .

Advertisements

2 thoughts on “Run Your First WCF Service

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s