为什么我的wcf客户端中的操作契约不能将接口作为参数?
我有一个简单的 wcf 服务契约,如果我使用接口的具体实现,它可以正常工作,但如果我想使用参数中的接口而不是具体类,它会给出错误,我将在下面显示。
这是代码:
[ServiceContract]
public interface IClientUserRegistration
{
[OperationContract]
void RegisterClientUser(ClientUser clientUser);
[OperationContract]
List<ClientUser> GetUsers();
}
如果我用 IClientUser 替换 ClientUser,WCF 测试客户端会说 RegisterClientUser 不支持操作,因为它使用 System.Object 类型。如果我将 GetUsers 的返回值替换为 List,则表示不支持此操作,因为它使用 System.Object[] 类型。为什么会出现这些错误?
我尝试使用 IClientUser 的原因是我可以实现不同的用户类型来实现 IClientUser 接口并将它们传递给 RegisterClient,但如果我只能传递 ClientUser,那么我必须创建一堆 RegisterClient 重写不同类型的用户。
I have a simple wcf Service Contract that works fine if I use the concrete implementations for the interfaces, but if I want to use the interfaces in the paramaters instead of the concrete classes, it gives me the error which I will display below.
Here is code:
[ServiceContract]
public interface IClientUserRegistration
{
[OperationContract]
void RegisterClientUser(ClientUser clientUser);
[OperationContract]
List<ClientUser> GetUsers();
}
If I replace ClientUser with IClientUser, the WCF Test Client says that RegisterClientUser
operation is not supported because it uses the type System.Object. If I replace the return value of GetUsers with List, it says that this operation is not supported because it uses the type System.Object[]. Why does it give these errors?
The reason why I was trying to use IClientUser is that I could implement different user types that implement the IClientUser interface and pass them into RegisterClient, but if I am only able to pass ClientUser, then I have to create a bunch of RegisterClient overrides that take different types of Users.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SOAP 没有接口的概念。这往往会使反序列化变得困难。
SOAP has no concept of interfaces. That would tend to make deserialization difficult.
这是因为对象必须被序列化才能在客户端和服务器之间传递。不允许您传递“对象”类型。所有类型都必须是具体的,以便可以正确地序列化和反序列化。接口只不过是带有接口子类型的“对象”类型。对象作为一个整体不能以这种方式反序列化和序列化,只有接口成员可以。这将使双方的实施都变得非常混乱。
This is because the objects have to be serialized for passing between the client and the server. You are not allowed to pass an "object" type. All types must be concrete so that they can be properly serialized and deserialized. An Interface is nothing more than an "object" type with a sub-type of the Interface. The object as a whole cannot be deserialized and serialized this way, only the interface members can be. This would make for a really messy implementation on both sides.