c# - WebMethod 在参数中接收 null

标签 c# asp.net web-services

我有一个 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;
}

最佳答案

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

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

关于c# - WebMethod 在参数中接收 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/916711/

相关文章:

c# - HttpRequestException 在 Windows Phone 中的激活事件中使用 HttpClient

c# - 从 IIS .Net 应用程序访问网络共享

c# - 为什么 DbMigration 类中的 Up() 方法是抽象方法,而 Down() 方法是虚拟方法

c# - 在 API REST .NET 中从 NLog 读取 NLog.config

asp.net - 什么是Asp.net中的信任?

c# - 为什么这个带有花括号的错误对象初始化甚至可以编译?

c# - ASP.NET 用 html 数字替换双引号

java - 如何识别请求类型是同步还是异步

java - 如何使用axis 1.4将java对象转换为XML?

java - Web 服务代理中的 SSL 证书问题