0

I am really confused about one scenario in WCF.

I have on WCF service " SERVICE1" which exposes two operations "OP1" and "OP2".

There are two clients consuming the service "CLIENT1" AND "CLIENT2".

The condition is "CLIENT1" can only invoke "OP1" while "CLIENT2" is restricted to "OP2".

halfer
  • 19,824
  • 17
  • 99
  • 186
Gopal
  • 57
  • 6

1 Answers1

1

Refactor your service contracts to exposes two endpoints, the interface of which is only relevant to the client that is consuming it:

   [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        void OperationOne();
    }

    [ServiceContract]
    public interface IService2
    {
        [OperationContract]
        void OperationTwo();
    }

    public class MyServcie: IService1, IService2
    {
        //Implementation here...
    }

This way the client of IService1 doesn't even know that the methods on IService2 exist.

This is not restricted to WCF - this is good practice in OOP too....

Lawrence
  • 3,287
  • 19
  • 32
  • thanks a lot...but do we have any other way ?Because one end point will be exposed to both the client..so contract also will be the same..we can not expose both the contracts through the same end point... – Gopal Aug 29 '13 at 09:02
  • Firstly - why do you want to expose operations to a client that it cannot use? This seems to be asking for trouble. If you really want to do this, you could put authorisation logic in each the implementation of these operations to ensure that only authorised clients can use them. (http://msdn.microsoft.com/en-us/library/ff647503.aspx) – Lawrence Aug 29 '13 at 09:08
  • thanks a lot...but suppose we have one contract with 6 operation for client1 and tomorrow client2 comes and says he needs only 4 operation out of 10 existing.. should i go and create a separate contract for him..means is it necessary to have separate contract for each n every client ? is there any way to reuse the existing contracts ? – Gopal Aug 30 '13 at 04:49
  • These are very good questions and highlight the need to think very carefully about the public contracts you expose, a discussion of which is too great to have here. Importantly, contracts should not be at the mercy of the needs of your clients, but should reflect the functionality of the service. Moreover, "needing" is not the same as "forbidding". If client 2 only needs 4 operations then you do not need to change anything - if your service forbids it then that is another matter. This is a massive subject - sorry i can't go in to more detail! – Lawrence Aug 30 '13 at 08:57