[英]Passing Interface in a WCF Service?
I'm experimenting with WCF Services, and have come across a problem with passing Interfaces. 我正在尝试使用WCF服务,并且遇到了传递接口的问题。
This works: 这有效:
[ServiceContract]
public interface IHomeService
{
[OperationContract]
string GetString();
}
but this doesn't: 但这不是:
[ServiceContract]
public interface IHomeService
{
[OperationContract]
IDevice GetInterface();
}
When I try to compile the client it fails on the GetInterface method. 当我尝试编译客户端时,它在GetInterface方法上失败。 I get an Exception saying that it can't convert Object to IDevice. 我得到一个异常,说它无法将Object转换为IDevice。
On the clientside the IHomeService class correctly implements GetString with a string as it's returntype, but the GetInterface has a returntype of object. 在客户端上,IHomeService类正确地使用字符串实现GetString,因为它是返回类型,但GetInterface具有返回类型的对象。 Why isn't it IDevice? 为什么不是IDevice?
您需要告诉WCF序列化程序使用哪个类来序列化接口
[ServiceKnownType(typeof(ConcreteDeviceType)]
Thanks, it works when I changed it like this: 谢谢,当我改变它时,它的工作原理如下:
[ServiceContract]
[ServiceKnownType(typeof(PhotoCamera))]
[ServiceKnownType(typeof(TemperatureSensor))]
[ServiceKnownType(typeof(DeviceBase))]
public interface IHomeService
{
[OperationContract]
IDevice GetInterface();
}
I also got help from this site: http://www.thoughtshapes.com/WCF/UsingInterfacesAsParameters.htm 我也从这个网站得到了帮助: http : //www.thoughtshapes.com/WCF/UsingInterfacesAsParameters.htm
I initially tried to pass an interface to a WCF method but couldn't get the code to work using the answers provided on this thread. 我最初尝试将接口传递给WCF方法,但无法使用此线程提供的答案使代码工作。 In the end I refactored my code and passed an abstract class over to the method rather than an interface. 最后,我重构了我的代码并将抽象类传递给方法而不是接口。 I got this to work by using the KnownType attribute on the base class eg 我通过在基类上使用KnownType属性来实现这一点,例如
[DataContract]
[KnownType(typeof(LoadTypeData))]
[KnownType(typeof(PlanReviewStatusData))]
public abstract class RefEntityData : EntityData, IRefEntityData
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.