WCF 服务的 VS2003 Web 参考有额外的“IdSpecified” 范围

发布于 2024-07-29 16:22:20 字数 1163 浏览 2 评论 0原文

我正在使用 VSTS 2008 + .Net 3.5 + C# 开发 WCF 服务,当我也使用 VSTS 2008 开发客户端时(使用添加服务引用功能自动生成客户端 Web 服务代理代码),它工作得很好。 我开发的WCF使用的是basicHttpBinding。

我遇到的问题是,当我使用 Visual Studio.Net (Visual Studio 2003) 生成客户端 Web 服务代理代码时,OperationContract 方法有一个名为 IdSpecified(bool 类型)的附加输入参数。 我测试过,当将 IdSpecified 指定为 true 时,Id 参数的值将正确传递到 WCF 服务器端,但是当我将 IdSpecified 指定为 false 时,无论我为 Id 参数指定什么值,在 WCF 服务器端,Id 都将是总是0。我也尝试过像字符串这样的输入参数类型,客户端没有这样的附加输入参数。

我的问题是为什么有一个额外的参数? 它的含义是什么?是否可以避免生成这样的附加参数?

这是 Visual Studio.Net 自动生成的客户端 Web 服务代理代码,

public StudentInfo Poll(int Id, [System.Xml.Serialization.XmlIgnoreAttribute()] bool IdSpecified)

这是我的 VSTS 2008 WCF 服务器端代码,

[OperationContract]
StudentInfo Poll(int Id);

编辑 1:这是在客户端自动生成的有关 Poll 方法的代码的一部分。

[return: System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public StudentInfo Poll(int Id, [System.Xml.Serialization.XmlIgnoreAttribute()] bool IdSpecified) {
    object[] results = this.Invoke("Poll", new object[] {
                Id,
                IdSpecified});
    return ((StudentInfo)(results[0]));
}

I am developing a WCF service using VSTS 2008 + .Net 3.5 + C# and it works fine when I also use VSTS 2008 to develop client (using Add Service Reference function to automatically generated client web services proxy code). The WCF I developed is using basicHttpBinding.

The issue I met with is, when I use Visual Studio.Net (Visual Studio 2003) to generate client web services proxy code, there is an additional input parameter for a OperationContract method called IdSpecified (bool type). I have tested that when specify IdSpecified to true, the value of Id parameter will be passed to WCF server side correctly, but when I specify IdSpecified to false, no matter what values I specify to Id parameter, at WCF server side, Id will be always 0. I also tried for input parameter type like string, there is no such additional input parameter at client side.

My question is why there is an additional parameter? What is its meaning and is it possible to avoid generate such additional parameter?

Here is Visual Studio.Net automatically generated client side web services proxy code,

public StudentInfo Poll(int Id, [System.Xml.Serialization.XmlIgnoreAttribute()] bool IdSpecified)

Here is my VSTS 2008 WCF server side code,

[OperationContract]
StudentInfo Poll(int Id);

EDIT 1: here is part of the automatically generated code at client side about Poll method.

[return: System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public StudentInfo Poll(int Id, [System.Xml.Serialization.XmlIgnoreAttribute()] bool IdSpecified) {
    object[] results = this.Invoke("Poll", new object[] {
                Id,
                IdSpecified});
    return ((StudentInfo)(results[0]));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄居人 2024-08-05 16:22:20

George,

这种行为是设计使然,自 .NET 1.0 以来就一直存在。 事实上,如果您使用 VS2003 创建一个 ASMX Web 服务,并使用相同的方法签名,您会发现相同的结果。

问题在于 WSDL 中标记为不需要的值类型。 由于它们是值类型,因此不能返回 null(VS2003 没有可为 null 的类型)。 Microsoft 实施的解决方案是添加一个单独的布尔字段或属性,您可以设置它来说明是否提供该值。

这意味着当您的.NET 1.1应用程序想要调用该服务时,它需要设置IdSpecified参数:

using (WebReference1.PollService svc = new WebReference1.PollService()) {
    StudentInfo info = svc.Poll(id, true); // True to specify
}

我还没有尝试过这个,但是为什么不呢:

[DataContract]
public class PollParameters {
    [DataMember(Required = true)]
    public int Id;
}

[OperationContract]
public StudentInfo Poll(PollParameters parameters);

尝试一下,看看代理在VS2003中是什么样子。

George,

This behavior is by design, and has been there since .NET 1.0. In fact, if you were to create an ASMX web service with VS2003, with the same method signature, you would find the same result.

The issue is with value types that are marked in the WSDL as not being required. Since they are value types, they can't return null (and VS2003 did not have nullable types). The solution that Microsoft implemented was to add a separate boolean field or property you can set to say whether or not you are supplying the value.

This means that when your .NET 1.1 application wants to call the service, it needs to set the IdSpecified parameter:

using (WebReference1.PollService svc = new WebReference1.PollService()) {
    StudentInfo info = svc.Poll(id, true); // True to specify
}

I haven't tried this, but why don't you:

[DataContract]
public class PollParameters {
    [DataMember(Required = true)]
    public int Id;
}

[OperationContract]
public StudentInfo Poll(PollParameters parameters);

Try that and see what the proxy looks like in VS2003.

甜柠檬 2024-08-05 16:22:20

您可以尝试使用 DataContract 而不是简单的整数参数。 数据协定允许您指定是否需要成员,如果需要成员,可能会删除那个奇怪的额外布尔值。

You could try using a DataContract rather than a simple integer parameter. The data contract allows you to specify if a member is required or not, which, if the member is required, might remove that weird extra bool.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文