0

I'm trying to create and consume a web service... I'm using .NET Framework 4.0 (c#). The service exposes a method like this:

  public List<object[]> GetData(string strRegion, List<string> lstBrand, List<string> lstColor)

Then on the client application, I declare a list of objects:

  List<object[]> lst = new List<object[]>();

... and attempt to fill it like so:

  MyService.MyClient os = new MyClient();
  lst = os.GetData(myRegionString, myBrandList, myColorList);

... but I get, "Cannot implicity convert type 'object[][]' to 'System.Collections.Generic.List'". What am I doing wrong?

Thanks!


JSON not working with Wookmark

When i try to execute the following script..i get an error .I am trying to receive data using JSON.

function loadData() {
      isLoading = true;
      $('#loaderCircle').show();
      alert("OOOOO");
      $.ajax({

          url: apiURL,
          dataType: 'json',
          data: { page: page }, // Page parameter to make sure we load new data
          success: function(data) {
              isLoading = false;
              $('#loaderCircle').hide();

              // Increment page index for future calls.
              page++;

              // Create HTML for the images.
              var html = '';
              var i = 0, length = data.length, image;
              alert(data.length);
              for (; i < length; i++) {
                  image = data[i];

                  html += '<li class="polaroid"><div class="optionbg"></div><div class="optionback"><span>Necklaces</span></div><div class="options"><span class="favs">14</span><span class="fav">like it!</span></div><a href="http://www.google.co.in"><img src="' + image.preview + '" width="180" height="' + Math.round(image.height / image.width * 180) + '"></a></li>';

              }

              // Add image HTML to the page.
              $('#tiles').append(html);

              // Apply layout.
              applyLayout();
          },
          error: function(result) {
              $('#qqq').text(JSON.stringify(result));
              //alert(JSON.stringify(result));
          }
      });
    };

Webservice:

 <WebMethod(Description:="Gets the Categories")> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function GetCategories() As String
        Dim details As New List(Of Nodes)()

        Using ds As Data.DataSet = db.ExecuteDataSet(CommandType.Text, "SELECT NodeID,NodeName FROM Nodes WHERE ParentID='1'")
            Dim JaggedArray As String()() = New String(ds.Tables(0).Rows.Count - 1)() {}
            Dim i As Integer = 0
            For Each rs As DataRow In ds.Tables(0).Rows
                Dim node As New Nodes()
                node.NodeId = rs("NodeId").ToString
                node.NodeName = rs("NodeName").ToString
                details.Add(node)
                'JaggedArray(i) = New String() {rs("NodeName").ToString(), rs("NodeID").ToString()}
                i = i + 1
            Next
        End Using
        Dim js As New JavaScriptSerializer()
        Dim strJSON As String = js.Serialize(details.ToArray)
        Return strJSON
    End Function

JSON Output:

<string xmlns="http://localhost:54511/"> [{"NodeId":"BK01","NodeName":"Books","ParentId":null,"Likes":null},{"NodeId":"CO01","NodeName":"Computers","ParentId":null,"Likes":null},{"NodeId":"GA01","NodeName":"Gaming","ParentId":null,"Likes":null},{"NodeId":"MO01","NodeName":"Mobile & Accessories","ParentId":null,"Likes":null}] </string>

Error:

{"readyState":4,"responseText":"<html>\r\n <head>\r\n <title>Request format is unrecognized for URL unexpectedly ending in '/GetCategories'.</title>\r\n <style>\r\n body {font-family:\"Verdana\";font-weight:normal;font-size: .7em;color:black;} \r\n p {font-family:\"Verdana\";font-weight:normal;color:black;margin-top: -5px}\r\n b {font-family:\"Verdana\";font-weight:bold;color:black;margin-top: -5px}\r\n H1 { font-family:\"Verdana\";font-weight:normal;font-size:18pt;color:red }\r\n H2 { font-family:\"Verdana\";font-weight:normal;font-size:14pt;color:maroon }\r\n pre {font-family:\"Lucida Console\";font-size: .9em}\r\n .marker {font-weight: bold; color: black;text-decoration: none;}\r\n .version {color: gray;}\r\n .error {margin-bottom: 10px;}\r\n .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }\r\n </style>\r\n </head>\r\n\r\n <body bgcolor=\"white\">\r\n\r\n <span><H1>Server Error in '/EntLib5' Application.<hr width=100% size=1 color=silver></H1>\r\n\r\n <h2> <i>Request format is unrecognized for URL unexpectedly ending in '/GetCategories'.</i> </h2></span>\r\n\r\n <font face=\"Arial, Helvetica, Geneva, SunSans-Regular, sans-serif \">\r\n\r\n <b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.\r\n\r\n <br><br>\r\n\r\n <b> Exception Details: </b>System.InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/GetCategories'.<br><br>\r\n\r\n <b>Source Error:</b> <br><br>\r\n\r\n <table width=100% bgcolor=\"#ffffcc\">\r\n <tr>\r\n <td>\r\n <code>\r\n\r\nAn unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</code>\r\n\r\n </td>\r\n </tr>\r\n </table>\r\n\r\n <br>\r\n\r\n <b>Stack Trace:</b> <br><br>\r\n\r\n <table width=100% bgcolor=\"#ffffcc\">\r\n <tr>\r\n <td>\r\n <code><pre>\r\n\r\n[InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/GetCategories'.]\r\n System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +405913\r\n System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212\r\n System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +47\r\n System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +193\r\n System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +93\r\n System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155\r\n</pre></code>\r\n\r\n </td>\r\n </tr>\r\n </table>\r\n\r\n <br>\r\n\r\n <hr width=100% size=1 color=silver>\r\n\r\n <b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:2.0.50727.4971; ASP.NET Version:2.0.50727.4971\r\n\r\n </font>\r\n\r\n </body>\r\n</html>\r\n<!-- \r\n[InvalidOperationException]: Request format is unrecognized for URL unexpectedly ending in '/GetCategories'.\r\n at System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response)\r\n at System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath)\r\n at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)\r\n at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)\r\n at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()\r\n at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)\r\n-->","status":500,"statusText":"Internal Server Error"}

4

4 に答える 4

3

ソリューション エクスプローラーで、サービスを右クリックして構成を行います。データ コレクションのデフォルト タイプを配列に設定している可能性があります。リスト型に設定できます。objectまた、 Web サービスを介して送信することを再考します。通常、適切に定義されたデータ型が必要です。objectWeb サービスを介してタイプを送信することさえできないと思います。

于 2012-09-17T19:16:32.800 に答える
2

試す

MyService.MyClient os = new MyClient();
  lst = os.GetData(myRegionString, myBrandList, myColorList).ToList();

リストは Web サービスからクライアントに渡されるときに配列にシリアル化されるため、再度リストに変換する必要があります。

于 2012-09-17T19:16:06.293 に答える
0

明らかに、object[] の一般的なリストではなく、ギザギザの配列を返します。1 つの解決策が既に提案されています。戻り値の型を変更するだけです。しかし、私がより良いと思うのは、あらゆる場所でインターフェイスを使用することです-配列とジェネリックリストの両方がIListを実装しているため、メソッドがIList型の値を返すようにし、lst変数もIListである場合は、そうすべきではありませんメソッドがギザギザの配列を返す場合でも問題があります。

于 2012-09-17T21:26:21.517 に答える
0

MyClient.GetData() メソッドがギザギザの配列を返しています。この配列をリストに変換するには、System.Linq 名前空間の ToList() メソッドを使用できる必要があります。

lst = os.GetData(myRegionString, myBrandList, myColorList).ToList();
于 2012-09-17T19:16:52.593 に答える