开发者

Can I pass a non-primitive data type to a WebMethod?

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 paramete开发者_如何学编程r 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.


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');
        }
    });


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?


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜