为什么 WCF 在作为 Web 服务托管时不能“正确"使用/公开抽象类型 [英] WHY doesn't WCF 'properly' consume/expose abstract types when hosted as a web service

查看:23
本文介绍了为什么 WCF 在作为 Web 服务托管时不能“正确"使用/公开抽象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计 Web 服务已经有一段时间了,但直到最近才需要公开复杂的"WCF 服务.我对 WCF 中明显缺乏对抽象类型的适当支持"感到困惑.当然 - 你可以使用它们 - 确保你可以让它们工作"......你只是没有得到你想要的......

I've been designing web services for quite a while now but never had to expose a 'complicated' WCF service until recently. I was baffled at the apparent lack of "proper support" in WCF for abstract types. Sure - you can USE them - sure you can get them to 'work'... you just don't end up with what you WANT...

第一个问题是,如果您从具有抽象类型的 wsdl 生成代码,您会得到截然不同的代码,因为它回退到 xmlserializer 而不是 DataContractSerializer.这显然有点不太理想......我想使用花哨的新的更快的序列化器,谢谢......(以及服务/数据合同附带的所有内容)

The first problem is that if you generate code from a wsdl with an abstract type you get vastly different code because it falls back to the xmlserializer and not the DataContractSerializer. This is obviously a bit less than desirable... I'd like to use the fancy new faster serializer please thank you... (and all that comes along with Service/DataContract)

另一方面 - 如果您首先从代码开始并将正确属性的抽象 wcf 类公开为 Web 服务,则公开的 wsdl 不包含使抽象类"在技术上具体化的 abstract="true" 属性...这当然不是我想要的...

on the flip side - if you start with code first and expose a properly attributed abstract wcf class as a web service the exposed wsdl does NOT contain the abstract="true" attribute making the "abstract class" technically concrete... This is not what I want of course...

我有一个解决方法,但它涉及到大量的hackery",我首先创建 wsdl/xsd 合约,删除任何 abstract="true"(哦 - 更不用说我不能在xsd 应该我们)然后 svcuitl 结果......但现在我留下了具有 CONCRETE 抽象类的 ac# api,然后我需要修改它以添加抽象关键字......这个有效"但是这是一个巨大的皮塔饼——而且不容易编写脚本"......

I've got a workaround but it involves a crazy amount of 'hackery' where I create the wsdl/xsd contract first, remove any abstract="true" (oh - let's not mention that I can't use attributes in the xsd shall we) and then svcuitl the result... But now I'm left with a c# api that has a CONCRETE abstract class and I then need to go modify that to ADD the abstract keyword... This 'works' but it's a huge pita - and not easily 'scriptable'...

这一切都被打击了!我希望有人能准确地向我解释为什么"这是......我欢迎没有引用可靠"资源的答案,但我真的在等待这个人告诉我 - 有适当的文件(比如最好来自好的 ol Don Box 本人)这到底是为什么...因为我就是不明白...

This is all just whacked! I'm hoping someone can explain to me precisely 'why' this is... I welcome answers that don't cite "solid" resources but I'm really waiting for the person to tell me - with proper documentation (like preferably from good ol Don Box himself) why exactly this is... Cause I just don't get it...

谢谢大家 - 如果有人想了解更多详情 - 请告诉我!

Thanks all - if anyone would like more details - please let me know!

已更新以添加示例请求 - 从 c# 开始

UPDATED FOR ADDITION OF A SAMPLE REQUEST - starting with c#

[ServiceContract]
public interface IShapeTest
{
  [OperationContract]
  AbsShape EchoShape(AbsShape shape);
}

public class ShapeTestImpl : IShapeTest
{
  public AbsShape EchoShape(AbsShape shape)
  {
    return shape;
  }
}

[KnownType(typeof(Square))]
public abstract class AbsShape
{
  [DataMember]
  public int numSides;
}

public class Square : AbsShape
{
  public Square() : base()
  {
    numSides = 4;//set the numSides to 'prove' it works
  }
}

预期类型:

<xs:complexType name="AbsShape" abstract="true"> <!--NOTE abstract="true"-->
  <xs:sequence>
    <xs:element minOccurs="0" name="numSides" type="xs:int"/>
  </xs:sequence>
</xs:complexType>

实际发射类型:

<xs:complexType name="AbsShape"> <!--NOTE the lack of abstract="true"-->
  <xs:sequence>
    <xs:element minOccurs="0" name="numSides" type="xs:int"/>
  </xs:sequence>
</xs:complexType>

推荐答案

这是因为 WCF 不传递对象,它传递消息.这不是远程处理,因此最终在客户端上的类型与您在服务器上的类型不同 - 它只是各种属性的持有类.实现 "abstract="true" 根本没有意义.消息只是数据 - 客户端如何知道要使用什么具体类型,因为您没有共享类,而只是消息的表示.

Well it's because WCF doesn't pass objects, it passes messages. This isn't remoting, so the type end up with on the client is not the same type you have on the server - it's simply a holding class for various properties. Implementing "abstract="true" simply makes no sense. Messages are just data - how would the client know what concrete type to use, as you're not sharing classes, but simply a representation of the message.

这篇关于为什么 WCF 在作为 Web 服务托管时不能“正确"使用/公开抽象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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