具有不同合同但业务对象相同的两个 WCF 服务

编程入门 行业动态 更新时间:2024-10-07 04:22:52
本文介绍了具有不同合同但业务对象相同的两个 WCF 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

例如,我在 IIS 中托管了两个服务.

For example I have two services hosted in IIS.

[ServiceContract]
public interface IDeviceService
{
    [OperationContract]
    DeviceCollection GetAllDevices(Customer customer);

}

[ServiceContract]
public interface IUserService
{
    [OperationContract]
    User Authenticate(string username, string password);

}

UserService 中 Authenticate 操作返回的 User 对象和 DeviceService 中 GetAllDevices 操作返回的 DeviceCollection 都具有 Customer 的子对象定义.Customer 是一个业务对象,与 User 和 Device 对象在同一个程序集中.

Both the User object that is returned from the Authenticate operation in the UserService and the DeviceCollection that is returned from the GetAllDevices operation in the DeviceService have a child object definition of Customer. Customer is a business object is in the same assembly as the User and Device objects.

我的问题出在客户端 - 当我调用设备操作时

My problem is on the client - when I call the device operation

userProxy.GetAllDevices(user.Customer);

编译器抱怨以下消息:

参数 1 - 无法从 UserService.Customer 转换为 DeviceService.Customer

Argument 1 - Cannot convert from UserService.Customer to DeviceService.Customer

我可以很好地连接到两个服务,问题是 Customer 的对象定义.我真的不想将操作放在同一个服务中,因为它们似乎自然地存在于自己的服务中.我想我想问的是其他程序员如何处理这样的问题?

I can connect to both services fine, it's the object definition of Customer that is the problem. I don't really want to put the Operations in the same service as they seem to live naturally in their own services. I guess what I'm asking is that how to other programmers deal with such a problem?

干杯,斯图尔特

推荐答案

如果您想在多个服务之间共享一个数据契约,那么您必须将有问题的数据契约编译成它自己的程序集,然后将该程序集分发给客户.

If you want to share a data contract across multiple services then you will have to compile the data contract in question into its own assembly and then distribute that assembly to the client.

即使类型看起来是相同的,但它实际上是两个独立的类型,这就是为什么您会看到您所看到的错误.您唯一的其他选择(除了单独的共享程序集)是将这两个服务合二为一,以便它们可以共享数据契约.

Even though the type appears to be the same, it is in fact a two separate types and that is why you are seeing the error that you are seeing. Your only other choice (other than a separate, shared assembly) is to combine the two services into one so that they can share the data contract.

这篇关于具有不同合同但业务对象相同的两个 WCF 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 20:19:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   合同   两个   业务   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!