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

时间:2017-09-05 14:46:44

标签: c# asp.net web-services object

1。类库项目 C#,我创建了Customer Class Library Project,它只有一个名为“Customer.cs”的类文件

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

2。网络服务项目

我在参考文件夹

下添加了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

在Service Reference文件夹(Created Proxy class)下添加了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)方法中传递“Customer C对象”时,我收到错误。

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

两个错误,我得到了:

错误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

2 个答案:

答案 0 :(得分:0)

尝试:

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

string returnString =ss.GetAllTickets(C);

Console.WriteLine(returnString); 

答案 1 :(得分:0)

            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();