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

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

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

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

Run your project & test your WCF service.


Post a Comment

  • Popular Posts
  • Comments