我可以将非原始数据类型传递给 WebMethod 吗?

发布于 2024-10-02 01:27:31 字数 753 浏览 1 评论 0原文

我遇到一个问题,我试图将非原始(和用户定义的)数据类型传递到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

删除→记忆 2024-10-09 01:27:31

是的,你可以。您需要创建一个与 UserDefinedType 的公共属性相匹配的对象。如果您使用 .Net Web 服务并将该方法标记为 [ScriptMethod()],然后该方法将响应 JSON,那么这相当简单。

下面是一个示例:

public class UserDefinedType
{
   public int Property1 { get; set; }
   public string Property2 { get; set; }
}

您可以使用以下 javascript 将其作为参数传递:

var param = "{ Property1 : '"+prop1Val+"', Property2 : '"+prop2Val+"'}";

注意,您必须声明作为字符串传递的 JSON 对象,否则如果您使用 jQuery.ajax(...) ,它将序列化您的参数对象到编码的参数字符串,而不是将其作为本机 JSON 字符串传递。

还有一些不错的库可以为您处理 JSON 数据对象字符串,例如 jquery-json 2.2。使用它,您可以简单地将参数作为 $.toJSON(param) 传递。

最后,下面是使用 jquery 将请求发送到 ASP .Net Web 服务的示例:

$.ajax({
        type: "POST",
        url: "/Services/YourService.asmx/YourMethod",
        cache: false,
        data: $.toJSON(param), // Convert JSON object to String for Post
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            HandleResponse(response.d)
        },
        error: function (e) {
            alert('error during web service call');
        }
    });

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:

public class UserDefinedType
{
   public int Property1 { get; set; }
   public string Property2 { get; set; }
}

Which you could pass as a param using the following javascript:

var param = "{ Property1 : '"+prop1Val+"', Property2 : '"+prop2Val+"'}";

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:

$.ajax({
        type: "POST",
        url: "/Services/YourService.asmx/YourMethod",
        cache: false,
        data: $.toJSON(param), // Convert JSON object to String for Post
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            HandleResponse(response.d)
        },
        error: function (e) {
            alert('error during web service call');
        }
    });
知足的幸福 2024-10-09 01:27:31

现在我自己的问题有了答案。我已将 Web 服务方法签名更改为:

[WebMethod]
public bool GetTableRecordCnt(int i, String str, DateTime? lastUpdatedDate, object udt, out FDT_SCHEDULER_STATUS[] schedulerTable)
{
    //code
}

请注意,我将 UserDefinedType 类型更改为 object。现在我可以让我的客户与我的网络服务对话。有人有理由认为不应该这样做吗?

For now I have an answer to my own question. I have changed the web service method signature to:

[WebMethod]
public bool GetTableRecordCnt(int i, String str, DateTime? lastUpdatedDate, object udt, out FDT_SCHEDULER_STATUS[] schedulerTable)
{
    //code
}

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?

天涯离梦残月幽梦 2024-10-09 01:27:31

这解释了您的问题:将自定义对象传递到 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.

有深☉意 2024-10-09 01:27:31

是的,只要您的类型是可序列化。

Yes this is possible as long as your type is serializable.

爱要勇敢去追 2024-10-09 01:27:31

好的,找出问题所在终于让我松了口气 =)

显然,当您将 UserDefinedType 传递给 Web 方法时,变量可以命名的字符数是有限制的。在对我在代码中为这个新函数添加的每个部分进行测试之后。我发现缩短以下变量名称:

SchedulerRecordCount

to:

SchedRecCnt

现在允许在我的 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:

SchedulerRecordCount

to:

SchedRecCnt

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文