简体   繁体   English

WCF REST的ServiceBehavior

[英]ServiceBehavior for WCF REST

I have a problem configuring a ServiceBehavior for my WCF service. 我在为WCF服务配置ServiceBehavior时遇到问题。

Some background. 一些背景。 Basically I am developing a REST service WCF that is supposed to run on IIS. 基本上我正在开发一个应该在IIS上运行的REST服务WCF。 I need to be able to log exceptions thrown by the service (I'm using log4net) and return HTTP status codes depending on the type of exception. 我需要能够记录服务抛出的异常(我正在使用log4net)并根据异常类型返回HTTP状态代码。 I want my service implementation to have a minimum knowledge of WCF related stuff, so I don't want to convert the exceptions to FaultException everywhere in the service. 我希望我的服务实现对WCF相关的东西知之甚少,所以我不想在服务的每个地方将异常转换为FaultException。 So I figured out that adding my own IErrorHandler to the service host would be the best way to do it. 所以我发现将自己的IErrorHandler添加到服务主机是最好的方法。

My problem is however that no matter what I try I can't seem to get the configuration for my custom ServiceBehavior right in Web.config. 我的问题是,无论我尝试什么,我似乎无法在Web.config中获得我的自定义ServiceBehavior的配置。 Here is the relevant code. 这是相关的代码。

Web config. 网络配置。

<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>

ErrorExtensionElement. ErrorExtensionElement。

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

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

ErrorServiceBehavior. 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)
        {
        }
    }
}

Where ExceptionModule implements IErrorHandler. ExceptionModule实现IErrorHandler的位置。

You have a <serviceBehavior> section named "UsingErrorLogBehavior", but no service configurations are referencing that section. 您有一个名为“UsingErrorLogBehavior”的<serviceBehavior>部分,但没有服务配置引用该部分。 You can either make that section the default service behavior (by not giving it a name, like you have for the endpoint behavior), or add a <service> element for your service which references that behavior: 您可以将该部分设置为默认服务行为(通过不为其命名,就像您对端点行为一样),或者为您的服务添加引用该行为的<service>元素:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM