WCF服务和客户端返回无效的响应类型

时间:2014-12-03 02:51:39

标签: c# asp.net-mvc json wcf wcf-binding

我已经在网上阅读了关于这个确切错误的所有相关问题:

内容类型text / html; charset =响应消息的UTF-8与绑定的内容类型不匹配(text / xml; charset = utf-8)

但是,我仍然无法确定错误的实际原因是什么。它随机发生在负载均衡器后面的4个Web服务器上。发生错误时,我手动键入负载均衡器名称所在的地址栏中的一个Web服务器名称。对于该特定实例,不会从其中一个Web服务器检索数据,但如果我尝试其他服务器,我将恢复数据。

我们不能指望我们的用户手动将负载均衡器的URL更改为4个服务器名称中的一个以尝试获得命中。

从我读到的内容来看,它似乎是客户端和服务器之间的配置错误。如果是这样的话,会不会一直发生?从我的日志消息中,错误显然发生在MVC控制器方法内的客户端。

我在下面发布我的代码并希望有人可以回答这个问题。我真的很感激。

服务器配置

<system.serviceModel>
    <services>
      <service name="FuelTktImgRetrievalSvc.FuelTktImgRetrieval" behaviorConfiguration="ServiceBehavior">
        <endpoint address="" binding="basicHttpBinding" contract="FuelTktImgRetrievalSvc.IFuelTktImgRetrieval" bindingConfiguration="BasicHttpBinding_IFuelTktImgRetrieval">
          <identity>
            <dns value="localhost" />
          </identity>
      </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <!--<add baseAddress="\\miavdeteoabweb\applications\FuelTktImgRetrievalSvc" />-->
          </baseAddresses>
        </host>
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IFuelTktImgRetrieval" closeTimeout="00:10:00"
      openTimeout="00:10:00" receiveTimeout="04:00:00"
      sendTimeout="04:00:00" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" />
      </basicHttpBinding>
    </bindings>
    <diagnostics wmiProviderEnabled="false" performanceCounters="All">
      <messageLogging logMessagesAtTransportLevel="true" logMessagesAtServiceLevel="true" logMalformedMessages="true" logEntireMessage="true" maxSizeOfMessageToLog="2147483647" maxMessagesToLog="500" />
    </diagnostics>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="http" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

客户端配置

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IFuelTktImgRetrieval" />
      </basicHttpBinding>
    </bindings>
    <diagnostics wmiProviderEnabled="true">
      <messageLogging logMessagesAtTransportLevel="true" logMessagesAtServiceLevel="true" logMalformedMessages="true" logEntireMessage="true" maxSizeOfMessageToLog="2147483647" maxMessagesToLog="500" />
    </diagnostics>
    <client>
      <endpoint address="http://ride/FuelTktImgRetrievalSvc/FuelTktImgRetrieval.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFuelTktImgRetrieval"
        contract="FuelTktImgRetrievalSvc.IFuelTktImgRetrieval" name="BasicHttpBinding_IFuelTktImgRetrieval" />
    </client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webhttp">
          <webHttp />
        </behavior>
        <behavior name="BasicHttpBinding_IFuelTktImgRetrieval">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

名为

的服务器方法
public async Task<List<HH_FuelTkt_Output>> GetFilteredFuelTicketsAsync(HH_FuelTkt_Input value)
        {

            using (HandheldEntities DbContext = new HandheldEntities())
            {
                using (var dbcxtTrans = DbContext.Database.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
                {
                    try
                    {
                        // code left out for brevity
                        List<HH_FuelTkt_Output> tkts_Combined = await tkts.ToListAsync();

                        dbcxtTrans.Commit();

                        return tkts_Combined;
                    }

MVC控制器代码

[HttpPost]
public async Task<JsonResult> GetFilteredFuelTicketsAsync(HH_FuelTkt_Input id)
{
    try
    {
        IEnumerable<HH_FuelTkt_Output> fto = await db.GetFilteredFuelTicketsAsync(id);

        return Json(fto, JsonRequestBehavior.AllowGet);
    }

从客户端到服务的AJAX调用。

$.ajax({
                    url: '@Url.Action("GetFilteredFuelTicketsAsync", "Home")',
                    data: JSON.stringify(HH_FuelTkt_Input),
                    dataType: 'json',
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {

1 个答案:

答案 0 :(得分:1)

这可能是您在客户端而不是服务器上看到的错误。该错误消息指出它在预期text/html时收到text/xml。这通常是在IIS中托管服务时的情况,并且服务器的某些问题导致它使用ASP.NET错误页面(将是HTML)而不是SOAP响应(这将是XML)进行响应。错误的间歇性以及您使用负载均衡器的事实告诉我,负载均衡器背后的服务器之一有问题。

您可以单独访问服务器吗?尝试浏览每个服务器的.svc页面和/或在像soapui这样的工具中调用Web服务,您应该看到HTML错误页面。或者尝试在每个服务器上搜索Windows事件日志以查找ASP.NET错误,您可能会发现其中一个旧错误已记录。