Monday, June 2, 2014

Create WCF Service in class project - Part 1

In normal scenario when we need to host service we add separate WCF project to the solution. My Solution architecture is as follows:

In this scenario I need to add WCF service to the Business Layer and I need to host that service when web application is hosted (self hosted).

So I used following steps to implement above scenario.
  • Added Interface to Business Layer.
  [ServiceContract]  
   public interface IRedirectService  
   {   
     [OperationContract]  
     [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "RedirectHyperlink")]  
     void RedirectHyperlink(System.IO.Stream jsonString);       
   }  

  • Added Class to Business Layer  as follows and write the implementation.
   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]  
   public class RedirectService : IRedirectService  
   {  
     public void RedirectHyperlink(System.IO.Stream jsonString)  
     {  
       string val = JsonSteamToString(jsonString);  
       string JsonVal = val.Replace("\"", "'");  
       var json_serializer = new JavaScriptSerializer();  
       TestJsonStateObject myobj = json_serializer.Deserialize<H2JsonStateObject>(JsonVal);  
      
     }  
     public string JsonSteamToString(Stream jsonStream)  
     {  
       StreamReader reader = new StreamReader(jsonStream);  
       return reader.ReadToEnd();  
     }     
   }  
In this method jsonString contain string value of object. In order to deserialize the object I have used JavaScriptSerializer class. It can be found in System.Web.Script.Serialization namespace.

 using System.Web.Script.Serialization;  
after deserialize it can be conver into object which you have declared.

  public class TestJsonStateObject  
   {  
     public string url { get; set; }  
     public string Id { get; set; }  
   }  

  • Modify Web application Web.config file in order to communicate with wcf service.
  <system.serviceModel>  
   <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">  
    <serviceActivations>  
     <add factory="System.ServiceModel.Activation.ServiceHostFactory" relativeAddress="./RedirectService.svc" service="BusinessLayerProjectName.RedirectService" />  
    </serviceActivations>     
   </serviceHostingEnvironment>  
   <services>  
    <service behaviorConfiguration="ServiceBehaviour" name="BusinessLayerProjectName.RedirectService">  
     <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="BusinessLayerProjectName.IRedirectService" />  
    </service>  
   </services>  
   <behaviors>  
    <endpointBehaviors>  
     <behavior name="web">  
      <webHttp />  
     </behavior>  
    </endpointBehaviors>  
    <serviceBehaviors>  
     <behavior name="ServiceBehaviour">  
      <serviceMetadata httpGetEnabled="true" />  
      <serviceDebug includeExceptionDetailInFaults="true" />  
     </behavior>  
    </serviceBehaviors>  
   </behaviors>  
  </system.serviceModel>  

then once you hosted your site you can see hosted wcf service by providing correct URL.

 http://localhost:50594/RedirectService.svc  



No comments:

Post a Comment