C# ASP.NET Web 服务结果、代理类和类型转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6681/
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
ASP.NET Web Service Results, Proxy Classes and Type Conversion
提问by Rob Cooper
I'm still new to the ASP.NET world, so I could be way off base here, but so far this is to the best of my (limited) knowledge!
我还是 ASP.NET 世界的新手,所以我在这里可能有点偏离基础,但到目前为止,这是我(有限的)知识中最好的!
Let's say I have a standard business object "Contact" in the Businessnamespace. I write a Web Service to retrieve a Contact's info from a database and return it. I then write a client application to request said details.
假设我在Business命名空间中有一个标准的业务对象“联系人” 。我编写了一个 Web 服务来从数据库中检索联系人的信息并返回它。然后我编写一个客户端应用程序来请求所述详细信息。
Now, I also then create a utility method that takes a "Contact" and does some magic with it, like Utils.BuyContactNewHat()
say. Which of course takes the Contact of type Business.Contact
.
现在,我还创建了一个实用方法,它接受一个“联系人”并用它做一些魔术,比如Utils.BuyContactNewHat()
say。这当然需要 type 的 Contact Business.Contact
。
I then go back to my client application and want to utilise the BuyContactNewHat
method, so I add a reference to my Utilsnamespace and there it is. However, a problem arises with:
然后我返回到我的客户端应用程序并想要使用该BuyContactNewHat
方法,所以我添加了对我的Utils命名空间的引用,它就在那里。但是,出现了一个问题:
Contact c = MyWebService.GetContact("Rob);
Utils.BuyContactNewHat(c); // << Error Here
Since the return type of GetContact
is of MyWebService.Contact
and not Business.Contact
as expected. I understand why this is because when accessing a web service, you are actually programming against the proxy class generated by the WSDL.
由于返回类型GetContact
是 ofMyWebService.Contact
而不是Business.Contact
预期的。我理解这是为什么,因为在访问 Web 服务时,您实际上是在针对 WSDL 生成的代理类进行编程。
So, is there an "easier" way to deal with this type of mismatch? I was considering perhaps trying to create a generic converter class that uses reflection to ensure two objects have the same structure than simply transferring the values across from one to the other.
那么,有没有一种“更简单”的方法来处理这种类型的不匹配?我正在考虑尝试创建一个通用转换器类,该类使用反射来确保两个对象具有相同的结构,而不是简单地将值从一个传递到另一个。
采纳答案by Lance Fisher
You are on the right track. To get the data from the proxy object back into one of your own objects, you have to do left-hand-right-hand code. i.e. copy property values. I'll bet you that there is already a generic method out there that uses reflection.
你走在正确的轨道上。要将代理对象中的数据返回到您自己的对象之一,您必须执行左右手代码。即复制属性值。我敢打赌,已经有一种使用反射的通用方法。
Some people will use something other than a web service (.net remoting) if they just want to get a business object across the wire. Or they'll use binary serialization. I'm guessing you are using the web service for a reason, so you'll have to do property copying.
有些人如果只想通过网络获取业务对象,则会使用 Web 服务(.net 远程处理)以外的其他服务。或者他们将使用二进制序列化。我猜您使用 Web 服务是有原因的,因此您必须进行属性复制。
回答by d4nt
You don't actually have to use the generated class that the WSDL gives you. If you take a look at the code that it generates, it's just making calls into some .NET framework classes to submit SOAP requests. In the past I have copied that code into a normal .cs file and edited it. Although I haven't tried this specifically, I see no reason why you couldn't drop the proxy class definition and use the original class to receive the results of the SOAP call. It must already be doing reflection under the hood, it seems a shame to do it twice.
您实际上不必使用 WSDL 为您提供的生成类。如果您查看它生成的代码,它只是调用一些 .NET 框架类来提交 SOAP 请求。过去,我已将该代码复制到普通的 .cs 文件中并对其进行了编辑。虽然我没有专门尝试过这个,但我看不出为什么不能删除代理类定义并使用原始类来接收 SOAP 调用的结果。它一定已经在幕后进行了反思,两次这样做似乎很可惜。
回答by Mark
I would recommend that you look at writing a Schema Importer Extension, which you can use to control proxy code generation. This approach can be used to (gracefully) resolve your problem without kludges (such as copying around objects from one namespace to another, or modifying the proxy generated reference.cs class only to have it replaced the next time you update the web reference).
我建议您考虑编写一个 Schema Importer Extension,您可以使用它来控制代理代码生成。这种方法可用于(优雅地)解决您的问题,而不会出现混乱(例如将对象从一个命名空间复制到另一个命名空间,或者修改代理生成的 reference.cs 类,以便在下次更新 Web 引用时替换它)。
Here's a (very) good tutorial on the subject:
这是有关该主题的(非常)好的教程:
http://www.microsoft.com/belux/msdn/nl/community/columns/jdruyts/wsproxy.mspx
http://www.microsoft.com/belux/msdn/nl/community/columns/jdruyts/wsproxy.mspx