简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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