简体   繁体   English

将对象或结构传递到.asmx Web服务

[英]Passing Objects or Structs to .asmx Web Service

Do you know of a good way to pass objects or structs from a winform app to .asmx web service? 您是否知道将对象或结构从Winform应用程序传递到.asmx Web服务的好方法? It seems like this should be really easy in .NET but I'm not seeing it. 在.NET中,这似乎应该非常容易,但我没有看到。

Rick Strahl had the same question in 2004 but it looks like what I'm looking for maybe doesn't exist: 里克·斯特拉尔(Rick Strahl)在2004年曾提出过同样的问题,但似乎我所寻找的也许并不存在:

http://www.west-wind.com/weblog/posts/2004/Apr/18/Net-Web-Services-and-passing-objects-and-Data http://www.west-wind.com/weblog/posts/2004/Apr/18/Net-Web-Services-and-passing-objects-and-Data

Basically, I just have a simple struct with 10 string or int members. 基本上,我只有一个具有10个字符串或整数成员的简单结构。 I want to send it to an asmx web service and keep the strong-typing intact. 我想将其发送到asmx Web服务,并保持强类型。 That's it. 而已。

I've tried defining the struct on the client and the service. 我尝试在客户端和服务上定义结构。 Then I created a DLL with the struct definition in it and included that in both my web service and client. 然后,我创建了一个带有结构定义的DLL,并将其包含在我的Web服务和客户端中。 Still had issues b/c the namespaces were different. 仍然存在问题b / c,名称空间有所不同。

I see in posts like the following that it seems people are doing what I'm describing. 我在类似以下的帖子中看到,似乎人们在做我正在描述的事情。 I just don't know how: 我只是不知道如何:

Passing Objects Via Web Service 通过Web服务传递对象

I could convert the service to WCF if that makes this any easier -- will probably convert it in a few months anyway. 如果可以轻松地将服务转换为WCF,则可能会在几个月后将其转换。

I don't know why I didn't think of it earlier but Jeremy's response helped out. 我不知道为什么我没有早些想到它,但杰里米的回应有所帮助。 Instead of creating the definition of the object on the client and on the service, I just made the definition of the struct on the web service public. 我没有在客户端和服务上创建对象的定义,而只是在Web服务上公开了结构的定义。 When I create a struct, I just make sure it's of type global::Webservice.com.mydomain.MyStruct. 创建结构时,只需确保其类型为global :: Webservice.com.mydomain.MyStruct。

What have you tried? 你尝试了什么? What errors do you run into? 您遇到什么错误? How does your code look like? 您的代码看起来如何?

However, you might need to mark your class of which you want to pass instances to the web services as [Serializable] ( MSDN ) like this: 但是,您可能需要将您想要将实例传递给Web服务的类标记为[Serializable]MSDN ),如下所示:

[Serializable] 
public class MyObject
{ 
    // Your properties here
    // Your parameterless constructor
}

Note that everything except a default (parameterless) constructor and the properties will be removed during serialization. 请注意,除了默认(无参数)构造函数和属性之外的所有内容都将在序列化过程中删除。 This is because when passing objects to a web service you are actually not passing .NET objects but (serialized) data. 这是因为将对象传递给Web服务时,实际上不是传递.NET对象,而是传递(序列化的)数据。 The corresponding types should be available through the service contract. 相应的类型应可通过服务合同获得。

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

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