简体   繁体   English

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

[英]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" 1.类库项目 C#,我创建了“客户类库项目”,该项目只有一个名为“ Customer.cs”的类文件。

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

2. Web Service Project 2. Web服务项目

I have added CustomerClassLibrary dll under the reference folder 我在参考文件夹下添加了CustomerClassLibrary dll

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) 3.客户端应用程序(C#Windows应用程序)

Added CustomerClassLibrary dll under the reference 在参考下添加了CustomerClassLibrary dll

Added WebService reference under the Service Reference folder (Created Proxy class) 在“服务引用”文件夹下添加了WebService引用(创建的代理类)

var ss = new SRMUserRegServiceReference.SRMUserRegistrationSoapClient();

Button 1 Click: 按钮1单击:

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

When I execute this , I can able retrieve "Hello World status Successfully. Everything looks good. 执行此操作后,我可以成功检索“ Hello World状态。一切看起来不错。

Button 2 click : 按钮2单击:

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. 在这里,当我尝试在GetAllTickets(C)方法中传递“客户C对象”时,出现错误。

Can you please guide me how do I pass "Single Object" through the web service? 您能否指导我如何通过Web服务传递“单个对象”?

Two Errors, I am getting : 两个错误,我得到:

Error 1 错误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 “ ClientApplicationCallWebService.SRMUserRegServiceReference.SRMUserRegistrationSoapClient.GetAllTickets(ClientApplicationCallWebService.SRMUserRegServiceReference.Customer)”的最佳重载方法匹配具有一些无效的参数Testing \\ ClientApplicationCallWebService \\ ClientApplicationCallWebService \\ Form1.cs 50 34 ClientApplicationCallWebService

Error 2 错误2

Argument 1: cannot convert from 'CustomerClassLibrary.Customer' to 'ClientApplicationCallWebService.SRMUserRegServiceReference.Customer' Testing\\ClientApplicationCallWebService\\ClientApplicationCallWebService\\Form1.cs 50 51 ClientApplicationCallWebService 参数1:无法从“ CustomerClassLibrary.Customer”转换为“ 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. 最后,我找到了答案,下面我为Customer Class创建了实例,现在我可以传递对象了。 Thanks all 谢谢大家

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

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

相关问题 通过JSON对象将Session对象中的Byte Array传递给Web Service(Asp.Net 2.0 asmx) - Pass Byte Array that was in Session object to a Web Service (Asp.Net 2.0 asmx) through a JSON object 如何通过ASP.NET网站中的WCF服务传递值 - How to pass values through a WCF Service in a ASP.NET Website 如何在ASP.Net中发布Web服务? - How to publish web service in ASP.Net? ASP.NET Web服务通过会话传递表单数据 - ASP.NET Web Service pass form data with session 通过Web服务将数据发送到ASP.net MVC中的数据库 - Sending data through web service to databse in ASP.net MVC 如何使用asp.net MVC 4将参数传递给SOAP Web服务 - How to pass parameters to SOAP web service using asp.net mvc 4 如何将对象列表从WCF服务传递到ASP.NET Web应用程序 - How do I pass a list of objects from a WCF service to an ASP.NET web app 如何通过Windows Phone 7将数据传递给Asp.net Web API - How to pass data to Asp.net Web API through Windows Phone 7 在ASP.Net Web服务的构造函数中使用带有参数的自定义对象 - Using custom object with parameters in the constructor in ASP.Net web service 在 asp.net web 服务中没有为此对象错误定义无参数构造函数 - No parameterless constructor defined for this object error in asp.net web service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM