WCF和接口继承 - 这是一件可怕的事情吗? [英] WCF and Interface Inheritance - Is this a terrible thing to do?

查看:136
本文介绍了WCF和接口继承 - 这是一件可怕的事情吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有2个服务,假设一个是一个基本的(整数)计算器,一个是一个浮点计算器。我将其表示为如下所示的界面:

  public interface IBasicCalculator 
{
int Add(int a, int b);
}

public interface IFloatingPointCalculator
{
double Add(double a,double b);
}

我想通过WCF公开这些。不幸的是,WCF似乎与你想公开的每一个可能的操作都必须经过一个单一的服务接口 - 你不能在服务之间共享会话这个概念是非常紧密的,从客户端来说,这是很麻烦的,因为你需要创建一个单独的代理,每个人,似乎没有任何子服务等...



所以,我收集了我需要呈现一个组合界面(一个也可以称之为立面),如下所示:

  [ServiceContract] 
public接口ICalculatorService:IBasicCalculator,IFloatingPointCalculator
{
[OperationContract(Name =AddInt)]
new int Add(int a,int b);

[OperationContract(Name =AddDouble)]
new double Add(double a,double b);
}

如果我这样做,那么WCF会将两种方法公开给客户端,打电话给他们,这一切实际上都是有效的。



然而,像这样的继承接口似乎是无礼的。特别 int添加 new double添加。严格来说,方法上的 new 表示隐藏了一个底层方法,我根本没有做。我可以省略新的,但是我只是得到编译器警告,这相当于我想我隐藏这种方法,你需要重命名它或者放入'新'



所以这是一个两部分的问题:


  1. 我正在跟踪我的将所有内容整合成一个界面的逻辑,还是实际上有一种使用WCF公开子服务或多个链接服务的方法?


  2. 如果这是需要做的,是否有更好的方法?


谢谢!

解决方案

我刚刚发现可以将多个端点(每个使用不同的接口)暴露给同一个服务,而您仍然只需要在客户端上生成一个代理服务器,可以访问所有的代理服务器,从而彻底解决了我的问题。


My application has 2 "services", let's say one is a basic (integer) calculator, and one is a floating point calculator. I express these as interfaces like so:

public interface IBasicCalculator
{
    int Add( int a, int b );
}

public interface IFloatingPointCalculator
{
    double Add( double a, double b );
}

I want to expose these via WCF. Unfortunately WCF seems to be very tightly tied to the notion that every possible operation you want to expose must go through one single service interface -- you can't share sessions between services, it is cumbersome from the client side as you need to create a seperate proxy for each one, there don't seem to be any "sub-services", etc...

So, I've gathered that I need to present a "combined" interface (one might also call it a facade), like this:

[ServiceContract]
public interface ICalculatorService : IBasicCalculator, IFloatingPointCalculator
{
    [OperationContract(Name = "AddInt")]
    new int Add( int a, int b );

    [OperationContract(Name = "AddDouble")]
    new double Add( double a, double b );
}

If I do this, then WCF exposes both methods to the client, which can call them, and it all actually works.

However, "inheriting the interfaces" like that seems to be ungainly. Particularly the new int Add and new double Add. Strictly speaking, new on a method indicates hiding an underlying method, which I'm not actually doing at all. I can omit the new, but then I just get compiler warnings which amount to "I think I'm hiding this method, you need to rename it method or put 'new' on it".

So, this is a 2-part question:

  1. Am I on track with my 'combine everything into one interface' logic, or is there actually a way to expose "sub-services" or "multiple linked services" using WCF?

  2. If this is what needs to be done, is there a better way?

Thanks!

解决方案

I just worked out that you can expose multiple endpoints (each using a different interface) to the same service, and you still only need generate ONE proxy lib on the client which gives access to all of them, which solves my problem entirely.

这篇关于WCF和接口继承 - 这是一件可怕的事情吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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