【问题标题】:WebMethod receives null in parametersWebMethod 在参数中接收 null
【发布时间】:2009-05-27 16:23:42
【问题描述】:

我有一个带有两个字符串参数的方法的 Web 服务。当我调试时,我可以在调用方法中看到它将两个字符串值传递给方法,但实际上 WebMethod 只是为这两个值获取 null。这是一些代码:

网络方法

[WebMethod(Description = "Set username and password for validation purposes.")]
public void Login(string uname, string pword)
{
    username = uname;
    password = pword;
}

调用方法

NewsletterEmailSubscribers nes = new NewsletterEmailSubscribers();
nes.Login("Username", "Password");

我在这里做错了什么?

--编辑--

添加更多代码。

网络服务:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class NewsletterEmailSubscribers : WebService
{
    private static string username, password;

    public NewsletterEmailSubscribers()
    {

    }


    /// <summary>
    /// Logins the specified username.
    /// </summary>
    /// <param name="username">The username.</param>
    /// <param name="password">The password.</param>
    [WebMethod(Description = "Set username and password for validation purposes.")]
    public void Login(string uname, string pword)
    {
        username = uname;
        password = pword;
    }

    /// <summary>
    /// Adds subscriber email account.
    /// </summary>
    /// <param name="emailAddress">The email address</param>
    /// <param name="newsletterType">The newsletter they have signed up to receive</param>
    /// <param name="validationCode">The validation code</param>
    [WebMethod(Description = "Initial add of subscriber email address and newsletter signing up for.")]

    public void AddSubscriber(
        string emailAddress,
        string newsletterType,
        string validationCode)
    {
        // Check some values
        //Authenticate user, will throw exception if the user is invalid

        using (SOAValidation validation = new SOAValidation())
        {
            validation.ValidateConnection(validationCode, username, password, "Full");
        }

        OracleParameterCollection parameters = new OracleParameterCollection();
        parameters.AddWithValue("subscriber_email", emailAddress);
        parameters.AddWithValue("newsletter_type", newsletterType);
        Database.ExecuteQuery("dw.newsletter_pkg.newsletter_subscriber_add", parameters);
    }
}

使用服务的网页(NewsletterEmailSubscribers)

private void SubmitEmail(string email)
{
    if (ValidateEmail(email))
    {
        try
        {
            NewsletterEmailSubscribers nes = new NewsletterEmailSubscribers();
            nes.Login("Username", "Password");
            string validationCode;
            using (Cokesbury.RemoteValidation.Validator validator = new Cokesbury.RemoteValidation.Validator())
            {
                validationCode = validator.ValidationCode(System.Configuration.ConfigurationManager.AppSettings["PasswordSalt"].ToString());
            }

            // submit to db
            nes.AddSubscriber(email, "FICT", validationCode);
            // Switch to confirm message
            mvPage.SetActiveView(vwThankYou);
        }
        catch (Exception ex)
        {
            mvPage.SetActiveView(vwFail);
            bool rethrow = ExceptionPolicy.HandleException(ex, "Presentation Services Exception Policy");
            if (rethrow)
            {
                throw (ex);
            }
        }
    }
    else
        lblEmailError.Visible = true;
}

【问题讨论】:

  • 你能发布整个代码吗?看不出有任何失败的原因。
  • 提供的信息太少,无法回答。只是一个盲目的猜测:尝试重新生成您的客户端代理。

标签: c# asp.net web-services


【解决方案1】:

您是否创建了此 Web 服务然后将其连接到您的其他代码,或者您是否将 Web 服务设计为使用与另一个相同的接口?

我做了一次后者并更改了 WebMethod 中参数的大小写,这导致我收到 null 而不是正确的值。

【讨论】:

    【解决方案2】:

    就在我今天发生了这种情况,最后通过简单地更新服务解决了问题(正如达林建议的那样)。发生的情况是我重命名了 WebMethod 中的一些参数,但客户端仍然使用旧名称来发送请求。

    【讨论】:

      猜你喜欢
      • 2012-04-05
      • 2015-10-26
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 2018-11-18
      • 2020-08-14
      • 2015-07-26
      • 2020-03-20
      相关资源
      最近更新 更多