我有一個要求,可以從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