How to Pass Name/Value Pairs to an ASMX Web Method?

会有一股神秘感。 提交于 2019-12-12 14:11:20

问题


I am creating a webservice that essentially remotes some database calls (so that they happen over HTTP rather than the default SQL Server port of 1433).

Essentially what I have been trying to do is pass a number of parameters to a WebMethod on an ASMX WebService. The parameters are:

  • A string representing the ID of the query to be execute.
  • A DataTable representing the structure of the table that the information should be filled into.
  • A dictionary of parameters (i.e. name value pairs) to be run with the query represented by the ID above to get the expected results.
  • A boolean which is unimportant to this question

Now the problem I am having is that the string for the ID, the DataTable and the boolean are all coming through just fine, but the Dictionary does not. I know that by default, anything implementing IDictionary is not supported by XMLSerializing, so I have converted the dictionary into two object arrays, one for the keys, and one for the values.

What I'm sending through from the Client appears to be correct. The SOAP packet contains the correct values for both the keys array and the values array.

What I'm receiving on the WebService end (I can break in the WebMethod as it is called) is null for both of the arrays. Not an empty array, just null.

I have tried a lot of things in order to get these parameters across to the webservice (coded a SerializableDictionary, attempted to jam the key/value pairs into a data table, attempted to simply use two string arrays and then cast the results back into the correct types on the WebService end) but I've had absolutely no luck whatsoever.

So I suppose in summary, my question is:

Does anyone know a good way to successfully send Name-Value pairs where Name is a string, and Value can be almost any object type, but will always be a serializable object to a WebService WebMethod, such that they Name-Values are correctly interpreted on the service side.


回答1:


I'm not entire sure what the problem was (it may have been my own stupidity) but I started from scratch again, and implemented the solution with 2 arrays (one of keys (strings) and one of objects (values)) and it worked as expected.




回答2:


You mention that the value can be "almost any object type", I suspect that's not going to fly and might be where it's breaking, the items all have to be serializable.

Try using something like this:

http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx



来源:https://stackoverflow.com/questions/4788571/how-to-pass-name-value-pairs-to-an-asmx-web-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!