How to create WCF web service in C# .NET ( visual studio 2013 )

Web Services are a set of programming interfaces , that can be accessible from different software applications or different platforms using XML based messages. It is very simple to create WCF web service in C# .NET in visual studio 2013.

Steps to create WCF web service in C# .NET :

Step 1 :

Create a C# project in visual studio 2013 ( File -> New -> Project )

If you want , you can create a Web Site .

Step 2 :

Expand left side Visual C# menu and select WFC. Select WCF Service Application from the right side. Give any name for your project at bottom text box . In our example, the name is FirstWCFServiceExe .

Click finish ,the new project will create in the Solution Explorer .

Step 3 :

Add a New Item to the project (Right click on the project , select Add -> New Item).

Step 4 :

From the New Item popup window , select Web -> WCF Service . Give any name to the service in the text box (Example : FirstService.svc ).

Step 5 :

So the New Item added to the project tree .

Step 6 :

Copy the below code to FirstService.svc.cs and IFirstService.cs files .

FirstService.svc.cs

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespaceFirstWCFServiceExe

{

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "FirstService" in code, svc and config file together.

// NOTE: In order to launch WCF Test Client for testing this service, please select FirstService.svc or FirstService.svc.cs at the Solution Explorer and start debugging.

publicclassFirstService:IFirstService

{

publicstringDoWork()

{

return"hellow world";

}

}

}

The above class having a simple method which returns a string.

IFirstService.cs

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespaceFirstWCFServiceExe

{

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IFirstService" in both code and config file together.

[ServiceContract]

publicinterfaceIFirstService

{

[OperationContract]

stringDoWork();

}

}

Step 7 :

Build the project (Ctrl+F7) .

Step 8 :

If it build successfully , the below window will appear . Copy the below URL .

Step 9 :

Add ?wsdl at the end of the URL http://localhost:49179/FirstService.svc .Now the full URL is http://localhost:49179/FirstService.svc?wsdl .
Paste the URL in the web browser . Finally , you will get the WSDL file.

Now you can test the service with SoapUI by using the WSDL url . We explained here that how to test with SoapUI . create WCF web service in C# .NET