WebMethod在参数中接收null

时间:2009-05-27 16:23:43

标签: c# asp.net web-services

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

的WebMethod

[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;
}

2 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分:0)

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