【问题标题】:Extra parameters added to webmethods添加到 webmethod 的额外参数
【发布时间】:2011-11-15 11:23:40
【问题描述】:

我正在从 wsdl 生成的代理中读取一些 MethodInfo

其中一个方法有三个(int)参数和一个int返回类型,但是当我探索ParameterInfo[]时,我实际上看到了八个参数:

  • Int32,
  • Boolean,
  • Int32,
  • Boolean,
  • Int32,
  • Boolean,
  • Int32&,
  • Boolean&

这些额外参数从何而来?

更新

更详细地说,生成的代理中的代码如下所示:

  /// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/IInleerAppService/AddThreeNumbers", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public void AddThreeNumbers(int one, [System.Xml.Serialization.XmlIgnoreAttribute()] bool oneSpecified, int two, [System.Xml.Serialization.XmlIgnoreAttribute()] bool twoSpecified, int three, [System.Xml.Serialization.XmlIgnoreAttribute()] bool threeSpecified, out int AddThreeNumbersResult, [System.Xml.Serialization.XmlIgnoreAttribute()] out bool AddThreeNumbersResultSpecified) {
    object[] results = this.Invoke("AddThreeNumbers", new object[] {
                one,
                oneSpecified,
                two,
                twoSpecified,
                three,
                threeSpecified});
    AddThreeNumbersResult = ((int)(results[0]));
    AddThreeNumbersResultSpecified = ((bool)(results[1]));
}

这是为什么?

更新

如果您像我一样对此感到困扰,您可以通过简单地应用以下 sn-p 代码来轻松避免显示这些额外参数:

if (!parameterInfo[i].Name.EndsWith("Specified") && !parameterInfo[i].IsRetval && !parameterInfo[i].Name.EndsWith("Result"))
{
    // magic
}

【问题讨论】:

  • 你是用什么来获取 MethodInfo 的?你确定这是同样的方法吗?例如,这里的方法名称是什么?基本上:请提供更多上下文
  • 基本上我只是在代码中生成了一个代理程序集并通过反射读取它。 webservice也是我自己写的,确实是一样的方法。

标签: c# wcf reflection proxy


【解决方案1】:

我最近自己发现了这一点。在某些情况下,它与 XSD 中的 minoccurs=0 有关。 WCF 代理类不使用可为空的类型,因此 XmlSerializer 无法确定您是否希望发送某个字段。

您可以设置one,但不会发送。您还必须将oneSpecified 设置为true 以使序列化程序将one 的值序列化并发送。

更多信息here

【讨论】:

  • 当我从 VS 2008 迁移到 VS 2010 时,我自己也遇到了类似的问题。CodeCaster 的答案很好!
猜你喜欢
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-01
相关资源
最近更新 更多