VS2003 Web引用一个WCF服务有多余的" IdSpecified"参数 [英] VS2003 Web Reference for a WCF Service has Extra "IdSpecified" Parameter

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

问题描述

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

我遇到的,当我使用的Visual Studio.Net(Visual Studio 2003中)生成客户端Web服务代理code,有一个名为IdSpecified(布尔型)OperationContract的方法额外的输入参数的问题。我测试的时候指定IdSpecified为true,id参数的值将被正确地传递给WCF服务器端,但是当我指定IdSpecified为false,不管我指定Id参数什么样的价值观,在WCF服务器端,ID将被始终为0。我也试着像字符串输入参数类型,还有在客户端没有这种额外的输入参数。

我的问题是,为什么有一个额外的参数?这是它的含义,并能够避免产生这样的附加参数α

下面是的Visual Studio.Net自动生成客户端Web服务代理code,

 公共StudentInfo投票(INT编号,[System.Xml.Serialization.XmlIgnoreAttribute()]布尔IdSpecified)
 

下面是我的VSTS 2008年的WCF服务器端code,

  [OperationContract的]
StudentInfo投票(INT ID);
 

编辑1:这里是自动生成的code约民意调查方法,客户端部分。

  [返回:System.Xml.Serialization.XmlElementAttribute(ISNULLABLE =真)
公共StudentInfo投票(INT编号,[System.Xml.Serialization.XmlIgnoreAttribute()]布尔IdSpecified){
    [对象]结果= this.Invoke(民意调查,新的对象[] {
                身份证,
                IdSpecified});
    返回((StudentInfo)(结果[0]));
}
 

解决方案

乔治

此行​​为是由设计,因为.NET 1.0已经在那里。事实上,如果你要创建一个VS2003 ASMX Web服务,用同样的方法签名,你会发现同样的结果。

问题是与被标记为不被需要的WSDL值类型。由于他们是值类型,它们不能返回null(与VS2003没有可空类型)。微软实现的解决方案是添加一个单独的布尔字段或属性可以设置要说你是否正在提供的价值。

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

 使用(WebReference1.PollService SVC =新WebReference1.PollService()){
    StudentInfo信息= svc.Poll(ID,真实); //真正的指定
}
 


我没有尝试这样做,但你为什么不:

  [DataContract]
公共类PollParameters {
    [数据成员(必填=真)
    公众诠释标识;
}

[OperationContract的]
公共StudentInfo投票(PollParameters参数);
 

尝试,看看代理看起来像VS2003。

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.

这篇关于VS2003 Web引用一个WCF服务有多余的" IdSpecified"参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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