【发布时间】:2011-01-09 14:48:22
【问题描述】:
我有多个共享一些数据协定的 WCF 服务,并且需要使用 svcutil.exe 生成客户端代码。我在使用两种最明显的方法时遇到了错误,需要一些帮助。
但首先,这里是服务:
[ServiceContract( Namespace = "http://www.me.com/services/" )]
public interface IFooService {
[OperationContract]
Response RunFoo( Request request );
}
[ServiceContract( Namespace = "http://www.me.com/services/" )]
public interface IBarService {
[OperationContract]
Response RunBar( Request request );
}
Response 和 Request 在单独的程序集中定义:
[DataContract( Namespace = "http://www.me.com/shared/" )]
public class Request {
[DataMember]
public int Input { get; set; }
}
[DataContract( Namespace = "http://www.me.com/shared/" )]
public class Response {
[DataMember]
public int Result { get; set; }
}
服务以某种简单的方式实现、编译、发布 - 现在让我们切换到客户端。
在 svcutil 命令行中包含这两个服务 - 像这样:
svcutil /o:Client.cs http://hostname.com/FooService.svc http://hostname.com/BarService.svc
将导致大量关于重复数据类型的错误消息,以
开头错误:导出期间生成的架构出现验证错误: 资源: 行:1 列:9087 验证错误:已声明全局元素“http://schemas.microsoft.com/2003/10/Serialization/:anyType”。
以
结尾错误:导出期间生成的架构出现验证错误: 资源: 行:1 列:12817 验证错误:complexType 'http://www.me.com/shared/:Response' 已被声明。
为每个服务单独生成一个客户端文件可以避免这些错误:
svcutil /o:Foo.cs http://hostname.com/FooService.svc
svcutil /o:Bar.cs http://hostname.com/BarService.svc
但是共享类型(如Request和Response)的定义会在Foo.cs中重复,然后在Bar.cs中,显然会导致编译器错误。
那么,生成使用此类服务的客户端代码的常规方法是什么?
限制:
- 无法向客户端发送包含共享类型的程序集(以便他们可以使用 svcutil.exe 的 /r 选项)
- 无法在 Visual Studio 中使用“添加服务引用...”命令 - 需要 svcutil 命令行(或其他命令行工具)。
【问题讨论】:
标签: c# wcf web-services proxy svcutil.exe