简体   繁体   English

在运行时选择“使用”命名空间

[英]Choose 'using' namespace at runtime

I have a web service client application that connects to a 3rd party's web service. 我有一个连接到第三方的Web服务的Web服务客户端应用程序。 The 3rd party has updated their web service so now my client app fails with SOAP exceptions. 第三方更新了他们的Web服务,因此现在我的客户端应用程序因SOAP异常而失败。 Unfortunately, I can't simply update my app to work with the new web service since some of my users will still be using the old web service. 不幸的是,由于一些用户仍将使用旧的Web服务,因此我不能简单地更新我的应用程序以使其与新的Web服务一起使用。 So basically I'd like to be able to work with either version of the web service using a single version of my client app. 因此,基本上,我希望能够使用单个版本的客户端应用程序来使用任一版本的Web服务。

Since Visual Studio generates a bunch of code that I compile against when a web reference is added, I now have two files: one generated for each version of the web service. 由于Visual Studio会生成一堆代码,因此在添加Web引用时我会根据这些代码进行编译,因此我现在拥有两个文件:一个为每个版本的Web服务生成。 Both of the APIs are actually identical, it is just the underlying SOAP messages that seem to have changed. 这两个API实际上是相同的,只是底层的SOAP消息似乎已经改变了。 I've put each of these generated files into their own namespace to avoid collisions. 我已将每个生成的文件放入其自己的命名空间中,以避免发生冲突。 So now I'd like to be able to choose which namespace to use at runtime (based on a config parameter). 因此,现在我希望能够选择要在运行时使用的名称空间(基于config参数)。

For example: 例如:

if (useOldVersion)
    using OldVersionNamespace;
else
    using NewVersionNamespace;

Obviously, this would never work since there would be no way to compile the code that references elements in this namespace. 显然,这将永远无法进行,因为将无法编译引用此命名空间中元素的代码。 Is there an easy way to deal with this issue? 有没有简单的方法来解决此问题? Maybe I could use reflection to do what I need at runtime? 也许我可以在运行时使用反射来做我需要做的事情?

The only thing I can really think of would be to literally copy/paste my current code into two different files, the only difference being which namespace is used at the top. 我真正想到的唯一一件事就是将当前代码从字面上复制/粘贴到两个不同的文件中,唯一的区别是在顶部使用了哪个名称空间。 This seems like a really bad idea though. 但是,这似乎是一个非常糟糕的主意。

Make both sets of generated classes implement a common interface. 使两组生成的类都实现一个公共接口。 This is pretty easy with partial types - you don't need to change the generated class at all, just create an appropriate interface and then a file with the same types in as the generated ones, saying that they implement the interface. 对于部分类型,这非常容易-您根本不需要更改生成的类,只需创建一个适当的接口,然后再创建一个与生成的类型相同类型的文件,就说明它们实现了该接口。

You then code to the interface, and pick which implementation to pick in one place based on a config file. 然后,您对接口进行编码,并根据配置文件在一个位置选择要选择的实现。

The additional benefit to this is you can then write unit tests against your code, mocking out the web service. 这样做的另一个好处是,您可以然后针对代码编写单元测试,从而模拟Web服务。

couldn't you use a compile time if? 你不能用编译时间吗?

#If (BuildType)
using X;
#else
using Y;
#endif

http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx[link text][1] http://msdn.microsoft.com/zh-CN/library/4y6tbswk.aspx [链接文本] [1]

this is quick and dirty. 这又快又脏。 hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM