什么会导致 WCF 服务返回“object”类型的对象?
根据我的另一篇关于 WCF 服务返回值,我正在使用另一家公司的 Web 服务,当我在 Visual Studio 中添加服务引用时,该方法的返回值是 object
。
Web 服务的作者向我展示了代码,它实际上返回了一个类型化对象。
我是否遗漏了某些内容,或者代理类是否应该返回键入的值?
是否有用于生成代理类或实际服务的设置?
更新:
我查看了 WCF 服务背后的实际类,并意识到服务方法的返回值实际上返回一个接口,即具体类型实现。 具体类型用 [DataContract] 属性(和适当的 [DataMember] 属性)标记,但接口没有这样的属性。 这是否会导致服务将返回类型设置为对象?
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
.
The author of the web service showed me the code, and it actually returns a typed object.
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. The concrete type is marked with the [DataContract] attribute ( and appropriate [DataMember] attributes), but the interface has no such attributes. Could this be causing the service to set the return type as object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设,如果您是服务开发人员,您可以使用
KnownTypeAttribute
:我个人还没有使用接口尝试过此操作,但我已经使用抽象基类尝试过,并且效果很好。 当客户端收到返回值后,就可以成功向下转型为派生类。
服务代码可能确实执行了此操作,问题在于 svcutil.exe 生成的代理类不够准确。
尽管您不控制服务代码,但您可以控制客户端代理代码。 您可以尝试手动编辑
svcutil.exe
为您提供的代理类,以自行添加KnownTypeAttribute
。 通过这样做,您可以控制 DataContractSerializer 的行为,只要您注意不要错误地更改数据的传输格式,它就应该仍然有效。Hypothetically, if you were the service developer, you could use a
KnownTypeAttribute
: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.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 theKnownTypeAttribute
yourself. 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.代理类是一个生成的文件,因此它可能包含错误。 如果您有数据协定的副本,您可以随意更改代理类以使用正确的类型而不是 System.Object,并且一切应该正常工作。
Visual Studio“添加服务引用”工具和 svcutil.exe 非常擅长生成代理类,但它们并不完美。 它们生成的文件是您可以修改的,我鼓励您简单地修改操作以返回正确的数据契约。
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.The Visual Studio "Add Service Reference" tool and
svcutil.exe
are very good at generating proxy classes but they are not perfect. 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.在使用 WCF 的 Java Web 服务时,我们遇到了类似的问题。
在我们的例子中,它所说的返回类型是实际返回类型的有限版本。
对我们有用的是将对象转换为预期的类型。 之后数据就可用了。
因此,要解决您的问题,您可以尝试将对象强制转换为预期的类型。
We had a similar problem when consuming a java web service from WCF.
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.