WCF 生成的存根没有生成正确的返回类型 [英] WCF generated stub does not generate correct return type

查看:30
本文介绍了WCF 生成的存根没有生成正确的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 客户端-服务器设置.在服务器端,我在我的服务方法中使用接口作为返回类型,然后使用 <ServiceKnownTypeAttribute> 来分配我的合同/服务.所有这些都运行良好.

I have a WCF client-server setup. On the server side I am using interfaces as return types in my service methods and then attributing my contracts/services with the <ServiceKnownTypeAttribute>. All this is working fine.

然而,有一个问题很烦人.当我向客户端添加服务引用时,生成的存根方法都具有 object 的返回类型.因此,我的客户端上不再存在 Intellisense.我完全能够使用返回值并可以访问属性,但我必须进行强制转换才能恢复"Intellisense.

There is, however, one issue that is quite annoying. When I add a service reference to my client, the generated stub methods all have a return type of object. Hence, Intellisense is no longer existing on my client. I am fully able to use the return values and can access the properties, but I have to do a cast to be able to "restore" Intellisense.

示例:

服务器方法:

<OperationContract()>
<ServiceKnownTypeAttribute(GetType(Something))>
Function GetSomething() As ISomething Implements Iservice.GetSomething

生成的存根:

Function GetSomething() As Object Implements Iservice.GetSomething

客户端代码(必须在此处进行强制转换,因为存根返回类型是对象):

client code (must do cast here as stub return type is object):

Dim s As ISomething= CType(client.GetSomething(), ISomething)

我是否遗漏了什么,或者这只是正常行为.由于对象可以序列化并且一切正常,我无法理解为什么我的存根不是特定于类型的.

Am I missing something or is this just normal behaviour. As the objects can be serialized and all is working I cant understand why my stub is not type specific.

谢谢

乔纳斯

推荐答案

我想我明白发生了什么,我会尝试给出解释,尽管其中一些是有根据的猜测.

I think I understand what's going on, and I'll try to give an explanation, though some of it is an educated guess.

首先,我将猜测您实际上正在生成一个具体类型,该类型实现了函数中的 ISomething,但您返回的是接口而不是对象.假设 MySomething 实现了 ISomething,它可能看起来像这样:

First, I'm going to make a guess that you're actually generating a concrete type that implements ISomething from the function, but you're returning the interface rather than the object. Assume MySomething implements ISomething and it might look like this:

Function GetSomething() As ISomething Implements Iservice.GetSomething

    ' code to create the object
    Return MySomething
End Function

如果您要在控制台应用程序之类的东西中调用它,您可能会这样做:

If you were to call this in something like a console app, you'd probably do this:

Dim MySomething As ISomething = GetSomething()

现在我要进行有根据的猜测了.

Now here's where I'm making an educated guess.

WCF 将根据服务契约的接口生成代理.由于服务契约的操作契约返回的是一个接口而不是一个具体的类,它使用 Object 来生成一个具体的实现,以便数据可以通过线路序列化.

WCF will generate the proxy based on the interface for the service contract. Since the service contract's operation contract is returning an interface as opposed to a concrete class, it uses Object to generate a concrete implementation so the data can be serialized over the wire.

由于在这种情况下客户端不知道 Object 正在实现 ISomething,因此除了强制转换 <代码>对象到ISomething.

Since the client doesn't know the Object in this case is implementing ISomething, I don't see any other way to "restore" Intellisense other than casting Object to ISomething.

这篇关于WCF 生成的存根没有生成正确的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆