Search This Blog

Wednesday, May 6, 2015

WCF Service Library Convert to WCF Rest and Implement to WebApplication

Download the project WcfServiceLibrary&WebApplication

1.creating a new project type: WCF Service Library


2. Modify the below line (yellow shaded)


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

namespace WcfServiceLibrary2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(UriTemplate = "/GetData/{value}", ResponseFormat = WebMessageFormat.Json)]
        string GetData(string  value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    // You can add XSD files into the project. After building the project, you can directly use the data types defined there, with the namespace "WcfServiceLibrary2.ContractType".
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

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

namespace WcfServiceLibrary2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class Service1 : IService1
    {
        public string GetData(string value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}
 ---------------------------------------------------
Create One Webapplication and WcfServiceLibrary2 reference. Then add one textfile and change the .txt to .svc.

Add below line on that svc file

<%@ ServiceHost Service="WcfServiceLibrary2.Service1" Language="C#" Debug="true" %>

Web Config change:

<system.serviceModel>
    <services>
      <service name ="WcfServiceLibrary2.Service1"  behaviorConfiguration="WcfServiceLibrary2.Service1Behavior">
        <endpoint address =""  behaviorConfiguration="WcfServiceLibrary2.WCFRestBehavior" binding ="webHttpBinding" contract ="WcfServiceLibrary2.IService1" />   
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>   
    <behaviors>
      <endpointBehaviors>
        <behavior name="WcfServiceLibrary2.WCFRestBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors    
      <serviceBehaviors>
        <behavior name="WcfServiceLibrary2.Service1Behavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>