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