尽管 AspnetCompability 允许,但自托管 WCF 服务具有 HttpContext.Current == null

发布于 2024-09-07 15:27:01 字数 2006 浏览 3 评论 0原文

我在 Web 应用程序中使用自托管(以编程方式托管)WCF 服务。 我将 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 属性放置到 SampleService 类中,并放置 元素添加到 system.serviceModel 部分中的 Web.config。 我使用下一个代码在 Application_Start 方法中的 Global.asax 中托管我的 WCF 服务:

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;
}

尽管如此,我还是有 HttpContext.Current == null (我试图从 SampleService< 的方法之一访问它/代码> 类)。

当 WCF 服务以编程方式托管时,可以访问 HttpContext.Current 吗?有人能帮我解决这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦欢 2024-09-14 15:27:01

如果您的网站托管在 IIS7 中,则集成管道已发生更改,导致请求上下文在 Application_Start 事件中不可用。当使用经典模式[不推荐](在早期版本的 IIS 上运行时的唯一模式)时,请求上下文曾经可用,即使 Application_Start 事件始终旨在作为全局且应用程序生命周期中与请求无关的事件。在 IIS6 或 IIS7 Classic 模式下可以这样做(是否应该这样做是另一个讨论)的原因是因为 ASP.NET 应用程序总是由对应用程序的第一个请求启动,因此可以访问请求上下文通过静态 HttpContext.Current。

除了上面的解释之外,我不建议在网站内以这种方式托管您的服务。查看此链接以在 IIS 中托管(如何在控制台应用程序中实现自托管服务

希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文