Как ввести wcf прокси с помощью Spring .net - PullRequest
1 голос
/ 20 июня 2011

Я новичок в 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();
  }
}

Как мне внедрить прокси service2 внутри прокси Service1, настроив его в файле App.config в Spring.Net?

Файл App.config @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>

Ответы [ 2 ]

1 голос
/ 20 июня 2011

Как это:

<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

1 голос
/ 20 июня 2011

Использование IoC в WCF не является простым, но в этом случае вы можете использовать IInstanceProvider для разрешения ссылки на IService2 при создании службы. Сообщение в http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspx содержит пример использования этого интерфейса для реализации простого контейнера IoC для службы WCF.

...