我有一个要求,可以从RESTful WCF服务中访问HttpContext.Current。我知道我能做到这一点通过添加以下来配置:如何在代码中设置ServiceHostingEnvironment.AspNetCompatibilityEnabled = true(不在配置中).NET/C#
<serviceHostingEnvironment aspNetCompatibilityEnabled=”true” />
,并使用我服务的以下属性:
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Required)]
这里是我的问题,我需要“旋转起来”在代码中的服务实例单元测试,因此我不能使用配置文件来指定服务bebaviours等 目前我的代码看起来像下面,但尽管淘网,我一直无法解决如何设置一个ServiceHostingEnvironment类并将AspNetCompatibilityEnabled属性设置为true而不使用配置,任何人都可以帮忙吗?
string serviceUrl = "http://localhost:8082/MyService.svc";
_host = new ServiceHost(typeof(MyService), new Uri[] { new Uri(serviceUrl) });
ServiceEndpoint serviceEndpoint
= _host.AddServiceEndpoint(typeof(IMyService), new WebHttpBinding(), string.Empty);
serviceEndpoint.Behaviors.Add(new WebHttpBehavior());
// Here's where I'm stuck, i need something like...
ServiceHostingEnvironmentSection shes = new ServiceHostingEnvironmentSection();
shes.AspNetCompatibilityEnabled = true;
_host.Add(shes);
_host.Open();
任何帮助非常感谢,并提前感谢。
这甚至不涉及如何设置AspNetCompatibilityEnabled,这是他的问题的95%。 – 2013-12-10 23:57:44