如何使用这个Web服务?

时间:2011-06-13 18:04:00

标签: asp.net c#-4.0

这是我第一次创建Web服务。我不确定我的实现是否不正确,但我试图像课程一样使用。问题是,当我尝试消费时,我感到困惑,无法设置属性的值。

这是网络服务。

public class Service1 : System.Web.Services.WebService
{
    private bool _isUserActive { get; set; }
    private bool _isCredentialValid { get; set; }
    public string email { get; set; }
    public string pass { get; set; }
    public int customerID { get; set; }

    [WebMethod]
    public bool VerifyUserCredential()
    {
        bool result = false;

        PURLDataContext purl = new PURLDataContext();
        try
        {

            var res = purl.Sel_User(email.ToLower(), pass);

            if (res != null)
                result = true;
            _isUserActive = true;
            _isCredentialValid = true;
        }
        catch (Exception ex)
        {
            if (ex.Message == "Account is inactive, please contact your administrator!")
            {
                _isUserActive = false;
                _isCredentialValid = false;
            }
            else
                _isCredentialValid = false;
            //Invalid credentials.
        }

        return result;
    }

    [WebMethod]
    public ArrayList retrieveCustomerInfo()
    {
        ArrayList customerInfo = new ArrayList();
        string validate = "Please Validate";


        if (_isCredentialValid)
        {
            PURLDataContext purl = new PURLDataContext();
            var customer = purl.Sel_Recipient(customerID);

            foreach (var c in customer)
            {
                customerInfo.Add(c);
            }

        }
        else
            customerInfo.Add(validate);

        return customerInfo;

    }
}

以下是我想要消费的内容。

        PURLServices.Service1SoapClient webserv = new Service1SoapClient();

        bool result;
        ArrayOfAnyType array = new ArrayOfAnyType();

        webserv.email = "email@email.com";
        webserv.pass = "pass";
        webserv.customerID = 12345;

        result = webserv.VerifyUserCredential();
        array = webserv.retrieveCustomerInfo();

感谢您的帮助/

1 个答案:

答案 0 :(得分:0)

您不想尝试使用此类属性。你的方法看起来应该更像这样:

public bool VerifyUserCredential(string userName, string password)
{
 // method body here

}

您可能希望返回服务器将缓存的某种访问令牌。然后可以将其传递给其他方法以显示用户有效。