“复制"WCF 代理代码中的类 [英] "Copied" classes in WCF proxy code

查看:23
本文介绍了“复制"WCF 代理代码中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 WCF 和 Visual Studio 2008 来构建大型分布式应用程序.我们在 WCF 方面没有太多经验,因此我们正在投入一些时间来设计客户端和服务器之间的 WCF 通信原型.我们已经观察到,由我们的服务器子系统中的 WCF 服务作为参数或返回类型引用的任何类都被 Visual Studio 通过生成的代码复制"到客户端代理中.我发现令人惊讶的是,在执行此操作时,Visual Studio 更改了类的命名空间.例如,假设我有以下代码:

We are using WCF and Visual Studio 2008 to build a large, distributed application. We don't have much experience with WCF so we are investing some time in prototyping the WCF communications between clients and server. We have observed that any class referenced by the WCF services in our server subsystem as a parameter or return type is "copied" into the client proxy by Visual Studio through generated code. What I found surprising was that, when doing this, Visual Studio alters the namespace of the classes. For example, imagine that I have the following code:

namespace MyLibrary
{
    class MyClass
    {
        //...
    }
}

想象一下,我将它编译成 MyLibrary.dll,我的 WCF 服务有一个引用.我的一些 WCF 服务使用 MyLibrary.MyClass 作为返回类型.当我查看生成的客户端代理代码时,我发现 MyClass 是自动可用的(这很酷),但它不再位于 MyLibrary 命名空间中,而是在一个生成的命名空间中通过 Visual Studio.

And imagine that I compile that into MyLibrary.dll, to which my WCF services have a reference. Some of my WCF services use MyLibrary.MyClass as a return type. When I look at the generated client proxy code, I find MyClass to be automagically available (which is cool), but it is not in the MyLibrary namespace anymore but in one generated by Visual Studio.

我们计划拥有一个复杂的域对象模型,其中包含分布在多个程序集中的数百个类,我担心这将如何在 Visual Studio 执行的代码生成中移植".此外,我担心我们设计的所有漂亮的命名空间层次结构都可能会丢失.我们最初的愿景是为这个领域对象模型创建类库,并在服务器和客户端上部署它.但由于 WCF 魔术似乎为必要的类生成代码,我想如果我们在客户端上部署我们自己的库会发生冲突.

We are planning to have a complex domain object model with hundreds of classes distributed across multiple assemblies, and I am concerned about how well this will "port" across the code generation that Visual Studio does. Also, I am concerned that all the nice hierarchy of namespaces that we have designed will be probably lost. Our initial vision was to create class libraries for this domain object model and delpoy it on both server and client. But since WCF magic seems to generate code for the necessary classes, I imagine that there would be a conflict if we deploy our own libraries on the client.

那么我有两个具体问题:

I have two specific questions then:

  • 在生成客户端代理代码时,有没有办法让 Visual Studio 使用原始命名空间方案而不是它自己的命名空间方案?如果没有,这会影响我们复制"的库在客户端上的可用性吗?
  • 我可以在客户端上部署原始库吗?我想不会,但这会导致什么样的冲突?

谢谢.

推荐答案

当您添加服务引用时,您可以单击高级"按钮,在那里您可以选中在引用的程序集中重用类型"框.您还需要确保您的程序集包含客户端项目中引用的原始类型.

When you add a service reference you can click the "Advanced" button, in there you can check the box that reads "Reuse types in referenced assemblies". You will also need to make sure you have the assembly that contains the original types referenced in the client project.

您当然可以在客户端上部署原始库.

You can of course deploy the original libraries on the client.

WCF 这样做的原因 - 这个概念是服务可以被没有原始二进制文件的客户端使用,甚至不支持 .net(例如 Java) - 因此服务边界发布所有内容客户端需要为了调用它,并处理它的响应.

The reason WCF does this - the concept is that the services could be consumed by a client that doesn't have your original binaries, or even doesn't support .net (e.g. Java) - therefore the service boundary publishes everything a client would need in order to call it, and deal with its response.

这篇关于“复制"WCF 代理代码中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆