Bogdan Khimych
Bogdan Khimych

Reputation: 11

Use multiple contracts but single proxy

I've created few contracts:

[ServiceContract]
public interface IInterface1
{
    [OperationContract]
    void Method1();
}

and

[ServiceContract]
public interface IInterface2
{
    [OperationContract]
    void Method2();
}

Created class Implementation.cs which inherits them both and implements their interfaces. Also I created few endpoints for every contract, so different operations worked on different protocols:

   <service name="Implementation">
    <endpoint binding="netNamedPipeBinding" contract="IInterface1" />
    <endpoint binding="netTcpBinding" contract="IInterface2" />

I want clients, who will discover my service, to use only one proxy instead of two. Is it possible for them to export single proxy somehow, which will expose both contracts?

Upvotes: 1

Views: 265

Answers (1)

ZZZ
ZZZ

Reputation: 2812

As Tim had said the ABC of endpoint -- Address, Binding and Contract.

You can have only 1 proxy per class, this is by concept, by design and by implementation of WCF. Otherwise, the client programmers (users) will be confused, and might scratch their heads.

Technically you can have multiple contracts with single proxy, however, you will have to build everything from scratch at the client side, and not to use WCF.

When using WCF at client side, either generating proxy classes through svcutil.exe or handcrafting, you will use System.ServiceModel.ClientBase as the base class of the proxy class. Since ClientBase takes only 1 contract, so you have one proxy class per contract.

For one contract, you may publish multiple endpoints.

In your config above, you have declared 2 endpoints with 2 contracts in different bindings. The client will see 2 contracts then generate 2 proxy classes.

If you have the following config

<service name="Implementation">
    <endpoint binding="netNamedPipeBinding" contract="IInterface1" />
    <endpoint binding="netTcpBinding" contract="IInterface2" />
    <endpoint binding="simpleHttpBinding" contract="IInterface1" />
    <endpoint binding="simpleHttpBinding" contract="IInterface2" />
</service>

the client will still generate 2 proxy classes, not 4, not 1. Since clients only care about the contracts, NOT the implementation.

proxy classes have no knowledge of bindings. Which binding to use is determined in your client codes or client config.

contracts, implementation, endpoints, binding, address and client proxy are separated concerns of different parties, so don't mix up them.

Upvotes: 3

Related Questions