服务合同实现另一个接口 [英] Service Contract Implements another Interface

查看:105
本文介绍了服务合同实现另一个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我这是否可行。
我在C#中有一个客户端win表单应用程序和一个wcf应用程序。这是我的模型。

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

public interface IServiceA
{
     string DoWorkA();
}

我在Common项目中没有使用ServiceContract或OperationContract属性。

I am not using ServiceContract or OperationContract attributes here in Common project.

现在,ClientProject引用了Common Project。
ServiceProject还引用了Common Project。

Now, ClientProject references the Common Project. ServiceProject also references the Common Project.

在ServiceProject中,我使用的服务合同如下所示:

In ServiceProject, I am using a Service Contract as shown below:

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


public class MyService : IGetData
{
    string DoWorkA()
     {

     }
}



在客户端



In the Client side

public class MyClass : IServiceA
{
    // Implements the IServiceA method
    string DoWorkA()
     {
        // Inside this I will call the MyService using DuplexChannel proxy
     }
}

[请假设在此模型中实现了回调合约]

[Please assume that a callback contract is implemented in this model]

为什么我问这个问题,在我的应用程序中,我有很多模块,每个都需要得到来自服务的数据用他们自己的方法。所以我打算使用类似外观的图案。请告诉我这是否正确????

Why I asked this question is that, in my application, I have a lot of modules, each needs to get data from service with their own method. So I am planning to use a facade like pattern. Please tell me if this correct or not ????

推荐答案

您编写的代码会生成警告,因为 IGetData DoWork()方法隐藏 IServiceA DoWork 方法。

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).

例如:

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

我们实施了类似的东西。

We implemented something similar.


  • 我们在共享程序集中定义了所有服务合同,并使用了客户端和服务器上的接口。

  • 我们创建了实现这些接口的客户端代理。

然后客户端可以使用更简单的子集与客户端代理进行交互整个服务接口。

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

首先定义接口:

[ServiceContract]
public interface IServiceA
{
    [OperationContract]
    string DoWorkA(); 
}

[ServiceContract]
public interface IServiceB
{
    [OperationContract]
    string DoWorkB();
}

[ServiceContract]
public interface IGetData : IServiceA, IServiceB
{
}

然后创建代理(你也可以在这里使用 ChannelFactory ):

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();
    }
}

上述内容与您的MyClass类似。

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.

这篇关于服务合同实现另一个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆