Use HttpContext.Current in WCF Service

Last Reply on Jun 23, 2015 02:08 AM By Mudassar

Posted on Jun 22, 2015 07:23 AM

I have created an WCF service for client in ASp.net. However the Response is not getting from POst method. Below is the code snippet:

 

 

public void Post()
{
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Write("<html><head>");

System.Web.HttpContext.Current.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", FormName));

System.Web.HttpContext.Current.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", FormName, Method, Url));
for (int i = 0; i < Inputs.Keys.Count; i++)
{
System.Web.HttpContext.Current.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", Inputs.Keys[i], Inputs[Inputs.Keys[i]]));
}
System.Web.HttpContext.Current.Response.Write("</form>");
System.Web.HttpContext.Current.Response.Write("</body></html>");


System.Web.HttpContext.Current.Response.End();

}

 

Posted on Jun 23, 2015 02:08 AM

You can use HttpContext in the WCF Service. Follow the steps below.

1. Add aspNetCompatibilityEnabled="true" attribute to serviceHostingEnvironment Tag in Web.Config.

 <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>

2. Import the following Namespaces in WCF Service class

using System.Web;
using System.ServiceModel.Activation;

3. Now decorate the Service class with following AspNetCompatibilityRequirements Attribute.

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class Service : IService
{
    public string GetName()
    {
        return HttpContext.Current.User.Identity.Name;
    }
}