我如何添加一个ErrorHandler到我的endpointbehavior在app.config

本文关键字:我的 endpointbehavior config app ErrorHandler 何添加 添加 一个 | 更新日期: 2023-09-27 18:15:54

所以我用Spring.net代理对象建立了几个ServiceHosts,一切都很好。现在我想在xml中添加一个自定义错误处理程序。如果有必要,我可以在春天站起来,但我不知道该怎么做。我的app.config如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
    </sectionGroup>
    <section name="databaseSettings" type="System.Configuration.NameValueSectionHandler"/>
  </configSections>
  <log4net>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="logs/crowbarserver.log"/>
      <maximumFileSize value="100KB"/>
      <maxSizeRollBackups value="20"/>
      <appendToFile value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger %ndc (%file:%line) - %message%newline"/>
      </layout>
    </appender>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="RollingFile"/>
    </root>
  </log4net>
  <spring>
    <context>
      <resource uri="config://spring/objects"/>
    </context>
    <objects>
      <object id="passDatabase" type="CrowbarCommon.Database.stub.StubDatabase, CrowbarCommon" autowire="autodetect" />
      <object id="authDatabase" type="CrowbarCommon.Database.stub.StubDatabase, CrowbarCommon" autowire="autodetect" />
      <object id="basicAuthManager" type="CrowbarCommon.Domain.AuthManagerImplementations.BasicAuthManager" >
        <constructor-arg name="passwordDatabase" ref="passDatabase"/>
        <constructor-arg name="authenticatedPlayerDatabase" ref="authDatabase"/>
      </object>
      <object id="serverService" singleton="false" type="DedicatedServer.ServerService, DedicatedServer" >
        <constructor-arg name="authManager" ref="basicAuthManager"/>
      </object>
      <object id="ServerServiceManager" type="Spring.ServiceModel.Activation.ServiceHostFactoryObject, Spring.Services">
        <property name="TargetName" value="serverService" />
      </object>
      <object id="playerService" singleton="false" type="DedicatedServer.PlayerService, DedicatedServer" >
        <constructor-arg name="authManager" ref="basicAuthManager"/>
      </object>    
      <object id="PlayerServiceManager" type="Spring.ServiceModel.Activation.ServiceHostFactoryObject, Spring.Services">
        <property name="TargetName" value="playerService" />
      </object>
      <object id="restService" singleton="false" type="RESTService.RestService, RESTService" />
      <object id="RESTServiceManager" type="Spring.ServiceModel.Activation.ServiceHostFactoryObject, Spring.Services">
        <property name="TargetName" value="restService" />
      </object>
    </objects>
  </spring>
  <databaseSettings>
    <add key="db.user" value="username" />
    <add key="db.password" value="password" />
    <add key="db.dataSource" value="server,port" />
    <add key="db.database" value="dbname" />
  </databaseSettings>
  <system.serviceModel>
    <services>
      <service name="serverService" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="CrowbarCommon.IServerService" behaviorConfiguration="web"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/server/"/>
          </baseAddresses>
        </host>
      </service>
      <service name="playerService" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="CrowbarCommon.IPlayerService" behaviorConfiguration="web"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/player/"/>
          </baseAddresses>
        </host>
      </service>
      <service name="restService" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="CrowbarCommon.IRestService" behaviorConfiguration="web"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/rest/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

我已经找到了如何在代码中做到这一点:

public class MyBehavior : IEndpointBehavior
{
    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new MyErrorHandler());
    }
}

有人能帮我吗?

我如何添加一个ErrorHandler到我的endpointbehavior在app.config

没关系,我已经回答了我自己的问题。

这篇文章很有启发性,并给出了一个完整的例子。您所需要做的就是按照这里的示例部分进行操作。