.NET Webservice 使用参数类型的实例?
我的解决方案中有一个 Windows 窗体项目和一个 Web 服务项目,我尝试调用 Web 服务并返回一个客户对象作为结果。 问题是,当我尝试接收返回对象时,出现无法转换它的错误。 例如,这是我的 Web 服务的签名:
Public Function GetDriverByID(ByVal DriverID As Integer) As Driver
这是我用来调用它的代码:
Dim d As Driver = mywebserviceinstance.GetDriverByID(1)
但我收到此编译时错误(wsDrivers 是我添加到表单项目中的 Web 引用的名称) :“ProjectNamespace.Common.wsDrivers.Driver 类型的值无法转换为 ProjectNamespace.Common.Driver”
这个“Common”命名空间包含 Driver 类,我不确定为什么来自 Web 服务的返回类不仅仅是一个通用的“Driver”,而是一个“wsDrivers.Driver”,我无法将其转换回来。 有人知道我该如何处理这种类型不匹配的情况吗?
编辑:感谢您的解释 - 这实际上清楚地表明了它在做什么。 但是,有什么方法可以强制它使用实际类型而不是代理(或者,有没有什么方法可以在“真实”实例和“代理”实例之间进行转换),或者我必须这样做在通过网络发送属性之前序列化属性,然后手动反序列化返回值?
I have a Windows forms project and a Web Service project in my solution, and I'm trying to call the web service and return a customer object as the result. The problem is that when I try to receive the return object, I get an error that it can't convert it. For example, here is the signature for my webservice:
Public Function GetDriverByID(ByVal DriverID As Integer) As Driver
And here is the code I'm using to call it:
Dim d As Driver = mywebserviceinstance.GetDriverByID(1)
But I receive this compile-time error (wsDrivers is the name of the web reference I've added to my form project): "Value of type ProjectNamespace.Common.wsDrivers.Driver cannot be converted to ProjectNamespace.Common.Driver"
This "Common" namespace contains the Driver class, and I'm not sure why the return class from the web service isn't just a generic "Driver", but is instead a "wsDrivers.Driver", and I can't convert it back. Anybody know how I can deal with this type mismatch?
EDIT: Thanks for the explanations - this actually makes it clear what it's doing. However, is there any way that I can force it to use the actual type instead of the proxy (or, rather, is there any way to convert between the "real" instance and the "proxy" instance), or do I have to serialize the properties before I send them over the wire, and then manually de-serialize the return values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上很常见。 所发生的情况是,Web 服务已在其中定义了 Web 服务中使用的所有类型的定义。 当您添加对该 Web 服务的引用时,它会在您的命名空间的子命名空间中自动生成代理类型。 这就是您调用 Web 服务时返回的内容。
但是,您可能还引用 Web 服务单独执行的包含相同类型的同一库。 这是您调暗驱动程序时所期望的类型。 这就是为什么存在不匹配的原因。
This is actually pretty common. What's happening is that the Web Service has defined in it the definitions of all the types used in the web service. When you add a reference to that web service, it auto-generates a proxy type in a sub namespace of your namespace. That is what is being returned by your web service when you call it.
However, you probably are also referencing the same library that the web service does seperately that contains the same type. That is the type that is expected when you Dim Driver. That's why there is a mismatch.
VB.NET 或 C# 项目中的 Web 服务引用可以引用任何类型的 Web 服务,并且不限于 ASP.NET 提供的服务。 这就是 Visual Studio 为每个可从 Web 服务检索的对象创建代理类的原因。
The web service reference in a VB.NET or C# project can reference any type of web service and is not limited to those provided by ASP.NET. That is why Visual Studio creates proxy classes for each object which can be retrieved from the web service.