Monday, 19 February 2018

In this article, I will explain How to create  first RESTfull  API using WCF.

Step 1 :  Open visual studio and Go to File->New->Web Site



Step 2 : Enter Project name and location and Hit Ok


Step 3 : Right click on project WCFDemoApp->Add New Item->Add


Step 4 :  Select WCF Service and Enter Hello.svc and Hit Ok


Step 5 :  Now our project structure is as shown below .IHello.cs ,Hello.cs and Hello.svc fill will be added 


Step 6 : Open IHello.cs file and paste the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IHello" in both code and config file together.
[ServiceContract]
public interface IHello
{
    [WebInvoke(Method = "GET",
     ResponseFormat = WebMessageFormat.Json,
     RequestFormat = WebMessageFormat.Json,
     BodyStyle = WebMessageBodyStyle.Wrapped,
     UriTemplate = "HelloService")]
    [OperationContract]
   List<Employee> Hi();
}
[DataContract]
public class Employee
{
    [DataMember]
    public string FirstName { get; set; }
    [DataMember]
    public string LastName { get; set; }
}


Step 7 : Open Hello.cs file and paste the following code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Hello" in code, svc and config file together.
public class Hello : IHello
{
    public List<Employee> Hi()
    {
        List<Employee> employeeList = new List<Employee>
        {
            new Employee
            {
                FirstName="Manichandra",
                LastName="Rai"
            },
            new Employee
            {
                FirstName="Sengson",
                LastName="Rai"
            }
        };
        return employeeList;
    }
}


Step 8 : Hello.svc file has service name Hello which is required in web.config file.


Step 9 : Paste the following code in your web.config file just above the </configuration> . Here Service name "Hello" should be same as service in Hello.svc file and contract should be your interface "IHello".

<system.serviceModel>
    <services>
      <service name="Hello" behaviorConfiguration="ServiceBehavior">
        <endpoint binding="webHttpBinding" contract="IHello" address="" behaviorConfiguration="webHttp"/>
      </service>

    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="false"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
 Now our final web.config file 
<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.6.1"/>
      <httpRuntime targetFramework="4.6.1"/>
    </system.web>
    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs"
                type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
            <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
                type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
        </compilers>
    </system.codedom>

  <system.serviceModel>
    <services>
      <service name="Hello" behaviorConfiguration="ServiceBehavior">
        <endpoint binding="webHttpBinding" contract="IHello" address="" behaviorConfiguration="webHttp"/>
      </service>

    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="false"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
</configuration>
Step 10 : Right click on Hello.svc and browse with your browser



Step 11 : Now output is as shown below :


Step 11 :  Now type /HelloService after Hello.svc file which gives us json result :


Step 12 : Now run our WCF api on Postman gives output as shown below :


2 comments:

Powered by Blogger.

Followers

Translate

Currency Converter

Exchange Rate

Featured post

Interpolation in angular 5

When we want to communicate from component class to template ,we can use interpolation which use the { { expression }} to render the bound...

Popular Posts

My Facebook Page