wcf Communication between two Services

Each ServiceHost hosts a single service. You can have as many active ServiceHosts in a single AppDomain as you like. Each service can also have as many endpoints as you like, where each endpoint is defined by a unique address-binding-contract combination. The decision about how you organize your deployment potentially depends on how independant you want your services to be, how related they are, their hosting requirements, etc.

A programmatic way of running multiple services in a single AppDomain is a simple matter of instantiating, configuring, and starting multiple ServiceHosts, each with its own set of endpoints. In config, it is a matter of multiple <service> elements inside a <services> element, like so:

<services>
      <service name="MyNamespace.MyService1">
        <endpoint address="address1"
              contract="MyNamespace.MyContract1"
              binding="wsHttpBinding"
              bindingConfiguration="MyHttpBinding1" />
        <endpoint address="address2"
              contract="MyNamespace.MyContract1"
              binding="customBinding"
              bindingConfiguration="MyCustomBinding1" />
        <endpoint address="address3"
              contract="MyNamespace.MyContract2"
              binding="wsDualHttpBinding"
              bindingConfiguration="MyDualHttpBinding" />
      </service>
      <service name="MyNamespace.MyService2">
        <endpoint address="address4"
        ....
      </service>
</services>

To establish communication between these two services, you'll create a ChannelFactory<T> and a channel in the service you want to act as a client, just like you would in a regular client application. Your client service would have to be triggered by your program to do this and establish a connection with the other service.

posted @ 2007-12-25 13:41  bluealarm  阅读(165)  评论(0)    收藏  举报