【问题标题】:WebMethod with optional parameter doesn't work in client side [duplicate]带有可选参数的 WebMethod 在客户端不起作用 [重复]
【发布时间】:2013-05-21 07:24:12
【问题描述】:

我写了一个带有可选参数的网络方法。

[WebMethod]
  public void EmailSend(string from, string to, string cc = null, string bcc = null, string replyToList = null, string subject = null, string body = null, bool isBodyHtml = false , string[] attachmentNames = null, byte[][] attachmentContents = null)
    {
     .....
    }

我在客户端应用程序中调用此方法

 EmailServiceManagement.EmailService es = new EmailServiceManagement.EmailService();
 es.EmailSend(from, to,null,null,null,subject,body,true,attName,att); //this works

但是

es.EmailSend(from,to); // this isn't working. According to c# optional parameter syntax it must work.

我做错了什么?

【问题讨论】:

  • Web 方法应该独立于平台/语言,并且每种语言都不必支持可选参数。所以你的第一个版本是正确的。
  • @I4V 所以在web方法中使用可选参数没有任何意义。

标签: c# c#-4.0


【解决方案1】:

WebMethods 上不能有可选参数。你可以做的是有这样的重载方法:

[WebMethod(MessageName="Test")]
public string GenerateMessage(string firstName)
{
   return string.Concat("Hi ", firstName);
}

[WebMethod(MessageName="AnotherTest")]
public string GenerateMessage(string firstName, string lastName)
{
   return string.Format("Hi {0} {1}", firstName, lastName);
}

不确定您是如何与此 WebMethod 交互的,但有这么多参数可能表明您可以将它们分组到一个对象中,例如:

[WebMethod]
public void EmailSend(MessageParameters messageParams)
{
     .....
}

【讨论】:

  • 谢谢,但我知道重载方法,我想知道是可选参数在 webmethods 上不起作用还是我做错了什么。
  • 不,他们只是不工作。
  • 为什么我被否决了?愿意发表评论吗?解决方案错了吗?我想知道我是否做错了,谢谢。
  • @DimitarDimitrov 我错过了MessageName 部分。如果你编辑你的答案,我会投票。
猜你喜欢
  • 2010-10-04
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
相关资源
最近更新 更多