WCF 在尝试反序列化发布的数据时抛出 NetDispatcherFaultException

WCF throwing NetDispatcherFaultException when trying to deserialize posted data

我正在处理一个面试项目,该项目必须调用我创建的简单 WCF。我以前从未使用过这些,所以我有点迷路了。我昨晚花了几个小时试图让它工作,现在已经缩小到序列化错误。

这是我正在使用的 WCF。

[ServiceContract(Namespace = "HighEnergy.Web")]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CustomerService {
    private const string ACCT   = "9999999999";
    private const string PHONE  = "111-111-1111";

    [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    public bool Validate(OutageModel model) {
        if (!string.IsNullOrWhiteSpace(model.AccountNumber) && !string.IsNullOrWhiteSpace(model.PhoneNumber)) {
            if (string.IsNullOrWhiteSpace(model.AccountNumber)) {
                model.AccountNumber = ACCT;
            }
            if (string.IsNullOrWhiteSpace(model.PhoneNumber)) {
                model.PhoneNumber = PHONE;
            }

            return model.AccountNumber == ACCT &&
                   model.PhoneNumber == PHONE;
        }

        return false;
    }
}

这是我用于通话的OutageModel

[Serializable]
public class OutageModel {
    [RegularExpression(Utilities.AccountNumberRegex, ErrorMessage = "Your account number is 10 digits.")]
    public string AccountNumber { get; set; }
    [Phone(ErrorMessage = "Your phone number must be in 123-456-7890 format.")]
    public string PhoneNumber   { get; set; }

    public OutageModel() {
        this.Clear();
    }

    private void Clear() {
        this.AccountNumber  = string.Empty;
        this.PhoneNumber    = string.Empty;
    }
}

下面是我尝试从我的 MVC 页面调用它的方式。我没有将它连接到任何按钮事件或任何东西,只是坐在页面底部 运行 因为它被解析。

$.ajax({
        url: '/CustomerService.svc/Validate',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: {
            "AccountNumber": "9999999999",
            "PhoneNumber": "111-111-1111"
        }
    }
    ).done(function (data) {
        alert(data);
    }).error(function (xhr, status, error) {
        console.log(xhr.responseText);
        //alert("Error\n-----\n" + xhr.status + '\n' + xhr.responseText);
    });

web.config相关栏目:

<system.serviceModel>
  <diagnostics>
    <messageLogging
        logEntireMessage = "true"
        logMalformedMessages ="false"
        logMessagesAtServiceLevel ="true"
        logMessagesAtTransportLevel = "false"
        maxMessagesToLog = "3000"
        maxSizeOfMessageToLog ="2000" />
  </diagnostics>
  <bindings>
    <webHttpBinding>
      <binding name="WebHttpEndpointBinding">
        <security mode="TransportCredentialOnly">
          <transport clientCredentialType="Windows"/>
        </security>
      </binding>
    </webHttpBinding>
  </bindings>
  <behaviors>
    <endpointBehaviors>
      <behavior name="HighEnergy.Web.CustomerServiceAspNetAjaxBehavior">
        <enableWebScript />
        <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior name="debug">
        <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>
      <behavior name="AjaxServiceBehavior">
        <serviceMetadata httpGetEnabled="true" />
        <serviceThrottling
                     maxConcurrentCalls="4096"
                     maxConcurrentSessions="4096"
                     maxConcurrentInstances="4096"
                   />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <standardEndpoints>
    <webHttpEndpoint>
      <standardEndpoint name="" contentTypeMapper="HighEnergy.Web.WebContentTypeMapper.JsonContentTypeMapper, JsonContentTypeMapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </webHttpEndpoint>
  </standardEndpoints>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
    multipleSiteBindingsEnabled="false" />
  <services>
    <service name="HighEnergy.Web.CustomerService">
      <endpoint address="http://localhost:44208/CustomerService.svc" behaviorConfiguration="HighEnergy.Web.CustomerServiceAspNetAjaxBehavior"
        binding="webHttpBinding" contract="HighEnergy.Web.CustomerService" />
    </service>
  </services>
</system.serviceModel>

以下是在 AJAX 调用中返回给客户端的异常详细信息的基础知识(为简洁起见进行了编辑):

"Type" : "System.ServiceModel.Dispatcher.NetDispatcherFaultException"},
"ExceptionType" : "System.ServiceModel.Dispatcher.NetDispatcherFaultException",
"Message" : "The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Validate'. Encountered unexpected character 'A'."

如果需要,我可以提供更多详细信息。我已经签出 several other questions,但在大约 6 小时后无法让它工作。我想这可能是我所缺少的非常简单的东西,但我们将不胜感激。

我认为问题在于您混淆了两种不同的东西。一方面,您在主机端使用 webHTTPBinding 并发出 AJAX 请求。这应该没问题,但您还添加了 AspNetCompatibilityRequirements,这是一个以 .NET 为中心的、仅限 Microsoft 的要求,它不能很好地与您的 Java 内容一起使用。

所以我会去掉 AspNetCompatibilityRequirements 然后再试一次。

我认为您在 data 参数周围遗漏了一个 JSON.stringify()。在我的脑海中,我不知道 javascript 对象序列化为什么。他们可能应该假设大多数人都希望它序列化为 json 并适应这一点 - 但他们没有,所以 $.ajax 的参数必须在 POST 之前进行字符串化。