1

我在 ASMX 文件中有一个 WebService,但是我最终遇到了问题,因为在某些情况下我有太多数据(打印分页网格,检索所有数据)。发现我无法使用 ASMX 服务配置与消息大小相关的任何内容,因此我决定采用 WCF 路线来解决我的问题。

转换我现有的 WebService,我创建了一个接口和实际服务,然后将我的配置添加到我的 WebConfig。到目前为止,一切都很好。转换后,我无法让它运行。我的方法的第一行有一个断点,但它永远不会到达它。

Chrome 在通话中给了我“400(错误格式)”错误,所以我去配置了跟踪。在跟踪中,我看到调用我的方法时引发了异常。

There was an error while trying to deserialize parameter http://tempuri.org/:jsonAOData. Please see InnerException for more details.

(完全例外,法语对不起)

Le serveur a rencontré une erreur lors du traitement de la demande. Le message d'exception est 'Le module de formatage a généré une exception en tentant de désérialiser le message : Une erreur s'est produite en tentant de désérialiser le paramètre http://tempuri.org/:jsonAOData. Le message InnerException était 'Une erreur s'est produite lors de la désérialisation de l'objet de type System.String. Jeton 'null' attendu, mais 'name' a été trouvé.'. Pour plus d'informations, consultez InnerException.'. Pour plus d'informations, consultez les journaux du serveur. La trace de la pile d'exception est :

à System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part) 
à System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameter(XmlDictionaryReader reader, PartInfo part) 
à System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, Object[] parameters, PartInfo returnInfo, Object& returnValue) 
à System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBodyCore(XmlDictionaryReader reader, Object[] parameters, Boolean isRequest) 
à System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest) 
à System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest) 
à System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters) 
à System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) 
à System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) 
à System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) à System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) 
à System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) 
à System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) 
à System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) 
à System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) 
à System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) 
à System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) 
à System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) 
à System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) 
à System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) 
à System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

查看 chrome 中的调用,它指定以下内容:

Request URL:http://localhost:2806/MyWebApp/WebServices.svc/GetRequests
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,fr-CA;q=0.6,fr;q=0.4
Connection:keep-alive
Content-Length:3436
Content-Type:application/json; charset=UTF-8
Cookie:DOKU_PREFS=list%23thumbs; ASP.NET_SessionId=bttjyzhltsrxflcn43i0v43f; MyWebApp=ECFF341038FFA20B7357CAEE592127F98B46F7421F5388A6CCB1D8460A1A66E345D82E62F634BF2854F9008DD01C875B53B3864E3EE3511AFAC67C40AE9BB4013E367D50605B326755CA7F286EF18CD7A3E229EDA2EE8066A12DC330B293AEE549322C7AADC46306CA63402A27F6F125703D070B07EE8BBDA9AC2F185CF546DDEAF1AA6AEE6B42BAFF41BDDC690CDED8
Host:localhost:2806
Origin:http://localhost:2806
Referer:http://localhost:2806/MyWebApp/Viewer.aspx
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
X-Requested-With:XMLHttpRequest

有效负载(我使用 JSONLint 验证):

{"jsonAOData": "[{"name":"sEcho","value":1},{"name":"iColumns","value":33},{"name":"sColumns","value":"FileId,StateDesc,DeptDescription,Division,ConfirmProblem,TaskDescription,GenComment,FileType,Problem,TreatmentTargetDate,TreatmentPlannedDate,Responsable,ReceivedDate,NoCiv,Generic,Streetname,StreetDirection,StreetDescription,TreatmentStartDate,TreatmentCompleteDate,TreatmentDemandDate,SiteDesc,SectorOld,District,AreaOld,IntersectStreetId,Prepose,PrintedDate,Requester,AdressC,LocationPrecision,RealisedFor,Source"},{"name":"iDisplayStart","value":0},{"name":"iDisplayLength","value":-1},{"name":"mDataProp_0","value":"FileId"},{"name":"mDataProp_1","value":"StateDesc"},{"name":"mDataProp_2","value":"DeptDescription"},{"name":"mDataProp_3","value":"Division"},{"name":"mDataProp_4","value":"ConfirmProblem"},{"name":"mDataProp_5","value":"TaskDescription"},{"name":"mDataProp_6","value":"GenComment"},{"name":"mDataProp_7","value":"FileType"},{"name":"mDataProp_8","value":"Problem"},{"name":"mDataProp_9","value":"TreatmentTargetDate"},{"name":"mDataProp_10","value":"TreatmentPlannedDate"},{"name":"mDataProp_11","value":"Responsable"},{"name":"mDataProp_12","value":"ReceivedDate"},{"name":"mDataProp_13","value":"NoCiv"},{"name":"mDataProp_14","value":"Generic"},{"name":"mDataProp_15","value":"Streetname"},{"name":"mDataProp_16","value":"StreetDirection"},{"name":"mDataProp_17","value":"StreetDescription"},{"name":"mDataProp_18","value":"TreatmentStartDate"},{"name":"mDataProp_19","value":"TreatmentCompleteDate"},{"name":"mDataProp_20","value":"TreatmentDemandDate"},{"name":"mDataProp_21","value":"SiteDesc"},{"name":"mDataProp_22","value":"SectorOld"},{"name":"mDataProp_23","value":"District"},{"name":"mDataProp_24","value":"AreaOld"},{"name":"mDataProp_25","value":"IntersectStreetId"},{"name":"mDataProp_26","value":"Prepose"},{"name":"mDataProp_27","value":"PrintedDate"},{"name":"mDataProp_28","value":"Requester"},{"name":"mDataProp_29","value":"AdressC"},{"name":"mDataProp_30","value":"LocationPrecision"},{"name":"mDataProp_31","value":"RealisedFor"},{"name":"mDataProp_32","value":"Source"},{"name":"iSortCol_0","value":0},{"name":"sSortDir_0","value":"asc"},{"name":"iSortingCols","value":1},{"name":"bSortable_0","value":true},{"name":"bSortable_1","value":true},{"name":"bSortable_2","value":true},{"name":"bSortable_3","value":true},{"name":"bSortable_4","value":true},{"name":"bSortable_5","value":true},{"name":"bSortable_6","value":true},{"name":"bSortable_7","value":true},{"name":"bSortable_8","value":true},{"name":"bSortable_9","value":true},{"name":"bSortable_10","value":true},{"name":"bSortable_11","value":true},{"name":"bSortable_12","value":true},{"name":"bSortable_13","value":true},{"name":"bSortable_14","value":true},{"name":"bSortable_15","value":true},{"name":"bSortable_16","value":true},{"name":"bSortable_17","value":true},{"name":"bSortable_18","value":true},{"name":"bSortable_19","value":true},{"name":"bSortable_20","value":true},{"name":"bSortable_21","value":true},{"name":"bSortable_22","value":true},{"name":"bSortable_23","value":true},{"name":"bSortable_24","value":true},{"name":"bSortable_25","value":true},{"name":"bSortable_26","value":true},{"name":"bSortable_27","value":true},{"name":"bSortable_28","value":true},{"name":"bSortable_29","value":true},{"name":"bSortable_30","value":true},{"name":"bSortable_31","value":true},{"name":"bSortable_32","value":true}]"}

作为参考,我还包括一些配置/代码片段来帮助解决它:

我的(部分)jQuery(DataTables)初始化

sAjaxSource: "WebServices.svc/GetRequests",
fnServerData: function (sSource, aoData, fnCallback) {
    var jsonAOData = JSON.stringify(aoData);
    $.ajax({
        type: "POST",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        url: sSource,
        data: '{"jsonAOData": "' + jsonAOData + '"}',
        success: function (msg) {
            fnCallback(msg.d);
            }
        });
    }

我在 IWebServices 中的方法定义:

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
FormatedList GetRequests(string jsonAOData);

它是 WebServices.cs 中的实现(它周围没有属性/装饰),WebServices.svc 继承了它。

public FormatedList GetRequests(string jsonAOData)
{
    // My code that was working before but never gets call now.
}

我的网络配置

<system.serviceModel>
   <behaviors>
      <serviceBehaviors>
         <behavior name="WebServicesBehavior">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="true"/>
         </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
         <behavior name="WebServicesAspNetAjaxBehavior">
            <webHttp/>
         </behavior>
      </endpointBehaviors>
   </behaviors>
   <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
   <services>
      <service behaviorConfiguration="WebServicesBehavior" name="WebServices">
         <endpoint address="" behaviorConfiguration="WebServicesAspNetAjaxBehavior"
            binding="webHttpBinding" contract="IWebServices" />
      </service>
   </services>
</system.serviceModel>

知道我缺少什么吗?我确信我已经接近我的目标,只是无法掌握最后一点......

谢谢!

4

1 回答 1

2

问题是您的 JSON 数据无效。

{"jsonAOData": "[{"name":"sEcho"...

JSON 字符串以"字符开头并以匹配"字符结尾(除非它被转义为\")。您的字符串从那里开始[{并结束。之后的字符无效 ( name)。

如果您希望字符串按原样传递,您实际上需要“重新 JSONify”它。首先,您将创建一个要传递给服务的 JSON 字符串 ( jsonAOData)。然后,您将创建一个新对象,该对象将传递给 WCF 服务(并且该对象也需要进行 JSON 化) - 见下文。

var jsonAOData = JSON.stringify(aoData);
var inputBody = JSON.stringify({ jsonAOData: jsonAOData });
$.ajax({
    type: "POST",
    dataType: 'json',
    contentType: "application/json; charset=utf-8",
    url: sSource,
    data: inputBody,
    success: function (msg) {
        fnCallback(msg.d);
        }
    });
}
于 2012-11-13T19:16:08.693 回答