我創建了一個RESTful WCF 4.0服務。爲了捕獲和記錄未處理的異常,我創建了一個錯誤處理程序,我希望爲了記錄目的而收集服務器變量。然而,訪問HttpContext.Current.Request.ServerVariables拋出一個「值沒有在預期範圍內」的異常:訪問服務器變量時出現異常
public class MyErrorHandler : IErrorHandler, IServiceBehavior
{
public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
IErrorHandler errorHandler = new VirusInfoErrorHandler();
foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
{
ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;
if (channelDispatcher != null)
{
channelDispatcher.ErrorHandlers.Add(errorHandler);
}
}
}
public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
}
public bool HandleError(Exception error)
{
// I'd like to log here but accessing
// HttpContext.Current.Request.ServerVariables
// throws a "Value does not fall within the expected range" exception.
return true;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
}
}
我的配置如下:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="Rest">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceDebug includeExceptionDetailInFaults="False" httpHelpPageEnabled="False" />
<serviceAuthorization serviceAuthorizationManagerType="ACME.MyAuthorizationManager, ACME.Authorization" />
<errorHandler />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name ="errorHandler" type="ACME.MyErrorHandlerElement, ACME.MyErrorHandler"/>
</behaviorExtensions>
</extensions>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" faultExceptionEnabled="true" helpEnabled="true" automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IAppAccess" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
服務類和方法:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyService
{
[WebGet(UriTemplate="/my-method", ResponseFormat=WebMessageFormat.Json)]
public Message MyMethod()
{
// might return good things
// might throw an exception in downstream tiers
}
}
是否沒有辦法從WCF的上下文中檢索服務器變量?
爲什麼要檢索這些「變量」?在任何情況下它們都不存在。相反,看看你可以從各種上下文對象中獲得什麼。 – 2012-02-17 01:08:47
當你說「他們在任何情況下都不存在」時,你能解釋一下嗎?我關心的是檢索請求者的IP地址和服務器名稱等內容。這可能來自WebOperationContext嗎?我沒有看到從IErrorHander的方式。 – Bullines 2012-02-17 01:14:43
「服務器變量」是Web服務器發明的一種小說,它使腳本語言更容易訪問這些信息。有些地方沒有一套真正的變數。簡單的研究表明,你可以從'ProvideFault'中訪問'OperationContext'。 – 2012-02-17 01:39:39