I'm using self-hosted (programmatically hosted) WCF-service in Web Application.
I placed the [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
attribute to the SampleService class and placed <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false"/>
element to the Web.config in system.serviceModel
section.
I'm hosting my WCF-service in Global.asax in Application_Start method using next code:
protected void Application_Start(object sender, EventArgs e)
{
var serviceType = typeof (SampleService);
var serviceInterfaceType = typeof(ISampleService);
var baseAddresses = new Uri(@"https://localhost:443/SilverWIF.WEB/SampleService");
var serviceHost = new ServiceHost(serviceType, baseAddresses);
var smb = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null)
{
smb = new ServiceMetadataBehavior { HttpsGetEnabled = true };
serviceHost.Description.Behaviors.Add(smb);
}
else smb.HttpsGetEnabled = true;
var sdb = serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
if (sdb == null)
{
sdb = new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true };
serviceHost.Description.Behaviors.Add(sdb);
}
else sdb.IncludeExceptionDetailInFaults = true;
serviceHost.Description.Endpoints.Clear();
serviceHost.AddServiceEndpoint(serviceInterfaceType, _getGustomBinding(), string.Empty);
serviceHost.Open();
}
private static CustomBinding _getGustomBinding()
{
var binaryMessageEncodingBindingElement = new BinaryMessageEncodingBindingElement();
var httpsTransportBindingElement = new HttpsTransportBindingElement();
var binding = new CustomBinding(binaryMessageEncodingBindingElement, httpsTransportBindingElement);
return binding;
}
Despite all this, I has HttpContext.Current == null (I'm trying to access it from one of he methods of SampleService
class).
It is possible to access HttpContext.Current when WCF-service programmatically hosted? Can anybody help me with this problem?