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:
我的解决方案中有一个Windows窗体项目和一个Web服务项目,我正在尝试调用Web服务并返回一个客户对象作为结果。问题是,当我尝试接收返回对象时,我收到一个错误,它无法转换它。例如,这是我的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"
但是我收到这个编译时错误(wsDrivers是我添加到我的表单项目的Web引用的名称):“ProjectNamespace.Common.wsDrivers.Driver类型的值无法转换为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?
这个“Common”命名空间包含Driver类,我不确定为什么Web服务的返回类不仅仅是一个通用的“Driver”,而是一个“wsDrivers.Driver”,我无法转换回来了。谁知道我怎么能处理这种类型的不匹配?
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?
编辑:感谢您的解释 - 这实际上清楚地说明了它在做什么。但是,有什么方法可以强制它使用实际类型而不是代理(或者更确切地说,有没有办法在“真实”实例和“代理”实例之间进行转换),或者我是否必须在通过网络发送属性之前序列化属性,然后手动反序列化返回值?
2 个解决方案
#1
2
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.
这实际上很常见。发生的事情是Web服务在其中定义了Web服务中使用的所有类型的定义。添加对该Web服务的引用时,它会在命名空间的子命名空间中自动生成代理类型。这就是您调用Web服务时返回的内容。
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.
但是,您可能还引用了Web服务单独包含相同类型的库。这是Dim Driver时预期的类型。这就是为什么会出现不匹配的原因。
#2
0
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.
VB.NET或C#项目中的Web服务引用可以引用任何类型的Web服务,并且不限于ASP.NET提供的那些。这就是Visual Studio为每个可以从Web服务检索的对象创建代理类的原因。
#1
2
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.
这实际上很常见。发生的事情是Web服务在其中定义了Web服务中使用的所有类型的定义。添加对该Web服务的引用时,它会在命名空间的子命名空间中自动生成代理类型。这就是您调用Web服务时返回的内容。
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.
但是,您可能还引用了Web服务单独包含相同类型的库。这是Dim Driver时预期的类型。这就是为什么会出现不匹配的原因。
#2
0
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.
VB.NET或C#项目中的Web服务引用可以引用任何类型的Web服务,并且不限于ASP.NET提供的那些。这就是Visual Studio为每个可以从Web服务检索的对象创建代理类的原因。