C# · 12月 22, 2021

c# – WebMethod在参数中接收null

我有一个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)][Tool@R_306_2419@Item(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而不是正确的值.