2013-05-30 154 views
0

因此,我用Spring.net代理對象站出了幾個ServiceHosts,一切正常。我現在想要在xml中添加一個自定義錯誤處理程序。如果有必要,我可以在春天站起來,但我不知道如何做到這一點。我的app.config如下:如何在app.config中添加一個ErrorHandler到我的EndpointBehaviour

<?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()); 
    } 
} 

誰能幫助我?

回答

0

沒關係,我回答了我自己的問題。

THIS文章是最有啓發性的,並給了一個完整的例子。你所需要做的就是按照這裏的例子部分。

+0

Nicholas,對於使用WCF和Spring的RESTful Web服務的完整,簡單的示例,我將不勝感激。你在這裏已經有了配置文件,但是我沒有看到你的RestService是如何實現的,當然我不理解爲什麼這些配置正是以這種方式配置的,也就是說這些配件如何組合在一起。提前致謝。 – Michael

相關問題