【问题标题】:how to pass object through asp.net web service如何通过asp.net Web服务传递对象
【发布时间】:2017-09-06 00:19:18
【问题描述】:

1.类库项目 C#,我创建了客户类库项目,它只有一个名为“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文件夹下添加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 对象时,我收到错误。

你能指导我如何通过网络服务传递“单一对象”吗?

两个错误,我得到了:

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

【问题讨论】:

  • 您遇到什么错误?还尝试将您的类 Customer 标记为 [Serializable]
  • 您的代码甚至无法编译 - GetAllTickets() 不返回任何内容。
  • 在方法结束时返回 statusCode。在方法中设置断点会发生什么?
  • 当我尝试执行客户端应用程序时,出现两个错误。
  • 嘿 Isma,请指导我如何将 Customer 类标记为 [Serializable]

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


【解决方案1】:

试试:

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

string returnString =ss.GetAllTickets(C);

Console.WriteLine(returnString); 

【讨论】:

  • 错误 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
  • 我收到上述错误。
  • 我尝试了上面的代码,仍然得到同样的错误。
  • 您的代码中似乎存在双重引用问题。使用 ss.CustomerClassLibrary.Customer c = new ss.CustomerClassLibrary.Customer(); 初始化您的客户对象
【解决方案2】:
            ClientApplicationCallWebService.SRMUserRegServiceReference.Customer C = new ClientApplicationCallWebService.SRMUserRegServiceReference.Customer(); 


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

        string returnString =ss.GetAllTickets(C);

        Console.WriteLine(returnString); 

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2011-12-09
    • 1970-01-01
    • 2014-11-15
    相关资源
    最近更新 更多