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

发布于 2024-11-16 04:11:51 字数 3751 浏览 2 评论 0原文

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

How do I Inject the proxy of service2 inside the Service1 proxy by saving it in App.config file in Spring.Net?

应用程序配置文件 @bbai 请找到下面的 app.config 文件。 ServiceHost 我在我的 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>

I'm new to Spring.Net.
I have one wcf service class which needs the proxy of another service like:

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

How do I inject the proxy of service2 inside the Service1 proxy by configuring it in App.config file in Spring.Net?

App.config file
@bbai Please find the below app.config file. ServiceHost im creating inside my Program.cs file.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天的雪花 2024-11-23 04:11:51

像这样:

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

更多文档在这里: http://www.springframework.net/ doc-latest/reference/html/wcf.html

Like this :

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

More documentation here : http://www.springframework.net/doc-latest/reference/html/wcf.html

不乱于心 2024-11-23 04:11:51

在 WCF 中使用 IoC 并不简单,但在这种情况下,您可以在创建服务时使用 IInstanceProvider 解析对 IService2 的引用。该帖子位于 http://blogs。 msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspx 有一个示例使用该接口为 WCF 服务实现一个简单的 IoC 容器。

Using IoC in WCF isn't straightforward, but in this case you can use an IInstanceProvider to resolve the reference to IService2 when the service is being created. The post at http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspx has an example of using that interface to implement a simple IoC container for a WCF service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文