Linux VS2010 中跨多个 Web 服务的通用 DTO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3677124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Common DTOs across multiple web services in VS2010
提问by Oriacle
I have a collection of WCF web services that share a common DTO model.
我有一组共享通用 DTO 模型的 WCF Web 服务。
The problem is that when I add service references in Visual Studio 2010, the proxy generated for each of the web services has its own namespace (internal to the solution), meaning that I essentially have duplicate DTO's on each of the service references.
问题是,当我在 Visual Studio 2010 中添加服务引用时,为每个 Web 服务生成的代理都有自己的命名空间(在解决方案内部),这意味着我基本上在每个服务引用上都有重复的 DTO。
Is there a clean way to get over this and to share the dto, short of creating a facade over the whole group of services?
有没有一种干净的方法来克服这个问题并共享 dto,而不是在整个服务组上创建一个外观?
Cheers.
干杯。
采纳答案by Dr. Wily's Apprentice
Instead of creating your client proxies via the Visual Studio "Add Service Reference" functionality, you could generate them using Visual Studio's command line tool svcutil.exe. Pass in the URL to the metadata for each service and svcutil should figure out what types they have in common.
您可以使用 Visual Studio 的命令行工具 svcutil.exe 生成它们,而不是通过 Visual Studio 的“添加服务引用”功能创建客户端代理。将 URL 传递给每个服务的元数据,svcutil 应该找出它们的共同类型。
.Net Consuming Web Service: Identical types in two different services
I'm not sure if there is an easier / better way.
我不确定是否有更简单/更好的方法。
回答by pdiddy
You should have the DTO in a seperate library. Add a reference to that library in your client project. And you can configure the proxy generation to used object from referenced library. Right click on your service reference and select configure Service reference there should be an option for Reuse types in referenced assemblies.
您应该在单独的库中拥有 DTO。在您的客户端项目中添加对该库的引用。您可以将代理生成配置为引用库中使用的对象。右键单击您的服务引用并选择配置服务引用,应该有一个用于在引用的程序集中重用类型的选项。