Create WCF Web Service In Visual Studio 2015

Datetime:2017-04-18 06:29:41         Topic: WCF  Web Service  Visual Studio          Share        Original >>
Here to See The Original Article!!!

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

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.

Figure-2

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

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.

.

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

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.

Figure-5

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

Figure-6

Add the code in IWelcomeWCFService.cs file, as highlighted below.
namespace WelcomeWCFService {  
    [ServiceContract]  
    public interface IWelcomeWCFService {  
        [OperationContract]  
        void DoWork();  
        [OperationContract]  
        [WebGet(UriTemplate = "/Welcome/{name}")]  
        string Welcome(string name);  
    }  
}  

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.

Figure-7

Add the code in WelcomeWCFService.svc.cs file, as highlighted below.

namespace WelcomeWCFService {  
    public class WelcomeWCFService: IWelcomeWCFService {  
        public void DoWork() {  
            throw new NotImplementedException();  
        }  
        public string Welcome(string name) {  
            return "Welcome to the first WCF Web Service Application " + name;  
        }  
    }  
}  

Edit your web.config file by adding the elements (endpointBehaviors and add), as highlighted below.

<system.serviceModel>  
    <behaviors>  
        <serviceBehaviors>  
            <behavior>  
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />  
                <serviceDebug includeExceptionDetailInFaults="false" /> </behavior>  
        </serviceBehaviors>  
        <endpointBehaviors>  
            <behavior>  
                <webHttp/> </behavior>  
        </endpointBehaviors>  
    </behaviors>  
    <protocolMapping>  
        <add binding="webHttpBinding" scheme="http" />  
        <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping>  
    <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.

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.

Fgure-8

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

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

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, seeSOAP and REST








New