繁体   English   中英

如何通过asp.net Web服务传递对象

[英]how to pass object through asp.net web service

1.类库项目 C#,我创建了“客户类库项目”,该项目只有一个名为“ Customer.cs”的类文件。

namespace CustomerClassLibrary
{
    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

2. Web服务项目

我在参考文件夹下添加了CustomerClassLibrary dll

创建了两种方法

[WebMethod]
public string HelloWorld()
{
    return "Hello World";
}

[WebMethod]
public string GetAllTickets(CustomerClassLibrary.Customer C )
{
    string statuscode;

    if (C.FirstName=="JOHN")
    {
        statuscode = "success"; 
    }
    else{
        statuscode = "failure";
    }


 return statuscode;
}

3.客户端应用程序(C#Windows应用程序)

在参考下添加了CustomerClassLibrary dll

在“服务引用”文件夹下添加了WebService引用(创建的代理类)

var ss = new SRMUserRegServiceReference.SRMUserRegistrationSoapClient();

按钮1单击:

string status = ss.HelloWorld();
Console.WriteLine(status); 

执行此操作后,我可以成功检索“ Hello World状态。一切看起来不错。

按钮2单击:

CustomerClassLibrary.Customer C;              
C.FirstName = "John";
C.LastName = "TEST";

string returnString =ss.GetAllTickets(C);

Console.WriteLine(returnString); 

在这里,当我尝试在GetAllTickets(C)方法中传递“客户C对象”时,出现错误。

您能否指导我如何通过Web服务传递“单个对象”?

两个错误,我得到:

错误1

“ ClientApplicationCallWebService.SRMUserRegServiceReference.SRMUserRegistrationSoapClient.GetAllTickets(ClientApplicationCallWebService.SRMUserRegServiceReference.Customer)”的最佳重载方法匹配具有一些无效的参数Testing \\ ClientApplicationCallWebService \\ ClientApplicationCallWebService \\ Form1.cs 50 34 ClientApplicationCallWebService

错误2

参数1:无法从“ CustomerClassLibrary.Customer”转换为“ ClientApplicationCallWebService.SRMUserRegServiceReference.Customer” Testing \\ ClientApplicationCallWebService \\ ClientApplicationCallWebService \\ Form1.cs 50 51 ClientApplicationCallWebService

尝试:

CustomerClassLibrary.Customer C =  new CustomerClassLibrary.Customer();              
C.FirstName = "John";
C.LastName = "TEST";

string returnString =ss.GetAllTickets(C);

Console.WriteLine(returnString); 
            ClientApplicationCallWebService.SRMUserRegServiceReference.Customer C = new ClientApplicationCallWebService.SRMUserRegServiceReference.Customer(); 


        C.FirstName = "JOHN";
        C.LastName = "TEST";

        string returnString =ss.GetAllTickets(C);

        Console.WriteLine(returnString); 

最后,我找到了答案,下面我为Customer Class创建了实例,现在我可以传递对象了。 谢谢大家

        ClientApplicationCallWebService.SRMUserRegServiceReference.Customer C = new ClientApplicationCallWebService.SRMUserRegServiceReference.Customer(); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM