WCF和接口的参数接口、参数、WCF

2023-09-03 07:22:31 作者:撑起一片天

我有共享同一界面的一些实体库。客户和服务共享本次大会。现在我不知道是否有办法有这个接口类型作为参数在我的服务合同,这样我可以用同样的方法实现接口的所有类。

i have a library with some entities that share the same interface. clients and service share this assembly. now i wonder if there is a way to have this Interface-type as Parameter in my service contracts so that i can use the same method for all classes implementing the interface.

自己的人都装饰有datacontract属性及其成员的DataMember属性的实体。

the entities themselve are all decorated with datacontract-attribute and its members with datamember attributes.

有没有可能呢?大概跟 NetDataContractSerializer 的? 我知道,我可以用一个基类(一些抽象类EG)和做它的 knowntype 的-attribute但我倒是肯定preFER接口为对象的识别器因为它被广泛使用在客户端应用程序,将简化开发。

is it possible at all? probably with the NetDataContractSerializer? i know that i can do it with a base class (some abstract class e.g.) and the knowntype-attribute but i´d definitely prefer the Interface as identificator of the objects cause it is used widely in the client app and would ease development.

感谢

推荐答案

我使用ServiceKnownType属性在OperationContracts的实施解决了这个问题。

I solved the problem using the ServiceKnownType attribute at the implementations of the OperationContracts.

当告诉你的类实现该接口为ServiceKnownType的,您可以使用该接口的参数,因此能够使用实现您的接口所有的类,只要他们是序列化。 (看从Juval洛伊,100页的编程WCF服务)

When telling your classes that implement the interface as ServiceKnownType's, you can use the interface as parameter and therefore are able to use all classes implementing your interface as long as they are serializable. (look at "Programming WCF Services" from Juval Löwy, page 100)