【问题标题】:ServiceBehavior for WCF RESTWCF REST 的服务行为
【发布时间】:2012-11-22 13:30:01
【问题描述】:

我在为我的 WCF 服务配置 ServiceBehavior 时遇到问题。

一些背景。 基本上我正在开发一个应该在 IIS 上运行的 REST 服务 WCF。 我需要能够记录服务抛出的异常(我正在使用 log4net)并根据异常类型返回 HTTP 状态代码。 我希望我的服务实现对 WCF 相关的东西有最少的了解,所以我不想在服务的任何地方都将异常转换为 FaultException。 所以我发现将我自己的 IErrorHandler 添加到服务主机将是最好的方法。

然而,我的问题是,无论我尝试什么,我似乎都无法在 Web.config 中获得我的自定义 ServiceBehavior 的配置。 这是相关代码。

网络配置。

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </modules>
</system.webServer>

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="UsingErrorLogBehavior">
        <errorLogBehavior/>
      </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior>
        <webHttp/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <extensions>
    <behaviorExtensions>
      <add name="errorLogBehavior"
           type="MyNameSpace.Web.ErrorExtensionElement, MyNameSpace.Web"/>
    </behaviorExtensions>
  </extensions>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  <standardEndpoints>
    <webHttpEndpoint>
      <standardEndpoint name="" helpEnabled="true" 
                        automaticFormatSelectionEnabled="false" 
                        defaultOutgoingResponseFormat="Json" 
                        maxReceivedMessageSize="4194304" transferMode="Buffered" />
    </webHttpEndpoint>
  </standardEndpoints>
</system.serviceModel>

错误扩展元素。

namespace MyNameSpace.Web
{
    public class ErrorExtensionElement : BehaviorExtensionElement
    {
        public override Type BehaviorType
        {
            get { return typeof(ErrorServiceBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new ErrorServiceBehavior();
        }
    }
}

错误服务行为。

namespace MyNameSpace.Web
{
    public class ErrorServiceBehavior : IServiceBehavior
    {
        public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
            {
                channelDispatcher.ErrorHandlers.Add(new ExceptionModule());
            }
        }

        public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
        }
    }
}

ExceptionModule 实现 IErrorHandler 的地方。

【问题讨论】:

    标签: wcf ierrorhandler servicebehavior


    【解决方案1】:

    您有一个名为“UsingErrorLogBehavior”的&lt;serviceBehavior&gt; 部分,但没有服务配置引用该部分。您可以将该部分设为 默认 服务行为(通过不给它命名,就像您为端点行为所做的那样),或者为您的服务添加一个引用该行为的 &lt;service&gt; 元素:

    <services>
      <service name="YourNamespace.YourServiceName"
               behaviorConfiguration="UsingErrorLogBehavior">
        <endpoint address=""
                  binding="webHttpBinding"
                  contract="YourNamespace.YourContractName" />
      </service>
    </services>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 2019-08-15
      • 2010-11-26
      • 1970-01-01
      相关资源
      最近更新 更多