如何使用 HTTP 上下文调试 WCF 服务? [英] How to debug a WCF Service with an HTTP Context?

查看:27
本文介绍了如何使用 HTTP 上下文调试 WCF 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调试 WCF 服务,但它需要有一个 HTTP 上下文.

I need to debug a WCF service but it needs to have an HTTP Context.

目前我有一个 WCF 服务网站的解决方案,当我单击调试时,它会启动,然后启动一个不包含测试表单的 html 页面.

Currently I have a solution with a WCF service web site, when I click on debug it starts up and then fires up an html page that contains no test form.

在项目运行时,我尝试手动启动 wcftestclient,然后提供我的服务地址,它找到了该服务,但是当我调用它时,它绕过了 IIS 层(或开发服务器),因此 httpContext 为 null...

While the project is running I tried starting the wcftestclient manually, then provided the address of my service, it finds the service but when I invoke it, it bypasses the IIS layer (or development server), so the httpContext is null...

通过 IIS 上下文调试 WCF 服务的正确方法是什么?

What is the correct way to debug a WCF service through an IIS context?

推荐答案

在 WCF 中,HttpContext 在默认情况下和设计上设置为 NULL,即使 WCF 服务托管在 IIS 中;毕竟,WCF 不是 ASP.NET.

In WCF, the HttpContext is set to NULL by default and by design, even if the WCF service is hosted in IIS; after all, WCF is not ASP.NET.

如果您确实需要 HttpContext,则需要通过配置单独打开它(web.config 如果您在 IIS 中托管,则您的自托管应用程序的 app.config 否则):

If you actually do need an HttpContext, you need to turn it on separately, through config (web.config if you host in IIS, your self-host app's app.config otherwise):

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

并且您需要通过将此属性放在您的服务类(实现服务契约)上来指定这一事实(您的服务允许甚至期望 ASP.NET 兼容模式):

and you need to specify that fact (that your service allows or even expects the ASP.NET compatibility mode) by putting this attribute on your service class (that implements the service contract):

[AspNetCompatibilityRequirements
(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]    
public class MyWCFService : IMyWCFService
{
  ......
}  

RequirementsMode=Allowed 只是简单地允许 ASP.NET 兼容模式,而 RequirementsMode=Required 实际上需要并且没有它就不能工作.

RequirementsMode=Allowed just simply allows the ASP.NET compatibility mode, while RequirementsMode=Required actually requires is and will not work without it.

一旦你这样做了,当你将调试器附加到 IIS 工作进程时,你应该得到你的 HttpContext.Current.

Once you do that, you should get your HttpContext.Current when you attach your debugger to the IIS worker process.

马克

这篇关于如何使用 HTTP 上下文调试 WCF 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆