AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Friday 26 February 2016

Configure WCF service in ASP.NET Website.

In this blog article I will discuss How to add WCF service in project. Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application. Please follow below steps to configure the project.

Step 1- Create new project or Website

Step 2- Add new WCF service
Right Click on project> Add New Item > Select WCF Service


After adding WCF Service directory structure will be shown as below Image.

Step 3- Setup Bindings in WebConfig


<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0" />
    <services>
      <service behaviorConfiguration="MyServiceBehavior" name="Service" >
        <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="IService" />      
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="WebBehavior">
          <webHttp helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="webBinding">
          <security mode="None">
            <transport clientCredentialType="None" />
          </security>
        </binding>
        <binding name="webBindingHTTPS">
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
  </system.serviceModel>

Step 3- Write service methods

Service Class


[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
 public void MethodType1(int parameter1,int parameter2)
 {
            //Do Something here
 }

        public void MethodType2(CustomEntityType obj)
 {
            //Do Something here
 }        
}

IService Class


[ServiceContract]
public interface IService
{
   [OperationContract]
   [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]    
   void MethodType1(int parameter1,int parameter2);
   
   [OperationContract]
   [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)] 
   void MethodType2(CustomEntityType obj);
 
}

Run your project & test your WCF service.

0 comments :

Post a Comment

  • Popular Posts
  • Comments