我在 Web 应用程序中使用自托管(以编程方式托管)WCF 服务。我将[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
属性放置在 SampleService 类中,并将<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false"/>
元素放置在部分的 Web.config 中system.serviceModel
。我使用下一个代码在 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 吗?有人可以帮我解决这个问题吗?