2
votes

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?

1
Yes, a WCF service is NOT a HTTP service - your HttpContext is supposed to be NULL, it's not the same, really. What do you need from the HttpContext??marc_s
I'm writing WIF-secured RP-application so I need to get caller identity (IClaimsPrincipal) in CheckAccess mthod in my ClaimsAuthorizationManager. I'm using SL.IdentityModel and SL.IdentityModel.Server libs provided in WIF Training Toolkit to provide authorization on the Silverlight client and trying to accsess user identity as it done in the examples. I read many information about WCF-service and Web-service difference and it is looks like that I can't acces caller identity in WCF-services like it I can make in Web-services. But, I still don't now how I can access user identity in WCF-servicesAshald
I need HttpContext.Current.User.Identity, in which in Web-services stored user identity.Ashald

1 Answers

0
votes

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.