我有多个共享一些数据协定的 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
但是共享类型的定义(例如请求和响应)将在 Foo.cs 和 Bar.cs 中重复,显然会导致编译器错误。
那么,生成使用此类服务的客户端代码的常规方法是什么?
限制:
- 无法将包含共享类型的程序集发送到客户端(以便他们可以使用 svcutil.exe 的 /r 选项)
- 无法在 Visual Studio 中使用“添加服务引用...”命令 - 需要 svcutil 命令行(或其他命令行工具)。