我正在开展一个必须调用我创建的简单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个小时后无法让它工作。我想这可能是一件非常简单的事情,我很遗憾,但任何帮助都会受到赞赏。
答案 0 :(得分:1)
我相信你在JSON.stringify()
参数附近错过data
。在我的头脑中,我不知道javascript对象序列化了什么。他们应该假设大多数人都希望将它序列化为json并适应它 - 但是他们不会这样做,所以$ .ajax的参数必须在POST之前进行字符串化。
答案 1 :(得分:0)
我认为问题在于你将两种不同的东西混在一起。一方面,您使用AJAX请求在主机端使用webHTTPBinding。这应该没问题,但是您还要添加AspNetCompatibilityRequirements,这是一个以.NET为中心,仅限Microsoft的要求,它不能很好地处理您的Java内容。
所以我摆脱了AspNetCompatibilityRequirements并再次尝试。