views:

81

answers:

1

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?

A: 

If your website is hosted in IIS7 there has been a change for the Integrated Pipeline that makes the request context unavailable in Application_Start event. When using the Classic mode [NOT RECOMMENDED] (the only mode when running on previous versions of IIS), the request context used to be available, even though the Application_Start event has always been intended as a global and request-agnostic event in the application lifetime. The reason why this can be done (whether it should be done is another discussion) in IIS6 or IIS7 Classic mode is because ASP.NET applications were always started by the first request to the app, therefore it was possible to get to the request context through the static HttpContext.Current.

The above explanation aside I wouldn't recommend hosting your service in this way inside a website. Have a look at this link for hosting within IIS (How to implement a self-hosted service in a console application)

Hope this helps.

heads5150
I'm using "Classic .NET AppPool" because it is the only application pool in which I can debug my Web Application. Maybe, there is some way in which I can debug my Web Application in "ASP.NET v4.0" application pool, for example?In any case, even I'll host my WCF-services in Windows Service or Console application, as I know I can't use AspNet pipeline for my services.Maybe, there is a way, in which I can acces information, which usually stored in HttpContext.Curent.User.Identity using WCF-service specific classes/properties?
Ashald