简体   繁体   English

WCF服务的VS2003 Web参考具有额外的“ IdSpecified”参数

[英]VS2003 Web Reference for a WCF Service has Extra “IdSpecified” Parameter

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). 我正在使用VSTS 2008 + .Net 3.5 + C#开发WCF服务,并且当我也使用VSTS 2008开发客户端(使用添加服务引用功能自动生成客户端Web服务代理代码)时,它可以正常工作。 The WCF I developed is using basicHttpBinding. 我开发的WCF使用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). 我遇到的问题是,当我使用Visual Studio.Net(Visual Studio 2003)生成客户端Web服务代理代码时,OperationContract方法的另一个输入参数称为IdSpecified(布尔类型)。 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. 我已经测试过,当将IdSpecified设置为true时,Id参数的值将正确传递到WCF服务器端,但是当我将IdSpecified设置为false时,无论我对Id参数指定什么值,在WCF服务器端,Id都是始终为0。我也尝试过输入参数类型(例如字符串),客户端没有这种附加输入参数。

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, 这是Visual Studio.Net自动生成的客户端Web服务代理代码,

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

Here is my VSTS 2008 WCF server side code, 这是我的VSTS 2008 WCF服务器端代码,

[OperationContract]
StudentInfo Poll(int Id);

EDIT 1: here is part of the automatically generated code at client side about Poll method. 编辑1:这是客户端自动生成的有关轮询方法的代码的一部分。

[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, 乔治,

This behavior is by design, and has been there since .NET 1.0. 此行为是设计使然,自.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. 实际上,如果要使用VS2003创建具有相同方法签名的ASMX Web服务,则会发现相同的结果。

The issue is with value types that are marked in the WSDL as not being required. 问题出在WSDL中标记为不需要的值类型。 Since they are value types, they can't return null (and VS2003 did not have nullable types). 由于它们是值类型,因此它们不能返回null(并且VS2003没有可为null的类型)。 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. Microsoft实现的解决方案是添加一个单独的布尔字段或属性,您可以设置该字段或属性来说明是否提供值。

This means that when your .NET 1.1 application wants to call the service, it needs to set the IdSpecified parameter: 这意味着,当您的.NET 1.1应用程序要调用该服务时,需要设置IdSpecified参数:

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. 试试看,看看代理在VS2003中是什么样子。

You could try using a DataContract rather than a simple integer parameter. 您可以尝试使用DataContract而不是简单的整数参数。 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. 数据合同允许您指定是否需要某个成员,如果需要该成员,则可以删除该怪异的多余布尔值。

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

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