官术网_书友最值得收藏!

Sharing a physical address between multiple endpoints

WCF supports exposing a single service through multiple heterogeneous endpoints. Another great feature is letting multiple endpoints listen over the same physical transport address. For example, you want to host a WCF service that has multiple endpoints exposed. However, you only have a single HTTP URL open for listening. Then you can use this feature to make all those endpoints (as long as they’re using the same transport protocol) listen on the same URL.

How to do it...

Our sample service will expose two endpoints (one is IFoo and the other is IBar) and both of them will listen on the same HTTP URL:

  1. The first thing we need to do is configure the service endpoints. We will still specify different values for the address attribute of the two endpoints. However, we will use a special attribute named listenUri to supply the physical address (identical for both endpoints). The following screenshot shows the usage of both attributes:
    How to do it...
  2. It is also important to configure the client endpoint so that the client proxy or ChannelFactory can correctly locate the service endpoint. WCF provides a ClientViaBehavior type for specifying the physical address of a client endpoint. You can use the ChannelFactory.Endpoint.Behaviors collection to inject the behavior in code, shown as follows:
    ChannelFactory<SharedAddressService.IFoo> fooFactory = new ChannelFactory<SharedAddressService.IFoo>(new WSHttpBinding(SecurityMode.None));
    
    SharedAddressService.IFoo foo = fooFactory.CreateChannel(
     new EndpointAddress(“urn:Foo”), /* logical address */
     new Uri(“http://localhost:8731/FooBarService/Operations”)); /*physical address*/
               foo.Foo();

    Alternatively, you can also use the <clientVia> element to supply the physical address in a configuration file, as shown in the following screenshot:

    How to do it...

How it works...

Generally, we will use the address attribute to specify the URL on which the endpoint will listen. However, this address is actually a logical address and WCF runtime will use it as a physical address also, if we haven’t explicitly provided a physical address. The listenUri, on the contrary, represents the physical transport address the service endpoint listens on.

Also, for the endpoints that share the same physical address, they will share the same service dispatcher and channel stack. Then, how does the WCF runtime differentiate these operation requests when there are multiple endpoints listening on a single physical address? The answer is WCF runtime will try to resolve the request target by the combination of two parts:

  • The Service/Operation Contract
  • The logical address specified the via address attribute

Therefore, the dispatcher/channel stack can correctly redirect the requests to the corresponding endpoint even if we supply an identical value for both logical and physical address.

There’s more...

For more in-depth explanation on WCF Addressing, you can have a look at the WCF Addressing In Depth MSDN article written by Aaron Skonnard (http://msdn.microsoft.com/en-us/magazine/cc163412.aspx).

See also

  • Creating a multiple-endpoint service
主站蜘蛛池模板: 千阳县| 广西| 双辽市| 晋城| 百色市| 涞源县| 启东市| 曲松县| 诏安县| 湘乡市| 阳谷县| 麻栗坡县| 上杭县| 益阳市| 孝昌县| 平远县| 福清市| 山东省| 九寨沟县| 南皮县| 子长县| 德格县| 黑水县| 青海省| 康乐县| 罗甸县| 怀远县| 泽库县| 阿尔山市| 孟连| 广东省| 大埔县| 和顺县| 锦屏县| 双辽市| 兴山县| 英吉沙县| 陕西省| 西盟| 东莞市| 周至县|