WCF 服务的 VS2003 Web 参考具有额外的“IdSpecified"范围 [英] VS2003 Web Reference for a WCF Service has Extra "IdSpecified" Parameter

查看:42
本文介绍了WCF 服务的 VS2003 Web 参考具有额外的“IdSpecified"范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

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

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

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

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

Here is my VSTS 2008 WCF server side code,

[OperationContract]
StudentInfo Poll(int Id);

编辑 1:这里是客户端自动生成的关于 Poll 方法的代码的一部分.

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]));
}

推荐答案

George,

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

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.

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

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.

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

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);

试试看,看看代理在 VS2003 中是什么样子.

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

这篇关于WCF 服务的 VS2003 Web 参考具有额外的“IdSpecified"范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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