简体   繁体   English

如何将对象传递给Web服务?

[英]How to pass an object to a web service?

I already searched a lot in Google. 我已经在Google中搜索了很多内容。 I created a EntityClass on client side, and then I added the library reference of this class on Web Service side. 我在客户端创建了一个EntityClass,然后在Web服务端添加了该类的库引用。 But when I want to call the method, it shows this error: 但是当我想调用该方法时,它显示此错误:

Error 2 Argument 1: cannot convert from 'Services_Library.UserService.UserServiceSoapClient' to 'Services_Library.UserService.UserEntity' 错误2参数1:无法从“ Services_Library.UserService.UserServiceSoapClient”转换为“ Services_Library.UserService.UserEntity”

here is the code, this method is called from a User Interface: 这是代码,从用户界面调用此方法:

public UserEntity test(UserEntity userEntityx)
    {
        UserService.UserServiceSoapClient userService = new UserService.UserServiceSoapClient();
        userService.testUserAsync(new UserEntity());
    }

I think we can do this without explicit serialization, right? 我认为我们可以在没有显式序列化的情况下做到这一点,对吗? If so, I prefer this way. 如果是这样,我更喜欢这种方式。

I think the problem is when you actually call the service, you're passing in the serviceReference and not the object that the call accepts. 我认为问题在于,当您实际调用服务时,您传递的是serviceReference而不是调用接受的对象。 I think it should look something like: 我认为它应该类似于:

public UserEntity test(UserEntity userEntityX)
{
    var userService = new UserService.UserServiceSoapClient();
    return userService.testUser(userEntityX);
}

No explicit serialization needed. 无需显式序列化。

Also, keep in mind that if you're calling the Async version of the method you're code is going to become more complicated. 另外,请记住,如果要调用方法的异步版本,则代码将变得更加复杂。 I used the synchronous version in my example. 我在示例中使用了同步版本。

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

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