我可以将非原始数据类型传递给 WebMethod 吗?
我遇到一个问题,我试图将非原始(和用户定义的)数据类型传递到 WebMethod 中。有某种方法可以做到这一点吗?这是我的代码示例:
[WebMethod]
public bool GetTableRecordCnt(int i, String str, DateTime? lastUpdatedDate, UserDefinedType udt, out FDT_SCHEDULER_STATUS[] schedulerTable)
{
//code
}
当我尝试从客户端应用程序调用此函数时,出现以下错误:
“无法从传输连接读取数据。”
如果我用原始数据类型(例如 int)替换 UserDefineType 参数,客户端就能够从 WebMethod 获取响应。
预先感谢您的帮助。
编辑:
从客户端应用程序调用代码:
UserDefinedType udt = new UserDefinedType();
UserDefinedType1[] tableRecords = ThisApplication.FillArray();
bool result = WebServiceReferenceName.GetTableRecordCnt(1, "tableName", "10/10/2010 12:00:00", udt, out tableRecords);
这是对传递给 Web 方法的参数的严重过度简化,但每个参数中的数据就是要传递的数据。
I am having an issue where I am attempting to pass a non-primitive (and user defined) data type into a WebMethod. Is there a certain way to do this? Here is an example of my code:
[WebMethod]
public bool GetTableRecordCnt(int i, String str, DateTime? lastUpdatedDate, UserDefinedType udt, out FDT_SCHEDULER_STATUS[] schedulerTable)
{
//code
}
When I try to call this function from my client application I get the following error:
"Unable to read data from the transport connection."
If I replace the UserDefineType parameter with a primitive data type (an int for example) the client is able to get a response from the WebMethod.
Thanks in advance for you help.
EDIT:
Calling code from client application:
UserDefinedType udt = new UserDefinedType();
UserDefinedType1[] tableRecords = ThisApplication.FillArray();
bool result = WebServiceReferenceName.GetTableRecordCnt(1, "tableName", "10/10/2010 12:00:00", udt, out tableRecords);
That is a gross oversimplification of the parameters that are being passed to the web method, but the data that is in each parameter is what would be passed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,你可以。您需要创建一个与 UserDefinedType 的公共属性相匹配的对象。如果您使用 .Net Web 服务并将该方法标记为 [ScriptMethod()],然后该方法将响应 JSON,那么这相当简单。
下面是一个示例:
您可以使用以下 javascript 将其作为参数传递:
注意,您必须声明作为字符串传递的 JSON 对象,否则如果您使用 jQuery.ajax(...) ,它将序列化您的参数对象到编码的参数字符串,而不是将其作为本机 JSON 字符串传递。
还有一些不错的库可以为您处理 JSON 数据对象字符串,例如 jquery-json 2.2。使用它,您可以简单地将参数作为 $.toJSON(param) 传递。
最后,下面是使用 jquery 将请求发送到 ASP .Net Web 服务的示例:
Yes, you can. You would need to create an object which matched the public properties of your UserDefinedType. This is fairly simply if you are using .Net webservices and have marked the method as [ScriptMethod()] which will then respond to JSON.
Here's an example:
Which you could pass as a param using the following javascript:
Note, you have to declare the JSON object being passed as a string as otherwise if you are using jQuery.ajax(...) it will serialise your param object to an encoded param string rather than pass it as a native JSON string.
There are also some nice libraries out there that will take care of the JSON data object to string for you such as jquery-json 2.2. Using this you can then simply pass the param as $.toJSON(param).
Finally, here's an example of sending the request to an ASP .Net web service using jquery:
现在我自己的问题有了答案。我已将 Web 服务方法签名更改为:
请注意,我将 UserDefinedType 类型更改为 object。现在我可以让我的客户与我的网络服务对话。有人有理由认为不应该这样做吗?
For now I have an answer to my own question. I have changed the web service method signature to:
Notice that I changed the UserDefinedType type to object. Now I am able to get my client to talk to my web service. Does anyone have any reason that this should not be done?
这解释了您的问题:将自定义对象传递到 Web 服务
可能您的 UserDefinedType 包含“花哨”的内容,例如列表、字典或无法序列化的内容,因此您会收到错误。
我很惊讶您可以像这样传递 UserDefinedType,确保您获得所需的所有属性,我怀疑您会为非原始部分获得很多空值。
This explain your problem: Passing a custom object to the web service
Probably your UserDefinedType contains "fancy" stuff like List, Dictionary or something that can't be serialized thus you get error.
I'm surprised that you can pass the UserDefinedType like this, make sure you get all the properties you need, I suspect you'll get lot of null values for the non primitive parts.
是的,只要您的类型是可序列化。
Yes this is possible as long as your type is serializable.
好的,找出问题所在终于让我松了口气 =)
显然,当您将 UserDefinedType 传递给 Web 方法时,变量可以命名的字符数是有限制的。在对我在代码中为这个新函数添加的每个部分进行测试之后。我发现缩短以下变量名称:
to:
现在允许在我的 Windows Mobile 应用程序和 C# Web 服务之间传递数据。去图吧!
现在,有谁知道可用于命名将传递给 Web 方法的 UserDefinedType 中的变量的字符数是否有特定的数字限制,或者这可能是我的环境问题?
OK, so finding out what the problem is has finally given me some relief =)
Apparently there is a limit to the number of characters that a variable can be named when you pass a UserDefinedType to a web method. After testing with each piece that I added for this new function in my code. I found that shortening the following variable name:
to:
is now allowing data to be passed between my windows mobile application and the c# web service. GO FIGURE!
Now, does anyone know if there is a specific numeric limit to the number of characters that can be used to name a variable in a UserDefinedType that will be passed to a Web Method, or could this be an environment issue on my end?