问题描述
限时送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:关键词]
发布评论