Service Contract Implements another Interface

前端 未结 2 995
醉酒成梦
醉酒成梦 2021-01-25 07:09

Please tell me if this is possible. I have a client win form app and a wcf app in C#. This is my model.

Common Project

public interface IServiceA
{
            


        
相关标签:
2条回答
  • 2021-01-25 07:20

    Your code as written generates a warning because the IGetData DoWork() method hides the IServiceA DoWork method.

    To get rid of the warning you need to add the new keyword:

    [ServiceContract]
    public interface IGetData : IServiceA
    {
       // Implements IServiceA method
       [OperationContract]
       new string DoWorkA();
    }
    

    But I think what you want to do is to aggregate smaller interfaces into one larger service. Then your modules can interact with their easy to understand interface (which is a subset of the service interface).

    For example:

    public interface IWarehouse : IShipping, IReceiving, IInventory, IMovement {}
    

    We implemented something similar.

    • We defined all of our service contracts in a shared assembly and used the interfaces on both the client and the server.
    • We created client proxies that implemented those interfaces.

    The clients could then interact with client proxies with a simpler subset of the entire service interface.

    First define the interfaces:

    [ServiceContract]
    public interface IServiceA
    {
        [OperationContract]
        string DoWorkA(); 
    }
    
    [ServiceContract]
    public interface IServiceB
    {
        [OperationContract]
        string DoWorkB();
    }
    
    [ServiceContract]
    public interface IGetData : IServiceA, IServiceB
    {
    }
    

    Then create the proxies (you could also use ChannelFactory here):

    public class ServiceClientA : ClientBase<IGetData>, IServiceA
    {
        public string DoWorkA()
        {
            return Channel.DoWorkA();
        }
    }
    
    public class ServiceClientB : ClientBase<IGetData>, IServiceB
    {
        public string DoWorkB()
        {
            return Channel.DoWorkB();
        }
    }
    

    The above would be analagous to your MyClass.

    Then the client can use the individual interface:

    IServiceA clientA = new ServiceClientA();
    string result = clientA.DoWorkA();
    
    IServiceB clientB = new ServiceClientB();
    result = clientB.DoWorkB();
    

    You could also create a service factory here if you wanted.

    I hope that gives you some ideas.

    0 讨论(0)
  • 2021-01-25 07:28

    Couple of points:

    • Your client does not need to implement IServiceA. Why is that? Your client can consume the service by just having reference to your WCF service.
    • Your interface IServiceA seems to be redundant in a given scenario. It is not bringing any benefit with it. IMO you can live IGetData. This is where you are implementing the service contract. If you wish you can rename this interface as IServiceA.

    To answer your point where you have lots of module which need to get data from service, can you categorize these modules in business/function nature? If yes, then you can create different WCF service for each of those business/function and respective modules will call respective service. Further to this I don't see a point why one module should not be allowed to gather data from more than one service. That is perfectly acceptable scenario.

    HTH

    0 讨论(0)
提交回复
热议问题