简体   繁体   中英

how to pass object through asp.net web service

1. Class Library Project C# , I have created Customer Class Library Project which has only one class file called "Customer.cs"

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

2. Web Service Project

I have added CustomerClassLibrary dll under the reference folder

Created two methods

[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. Client Application (C# Windows application)

Added CustomerClassLibrary dll under the reference

Added WebService reference under the Service Reference folder (Created Proxy class)

var ss = new SRMUserRegServiceReference.SRMUserRegistrationSoapClient();

Button 1 Click:

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

When I execute this , I can able retrieve "Hello World status Successfully. Everything looks good.

Button 2 click :

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

string returnString =ss.GetAllTickets(C);

Console.WriteLine(returnString); 

Here, When I tried to pass "Customer C object in GetAllTickets(C) methods, I am getting error.

Can you please guide me how do I pass "Single Object" through the web service?

Two Errors, I am getting :

Error 1

The best overloaded method match for 'ClientApplicationCallWebService.SRMUserRegServiceReference.SRMUserRegistrationSoapClient.GetAllTickets(ClientApplicationCallWebService.SRMUserRegServiceReference.Customer)' has some invalid arguments Testing\\ClientApplicationCallWebService\\ClientApplicationCallWebService\\Form1.cs 50 34 ClientApplicationCallWebService

Error 2

Argument 1: cannot convert from 'CustomerClassLibrary.Customer' to 'ClientApplicationCallWebService.SRMUserRegServiceReference.Customer' Testing\\ClientApplicationCallWebService\\ClientApplicationCallWebService\\Form1.cs 50 51 ClientApplicationCallWebService

Try :

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

Finally, I found the answer, I created instance for Customer Class the below, now I can able to pass the object. Thanks all

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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