In normal scenario when we need to host service we add separate WCF project to the solution. My Solution architecture is as follows:
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();
}
}
using System.Web.Script.Serialization;
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