简体   繁体   English

什么会导致WCF服务返回“对象”类型的对象

[英]What would cause a WCF service to return an object of type “object”

Per my other post about WCF service return values , I'm consuming a web service from another company, and when I add the service reference inside Visual Studio, the return value of the method is an object of type object . 根据我关于WCF服务返回值的其他帖子,我正在使用来自其他公司的Web服务,当我在Visual Studio中添加服务引用时,该方法的返回值是object类型的object

The author of the web service showed me the code, and it actually returns a typed object. Web服务的作者向我展示了代码,它实际上返回了一个类型化的对象。

Am I missing something, or is the proxy class supposed to return a typed value? 我错过了什么,或者代理类是否应该返回一个类型值?

Is there a setting for generating the proxy class, or the actual service? 是否有用于生成代理类或实际服务的设置?

UPDATE: 更新:

I looked at the actual classes behind the WCF service and realized that the return value of the service method is actually returning an interface , that the concrete type implements. 我查看了WCF服务背后的实际类,并意识到service方法的返回值实际上是返回一个具体类型实现的接口 The concrete type is marked with the [DataContract] attribute ( and appropriate [DataMember] attributes), but the interface has no such attributes. 具体类型使用[DataContract]属性(以及相应的[DataMember]属性)标记,但接口没有此类属性。 Could this be causing the service to set the return type as object? 这会导致服务将返回类型设置为对象吗?

Hypothetically, if you were the service developer, you could use a KnownTypeAttribute : 假设您是服务开发人员,可以使用KnownTypeAttribute

[DataContract]
[KnownType(typeof(MyConcreteClass))]
public interface IMyInterface {
}

[DataContract]
public class MyConcreteClass : IMyInterface {
}

I haven't personally tried this with an interface, but I have tried it with an abstract base class and it works fine. 我没有亲自尝试过这个界面,但是我已经尝试了一个抽象的基类,它运行正常。 When the client receives the return value, it can successfully downcast to the derived class. 当客户端收到返回值时,它可以成功向下转换为派生类。

It might be that the service code actually does this, and the problem lies with svcutil.exe not generating the proxy classes accurately enough. 可能是服务代码实际上是这样做的,问题在于svcutil.exe没有足够准确地生成代理类。

Although you don't control the service code, you do control the client proxy code. 虽然您不控制服务代码,但您可以控制客户端代理代码。 You could try manually editing the proxy classes that svcutil.exe gave you to add the KnownTypeAttribute yourself. 您可以尝试手动编辑svcutil.exe为您自己添加KnownTypeAttribute的代理类。 By doing this, you are taking control of the behaviour of the DataContractSerializer at your end, and as long as you take care not to change the wire format of the data by mistake, it should all still work. 通过这样做,您可以控制DataContractSerializer的行为,只要您注意不要错误地更改数据的有线格式,它应该仍然可以工作。

The proxy class is a generated file and as such it can contain mistakes. 代理类是生成的文件,因此它可能包含错误。 If you have a copy of the data contract you are free to change the proxy class to use the correct type rather than System.Object and things ought to work properly. 如果您有数据协定的副本,您可以自由更改代理类以使用正确的类型而不是System.Object并且事情应该正常工作。

The Visual Studio "Add Service Reference" tool and svcutil.exe are very good at generating proxy classes but they are not perfect. Visual Studio“添加服务引用”工具和svcutil.exe非常擅长生成代理类,但它们并不完美。 The files that they generate are yours to modify and I would encourage you to simply modify the operation to return the proper data contract. 他们生成的文件是您修改的文件,我建议您只需修改操作即可返回正确的数据合同。

We had a similar problem when consuming a java web service from WCF. 从WCF使用java Web服务时遇到了类似的问题。

In our case the type that it said that it was returning was a limited version of what was actually being returned. 在我们的例子中,它说它返回的类型是实际返回的有限版本。

What worked for us was to cast the object to the type that was expected. 对我们有用的是将对象转换为预期的类型。 After that the data was available. 之后,数据可用。

Therefore, to fix your problem you could try to cast the object to the type expected. 因此,要解决您的问题,您可以尝试将对象强制转换为预期的类型。

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

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