Create WCF Web Service In Visual Studio 2015

Open Visual Studio 2015 and go to File > New > Project, as shown below.

WCF

Figure-1

Go to Installed > Templates > Visual C# > WCF and select WCF Service Application. Write WelcomeWCFService and choose location for your project, followed by clicking OK, as shown in Figure-2.

WCF
Figure-2

During the project creation, Visual Studio will create some default files, as highlighted in Solution Explorer Window in Figure-3.

WCF


Figure-3

Service Contract

For Service contract detail, see WCF Service Components.

Service Implementation

It is a class, which implements Service contract (interface) and in this class, you will define your business logic or behaviors.

Configuration file

Configuration file (web.config) is the place, where you can define the end points (see the details for the end points WCF Service Components) and other configurations for your Web Service.

Rename the IService1 and Service1 by right clicking on each and select rename, as show in Figure-4.

WCF
.
Figure-4

A prompt will open, which asks about the changes of all the references. Click OK and Visual Studio will automatically rename these files throughout the project

WCF 

Alternatively you can delete these files and recreate by right clicking on the project, followed by going to Add > New item, as shown below in fFgure-5.

WCF 

Figure-5

Now, select WCF Service. Write the name WelcomeWCFService and click Add. It will automatically generate IWelcomeWCFService and WelcomeWCFService, as shown below.

WCF 

Figure-6

Add the code in IWelcomeWCFService.cs file, as highlighted below.

  1. namespace WelcomeWCFService {  
  2.     [ServiceContract]  
  3.     public interface IWelcomeWCFService {  
  4.         [OperationContract]  
  5.         void DoWork();  
  6.         [OperationContract]  
  7.         [WebGet(UriTemplate = "/Welcome/{name}")]  
  8.         string Welcome(string name);  
  9.     }  
  10. }  

Add the namespace for WebGet attribute (this attribute maps your methods and its parameters to the specific URL, which can be accessed through Http GET request) by clicking Quick acitons icon, as show in Figure-7.

WCF 

Figure-7

Add the code in WelcomeWCFService.svc.cs file, as highlighted below.
  1. namespace WelcomeWCFService {  
  2.     public class WelcomeWCFService: IWelcomeWCFService {  
  3.         public void DoWork() {  
  4.             throw new NotImplementedException();  
  5.         }  
  6.         public string Welcome(string name) {  
  7.             return "Welcome to the first WCF Web Service Application " + name;  
  8.         }  
  9.     }  
  10. }  
Edit your web.config file by adding the elements (endpointBehaviors and add), as highlighted below.
  1. <system.serviceModel>  
  2.     <behaviors>  
  3.         <serviceBehaviors>  
  4.             <behavior>  
  5.                 <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />  
  6.                 <serviceDebug includeExceptionDetailInFaults="false" /> </behavior>  
  7.         </serviceBehaviors>  
  8.         <endpointBehaviors>  
  9.             <behavior>  
  10.                 <webHttp/> </behavior>  
  11.         </endpointBehaviors>  
  12.     </behaviors>  
  13.     <protocolMapping>  
  14.         <add binding="webHttpBinding" scheme="http" />  
  15.         <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping>  
  16.     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>  

endpointBehaviors indicates that this Web Service endpoint will be accessed, using REST and add element specifies the REST based Http communication protocol.

Build your project to ensure that your project does not contain any errors.

WCF
If your project is successfully built, then select your WelcomeWCFService.svc file from Solution Explorer Window. Right click it, followed by selecting View in the Browser, as shown in Figure-8.

WCF
Fgure-8

If your Browser view is like the picture given, then congratulations; as your first WCF Service has successfully deployed.

WCF

You can access your method by appending /Welcome/YourName (/mehtodName/parameterValue) to your Browser URL, as shown in Figure-9.

WCF
Figure-9

As you can see the response is in XML format; this is all about REST based XML WCF Web Service. For more information regarding REST, see SOAP and REST

Next Recommended Readings