如何使用spring.net注入wcf代理

如何使用spring.net注入wcf代理,.net,wcf,spring.net,.net,Wcf,Spring.net,我是Spring.Net的新手。 我有一个wcf服务类,它需要另一个服务的代理,如: [ServiceContract] Interface IService1{ [OperationContract] void show(); } [ServiceContract] Interface IService2{ [OperationContract] void show(); } class Service1:IService1 { public IService2 Se

我是Spring.Net的新手。 我有一个wcf服务类,它需要另一个服务的代理,如:

[ServiceContract]
Interface IService1{
  [OperationContract]
  void show();

}

[ServiceContract]
Interface IService2{
  [OperationContract]
  void show();

}
class Service1:IService1
{
  public IService2 Service2
  {
    get;set;
  }

  public void Show()
  {
    Service2.Show();
  }
}
<sectionGroup name="spring">
  <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
  <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
  <object id="Service1Obj" singleton="false"
               type="MyNamespace.Service1, MyAssembly">
    <!-- Setter injection-->
    <property name="Service2" ref="Service2"/>
  </object>

  <object id="Service2Obj" singleton="false"
         type="MyNamespace.Service2, MyAssembly">
  </object>

  <!-- Creating Client side proxy dynamically-->
  <object id="Service1"
    type="MyNamespace.IService1, MyAssembly"
    factory-object="Service1ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service1ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService1>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service1Endpoint" />
  </object>

  <object id="Service2"
    type="MyNamespace.IService2, MyAssembly"
    factory-object="Service2ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service2ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService2>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service2Endpoint" />
  </object>
</objects>
  <service name="Service2Obj" behaviorConfiguration="DefaultBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8001"/>
      </baseAddresses>
    </host>
    <endpoint address=""
              binding="basicHttpBinding"
              contract="MyNamespace.IService2" />
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<client>
  <!--<endpoint name="serverWebCalculatorEndpoint" address="http://localhost:2637/Spring.WcfQuickStart.ServerWeb/Calculator.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="Spring.WcfQuickStart.ICalculator"/>-->
  <endpoint name="Service1Endpoint" address="http://localhost:8000" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService1"/>
  <endpoint name="Service2Endpoint" address="http://localhost:8001" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService2"/>
</client>
如何通过在Spring.Net的App.config文件中配置service2的代理,将其注入Service1代理中

<sectionGroup name="spring">
  <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
  <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
  <object id="Service1Obj" singleton="false"
               type="MyNamespace.Service1, MyAssembly">
    <!-- Setter injection-->
    <property name="Service2" ref="Service2"/>
  </object>

  <object id="Service2Obj" singleton="false"
         type="MyNamespace.Service2, MyAssembly">
  </object>

  <!-- Creating Client side proxy dynamically-->
  <object id="Service1"
    type="MyNamespace.IService1, MyAssembly"
    factory-object="Service1ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service1ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService1>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service1Endpoint" />
  </object>

  <object id="Service2"
    type="MyNamespace.IService2, MyAssembly"
    factory-object="Service2ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service2ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService2>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service2Endpoint" />
  </object>
</objects>
  <service name="Service2Obj" behaviorConfiguration="DefaultBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8001"/>
      </baseAddresses>
    </host>
    <endpoint address=""
              binding="basicHttpBinding"
              contract="MyNamespace.IService2" />
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<client>
  <!--<endpoint name="serverWebCalculatorEndpoint" address="http://localhost:2637/Spring.WcfQuickStart.ServerWeb/Calculator.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="Spring.WcfQuickStart.ICalculator"/>-->
  <endpoint name="Service1Endpoint" address="http://localhost:8000" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService1"/>
  <endpoint name="Service2Endpoint" address="http://localhost:8001" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService2"/>
</client>
App.config文件 @bbai请找到下面的app.config文件。ServiceHost im正在我的Program.cs文件中创建。

<sectionGroup name="spring">
  <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
  <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
  <object id="Service1Obj" singleton="false"
               type="MyNamespace.Service1, MyAssembly">
    <!-- Setter injection-->
    <property name="Service2" ref="Service2"/>
  </object>

  <object id="Service2Obj" singleton="false"
         type="MyNamespace.Service2, MyAssembly">
  </object>

  <!-- Creating Client side proxy dynamically-->
  <object id="Service1"
    type="MyNamespace.IService1, MyAssembly"
    factory-object="Service1ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service1ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService1>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service1Endpoint" />
  </object>

  <object id="Service2"
    type="MyNamespace.IService2, MyAssembly"
    factory-object="Service2ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service2ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService2>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service2Endpoint" />
  </object>
</objects>
  <service name="Service2Obj" behaviorConfiguration="DefaultBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8001"/>
      </baseAddresses>
    </host>
    <endpoint address=""
              binding="basicHttpBinding"
              contract="MyNamespace.IService2" />
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<client>
  <!--<endpoint name="serverWebCalculatorEndpoint" address="http://localhost:2637/Spring.WcfQuickStart.ServerWeb/Calculator.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="Spring.WcfQuickStart.ICalculator"/>-->
  <endpoint name="Service1Endpoint" address="http://localhost:8000" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService1"/>
  <endpoint name="Service2Endpoint" address="http://localhost:8001" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService2"/>
</client>

<sectionGroup name="spring">
  <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
  <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
  <object id="Service1Obj" singleton="false"
               type="MyNamespace.Service1, MyAssembly">
    <!-- Setter injection-->
    <property name="Service2" ref="Service2"/>
  </object>

  <object id="Service2Obj" singleton="false"
         type="MyNamespace.Service2, MyAssembly">
  </object>

  <!-- Creating Client side proxy dynamically-->
  <object id="Service1"
    type="MyNamespace.IService1, MyAssembly"
    factory-object="Service1ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service1ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService1>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service1Endpoint" />
  </object>

  <object id="Service2"
    type="MyNamespace.IService2, MyAssembly"
    factory-object="Service2ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service2ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService2>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service2Endpoint" />
  </object>
</objects>
  <service name="Service2Obj" behaviorConfiguration="DefaultBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8001"/>
      </baseAddresses>
    </host>
    <endpoint address=""
              binding="basicHttpBinding"
              contract="MyNamespace.IService2" />
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<client>
  <!--<endpoint name="serverWebCalculatorEndpoint" address="http://localhost:2637/Spring.WcfQuickStart.ServerWeb/Calculator.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="Spring.WcfQuickStart.ICalculator"/>-->
  <endpoint name="Service1Endpoint" address="http://localhost:8000" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService1"/>
  <endpoint name="Service2Endpoint" address="http://localhost:8001" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService2"/>
</client>

<sectionGroup name="spring">
  <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
  <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
  <object id="Service1Obj" singleton="false"
               type="MyNamespace.Service1, MyAssembly">
    <!-- Setter injection-->
    <property name="Service2" ref="Service2"/>
  </object>

  <object id="Service2Obj" singleton="false"
         type="MyNamespace.Service2, MyAssembly">
  </object>

  <!-- Creating Client side proxy dynamically-->
  <object id="Service1"
    type="MyNamespace.IService1, MyAssembly"
    factory-object="Service1ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service1ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService1>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service1Endpoint" />
  </object>

  <object id="Service2"
    type="MyNamespace.IService2, MyAssembly"
    factory-object="Service2ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service2ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService2>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service2Endpoint" />
  </object>
</objects>
  <service name="Service2Obj" behaviorConfiguration="DefaultBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8001"/>
      </baseAddresses>
    </host>
    <endpoint address=""
              binding="basicHttpBinding"
              contract="MyNamespace.IService2" />
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<client>
  <!--<endpoint name="serverWebCalculatorEndpoint" address="http://localhost:2637/Spring.WcfQuickStart.ServerWeb/Calculator.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="Spring.WcfQuickStart.ICalculator"/>-->
  <endpoint name="Service1Endpoint" address="http://localhost:8000" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService1"/>
  <endpoint name="Service2Endpoint" address="http://localhost:8001" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService2"/>
</client>

<sectionGroup name="spring">
  <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
  <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
  <object id="Service1Obj" singleton="false"
               type="MyNamespace.Service1, MyAssembly">
    <!-- Setter injection-->
    <property name="Service2" ref="Service2"/>
  </object>

  <object id="Service2Obj" singleton="false"
         type="MyNamespace.Service2, MyAssembly">
  </object>

  <!-- Creating Client side proxy dynamically-->
  <object id="Service1"
    type="MyNamespace.IService1, MyAssembly"
    factory-object="Service1ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service1ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService1>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service1Endpoint" />
  </object>

  <object id="Service2"
    type="MyNamespace.IService2, MyAssembly"
    factory-object="Service2ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service2ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService2>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service2Endpoint" />
  </object>
</objects>
  <service name="Service2Obj" behaviorConfiguration="DefaultBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8001"/>
      </baseAddresses>
    </host>
    <endpoint address=""
              binding="basicHttpBinding"
              contract="MyNamespace.IService2" />
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<client>
  <!--<endpoint name="serverWebCalculatorEndpoint" address="http://localhost:2637/Spring.WcfQuickStart.ServerWeb/Calculator.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="Spring.WcfQuickStart.ICalculator"/>-->
  <endpoint name="Service1Endpoint" address="http://localhost:8000" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService1"/>
  <endpoint name="Service2Endpoint" address="http://localhost:8001" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService2"/>
</client>

<sectionGroup name="spring">
  <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
  <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
  <object id="Service1Obj" singleton="false"
               type="MyNamespace.Service1, MyAssembly">
    <!-- Setter injection-->
    <property name="Service2" ref="Service2"/>
  </object>

  <object id="Service2Obj" singleton="false"
         type="MyNamespace.Service2, MyAssembly">
  </object>

  <!-- Creating Client side proxy dynamically-->
  <object id="Service1"
    type="MyNamespace.IService1, MyAssembly"
    factory-object="Service1ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service1ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService1>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service1Endpoint" />
  </object>

  <object id="Service2"
    type="MyNamespace.IService2, MyAssembly"
    factory-object="Service2ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service2ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService2>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service2Endpoint" />
  </object>
</objects>
  <service name="Service2Obj" behaviorConfiguration="DefaultBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8001"/>
      </baseAddresses>
    </host>
    <endpoint address=""
              binding="basicHttpBinding"
              contract="MyNamespace.IService2" />
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<client>
  <!--<endpoint name="serverWebCalculatorEndpoint" address="http://localhost:2637/Spring.WcfQuickStart.ServerWeb/Calculator.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="Spring.WcfQuickStart.ICalculator"/>-->
  <endpoint name="Service1Endpoint" address="http://localhost:8000" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService1"/>
  <endpoint name="Service2Endpoint" address="http://localhost:8001" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService2"/>
</client>

<sectionGroup name="spring">
  <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
  <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
  <object id="Service1Obj" singleton="false"
               type="MyNamespace.Service1, MyAssembly">
    <!-- Setter injection-->
    <property name="Service2" ref="Service2"/>
  </object>

  <object id="Service2Obj" singleton="false"
         type="MyNamespace.Service2, MyAssembly">
  </object>

  <!-- Creating Client side proxy dynamically-->
  <object id="Service1"
    type="MyNamespace.IService1, MyAssembly"
    factory-object="Service1ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service1ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService1>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service1Endpoint" />
  </object>

  <object id="Service2"
    type="MyNamespace.IService2, MyAssembly"
    factory-object="Service2ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service2ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService2>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service2Endpoint" />
  </object>
</objects>
  <service name="Service2Obj" behaviorConfiguration="DefaultBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8001"/>
      </baseAddresses>
    </host>
    <endpoint address=""
              binding="basicHttpBinding"
              contract="MyNamespace.IService2" />
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<client>
  <!--<endpoint name="serverWebCalculatorEndpoint" address="http://localhost:2637/Spring.WcfQuickStart.ServerWeb/Calculator.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="Spring.WcfQuickStart.ICalculator"/>-->
  <endpoint name="Service1Endpoint" address="http://localhost:8000" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService1"/>
  <endpoint name="Service2Endpoint" address="http://localhost:8001" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService2"/>
</client>

<sectionGroup name="spring">
  <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
  <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
  <object id="Service1Obj" singleton="false"
               type="MyNamespace.Service1, MyAssembly">
    <!-- Setter injection-->
    <property name="Service2" ref="Service2"/>
  </object>

  <object id="Service2Obj" singleton="false"
         type="MyNamespace.Service2, MyAssembly">
  </object>

  <!-- Creating Client side proxy dynamically-->
  <object id="Service1"
    type="MyNamespace.IService1, MyAssembly"
    factory-object="Service1ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service1ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService1>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service1Endpoint" />
  </object>

  <object id="Service2"
    type="MyNamespace.IService2, MyAssembly"
    factory-object="Service2ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service2ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService2>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service2Endpoint" />
  </object>
</objects>
  <service name="Service2Obj" behaviorConfiguration="DefaultBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8001"/>
      </baseAddresses>
    </host>
    <endpoint address=""
              binding="basicHttpBinding"
              contract="MyNamespace.IService2" />
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<client>
  <!--<endpoint name="serverWebCalculatorEndpoint" address="http://localhost:2637/Spring.WcfQuickStart.ServerWeb/Calculator.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="Spring.WcfQuickStart.ICalculator"/>-->
  <endpoint name="Service1Endpoint" address="http://localhost:8000" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService1"/>
  <endpoint name="Service2Endpoint" address="http://localhost:8001" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService2"/>
</client>

在WCF中使用IoC并不简单,但是在这种情况下,您可以使用
IInstanceProvider
在创建服务时解析对IService2的引用。post at有一个使用该接口为WCF服务实现简单IoC容器的示例。

如下所示:

<sectionGroup name="spring">
  <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
  <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
  <object id="Service1Obj" singleton="false"
               type="MyNamespace.Service1, MyAssembly">
    <!-- Setter injection-->
    <property name="Service2" ref="Service2"/>
  </object>

  <object id="Service2Obj" singleton="false"
         type="MyNamespace.Service2, MyAssembly">
  </object>

  <!-- Creating Client side proxy dynamically-->
  <object id="Service1"
    type="MyNamespace.IService1, MyAssembly"
    factory-object="Service1ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service1ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService1>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service1Endpoint" />
  </object>

  <object id="Service2"
    type="MyNamespace.IService2, MyAssembly"
    factory-object="Service2ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service2ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService2>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service2Endpoint" />
  </object>
</objects>
  <service name="Service2Obj" behaviorConfiguration="DefaultBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8001"/>
      </baseAddresses>
    </host>
    <endpoint address=""
              binding="basicHttpBinding"
              contract="MyNamespace.IService2" />
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<client>
  <!--<endpoint name="serverWebCalculatorEndpoint" address="http://localhost:2637/Spring.WcfQuickStart.ServerWeb/Calculator.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="Spring.WcfQuickStart.ICalculator"/>-->
  <endpoint name="Service1Endpoint" address="http://localhost:8000" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService1"/>
  <endpoint name="Service2Endpoint" address="http://localhost:8001" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService2"/>
</client>
<objects xmlns="http://www.springframework.net"
         xmlns:wcf="http://www.springframework.net/wcf">

  <!-- Service2 proxy -->
  <wcf:channelFactory id="Service2"
       channelType="MyNamespace.IService2, MyAssembly"
       endpointConfigurationName="Service2Endpoint" />

  <!-- Service 1 -->
  <object id="Service1" type="MyNamespace.Service1, MyAssembly" singleton="false">
    <property name="Service2" ref="Service2" />
  </object>

</objects>


这里有更多文档:

Hi,我只想使用spring.net。我是sprin.net的新手。如果您有任何使用spring.net的解决方案,请告诉我。@bbaia您好,我已经尝试了上述解决方案,但仍然收到空引用异常。在Service1.Show()中,service2实例为null。我所做的是从应用程序上下文中获取Service1实例,然后在调用Service1.Show()时,发现时间service2为null。这里Service1也是一个代理。这就像在另一个代理中注入代理一样。@bbaia您好,我已经尝试了上述解决方案,但仍然收到空引用异常。在Service1.Show()中,service2实例为null。我所做的是从应用程序上下文中获取Service1实例,然后在调用Service1.Show()时,发现时间service2为null。这里Service1也是一个代理。它就像在另一个代理中插入代理一样。请共享您的配置示例。@bbai:hi,我已经共享了我的配置文件。请检查一下。您如何托管您的服务?使用.svc文件或使用SpringServiceHost?@bbai:im主机,如下所示:ServiceHost主机=新ServiceHost(typeof(MyNamespace.Service1));host.AddServiceEndpoint(typeof(MyNamespace.IService1),new BasicHttpBinding(),“”;host.Open();
<sectionGroup name="spring">
  <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
  <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
  <object id="Service1Obj" singleton="false"
               type="MyNamespace.Service1, MyAssembly">
    <!-- Setter injection-->
    <property name="Service2" ref="Service2"/>
  </object>

  <object id="Service2Obj" singleton="false"
         type="MyNamespace.Service2, MyAssembly">
  </object>

  <!-- Creating Client side proxy dynamically-->
  <object id="Service1"
    type="MyNamespace.IService1, MyAssembly"
    factory-object="Service1ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service1ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService1>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service1Endpoint" />
  </object>

  <object id="Service2"
    type="MyNamespace.IService2, MyAssembly"
    factory-object="Service2ChannelFactory"
    factory-method="CreateChannel" />

  <object id="Service2ChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;MyNamespace.IService2>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="Service2Endpoint" />
  </object>
</objects>
  <service name="Service2Obj" behaviorConfiguration="DefaultBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8001"/>
      </baseAddresses>
    </host>
    <endpoint address=""
              binding="basicHttpBinding"
              contract="MyNamespace.IService2" />
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<client>
  <!--<endpoint name="serverWebCalculatorEndpoint" address="http://localhost:2637/Spring.WcfQuickStart.ServerWeb/Calculator.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="Spring.WcfQuickStart.ICalculator"/>-->
  <endpoint name="Service1Endpoint" address="http://localhost:8000" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService1"/>
  <endpoint name="Service2Endpoint" address="http://localhost:8001" binding="basicHttpBinding" bindingConfiguration="" contract="MyNamespace.IService2"/>
</client>