0

所以我用 Spring.net 代理对象建立了一些 ServiceHost,一切都很好。我现在想在 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());
    }
}

谁能帮我?

4

1 回答 1

0

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

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

于 2013-05-30T04:56:29.343 回答