本文介绍了在 WCF 服务中传递接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在试验 WCF 服务,但遇到了传递接口的问题.
I'm experimenting with WCF Services, and have come across a problem with passing Interfaces.
这有效:
[ServiceContract]
public interface IHomeService
{
[OperationContract]
string GetString();
}
但这不会:
[ServiceContract]
public interface IHomeService
{
[OperationContract]
IDevice GetInterface();
}
当我尝试编译客户端时,它在 GetInterface 方法上失败.我收到一个异常,说它无法将 Object 转换为 IDevice.
When I try to compile the client it fails on the GetInterface method. I get an Exception saying that it can't convert Object to IDevice.
在客户端,IHomeService 类使用字符串正确实现 GetString 作为它的返回类型,但 GetInterface 具有对象的返回类型.为什么不是 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. Why isn't it IDevice?
推荐答案
需要告诉WCF序列化器使用哪个类来序列化接口
You need to tell the WCF serializer which class to use to serialize the interface
[ServiceKnownType(typeof(ConcreteDeviceType)]
这篇关于在 WCF 服务中传递接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!