chamara
chamara

Reputation: 12709

Restrict WCF client from accessing certain public classes and methods

I have the following in my project.

WarehouseAPI.Core.dll // contains all data access code.

WCF service // references the WarehouseAPI.Core.dll library.

when the wcf client consumes the service it gets access to all the public methods and classes in WarehouseAPI.Core.dll but I just want to grant access only to certain classes and methods only.

How can I achieve this?

Upvotes: 0

Views: 164

Answers (2)

marc_s
marc_s

Reputation: 755157

When you define the contract for a WCF service (the IService1 style interface), then any client connecting to that service will see all methods defined in that service contract:

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

    [OperationContract]
    public void DoSomethingElse();

    [OperationContract]
    public void DoSomethingTotallyDifferent();
}

That's by design, and it's a good thing.

So if your IService1 interface exposes too much, then you need to break it up into a smaller "core" and the larger "extended" version (---> adhering to the Interface Segregating Principle of object-oriented design).

Then you can expose these two services separately, and allow certain clients to only connect to the "core" service:

[ServiceContract]
public interface IService1Core
{ 
    [OperationContract]
    public void DoSomething();
}

[ServiceContract]
public interface IService1Extended : IService1Core
{ 
    [OperationContract]
    public void DoSomethingElse();

    [OperationContract]
    public void DoSomethingTotallyDifferent();
}

Since IService1Extended descends from IService1Core, in the end, that "extended" service will still offer all the functionality of the original service. But now you can have an endpoint that only exposes the IService1Core contract and thus limit clients connecting to that endpoint to call just DoSomething() - and no other methods (since on that service contract, no other methods are defined)

Upvotes: 0

Ken
Ken

Reputation: 1985

This answer is very generic, but... Create WarehouseAPI_WCF.Core.dll which includes only the public methods that you want to be shown, and they call WarehouseAPI.Core.dll. Point your WCF service to WarehouseAPI_WCF.Core.dll

Depending how your class is structured you can use inheritance and create private methods to hide the public methods.

Upvotes: 1

Related Questions