服务器上的WCF服务引发错误(在本地工作正常)

时间:2013-09-16 18:05:47

标签: wcf wcf-binding

我使用c#4.0和wcf 4.0创建了一个WCF服务,它在本地工作正常。使用WCf Test客户端测试,并使用控制台应用程序作为客户端。但是在部署服务之后我做了同样的事情,但它给出了以下错误。我不知道我做错了什么......

  

内容类型text / html; charset =响应消息的UTF-8与   绑定的内容类型(text / xml; charset = utf-8)   如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法   响应的前1024个字节是:
  “   #content {FONT-SIZE:0.7em; PADDING-BOTTOM:2em; MARGIN-LEFT:30px} BODY {MARGIN-TOP:0px; MARGIN-LEFT:0px;颜色:#000000; FONT-FAMILY:Verdana;背景颜色:白色} P {MARGIN-TOP:0px; MARGIN-BOTTOM:12px;颜色:#000000; FONT-FAMILY:Verdana} PRE {BORDER-RIGHT:#f0f0e0 1px solid; PADDING-RIGHT:5px; BORDER-TOP:#f0f0e0 1px solid; MARGIN-TOP:-5px; PADDING-LEFT:5px; FONT-SIZE:1.2em; PADDING-BOTTOM:5px; BORDER-LEFT:#f0f0e0 1px solid; PADDING-TOP:5px; BORDER-BOTTOM:#f0f0e0 1px solid; FONT-FAMILY:Courier New;背景颜色:#e5e5cc} .heading1 {MARGIN-TOP:0px; PADDING-LEFT:15px; FONT-WEIGHT:正常; FONT-SIZE:26px; MARGIN-BOTTOM:0px; PADDING-BOTTOM:3px; MARGIN-LEFT:-30px;宽度:100%;颜色:#ffffff; PADDING-TOP:10px; FONT-FAMILY:Tahoma;背景颜色:#003366} .intro {MARGIN-LEFT:-15px}   测试服务      

服务器堆栈跟踪:
  在System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest请求,HttpWebResponse响应,HttpChannelFactory 1 factory, WebException responseException, ChannelBinding channelBinding)
at System.ServiceModel.Channels.HttpChannelFactory
1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan超时)
  在System.ServiceModel.Channels.RequestChannel.Request(消息消息,TimeSpan超时)
  在System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息消息,TimeSpan超时)
  在System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway,ProxyOperationRuntime operation,Object [] ins,Object [] outs,TimeSpan timeout)
  在System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)
  在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

致电服务::

public static void Main(string[] args)
{

    TestClient user = new TestClient();  
    string id = "rSmith";
    string u = user.GetUserName(id);
    user.Close();
    Console.WriteLine("The User you Requested is "+ u+" .");
}

服务WebConfig。

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="NameSoap" 
               closeTimeout="00:01:00" 
               openTimeout="00:01:00"
               receiveTimeout="00:10:00" 
               sendTimeout="00:01:00" 
               allowCookies="false"
               bypassProxyOnLocal="false" 
               hostNameComparisonMode="StrongWildcard"
               maxBufferSize="65536" 
               maxBufferPoolSize="524288" 
               maxReceivedMessageSize="65536"
               messageEncoding="Text" 
               textEncoding="utf-8" 
               transferMode="Buffered"
               useDefaultWebProxy="true">
        <readerQuotas maxDepth="32" 
                      maxStringContentLength="8192" 
                      maxArrayLength="16384"
                      maxBytesPerRead="4096" 
                      maxNameTableCharCount="16384" />
        <security mode="None">
          <transport clientCredentialType="None" 
                     proxyCredentialType="None"
                     realm="" />
          <message clientCredentialType="UserName" 
                   algorithmSuite="Default" />
        </security>
      </binding>
    </basicHttpBinding>
    <mexHttpBinding>
      <binding name="NewBinding1"/>
    </mexHttpBinding>
  </bindings>
  <!--<client/>-->
  <services>
    <service behaviorConfiguration="MyServiceTypebehaviors" 
             name="Test">
      <endpoint address="" 
                binding="basicHttpBinding" 
                bindingConfiguration="UserNameSoap" 
                bindingNamespace="https:/abc.com/Services/Test/"
                contract="Test.ITest" />
      <endpoint address="mex" 
                binding="mexHttpBinding" 
                bindingConfiguration="NewBinding1"
                contract="IMetadataExchange" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="MyServiceTypebehaviors">
        <!-- 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"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="false"
                             multipleSiteBindingsEnabled="true"/>
</system.serviceModel>

客户端配置文件

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="BasicHttpBinding_ITest" />
    </basicHttpBinding>
  </bindings>
  <client>            
    <endpoint address="http://abc.com/Services/Test/Test.svc"
              binding="basicHttpBinding" 
              bindingConfiguration="BasicHttpBinding_ITest"
              contract="DevService.ITest" 
              name="BasicHttpBinding_ITest" />         
  </client>
</system.serviceModel>

3 个答案:

答案 0 :(得分:0)

这里的问题是“响应消息与绑定的内容类型不匹配”,

检查此链接,解决了问题:

http://forums.asp.net/t/1815638.aspx

论坛解释实际问题,您可以从Error consuming webservice, content type "application/xop+xml" does not match expected type "text/xml"

获取解决方案

答案 1 :(得分:0)

这个问题有时会出现严重问题,但对我而言,服务引用和服务之间总是存在简单的不匹配。

更新服务引用时,它会更新该项目的web.config。 但是,如果您的代码不在启动项目中,则Web配置条目对您的代码没有影响。因此,如果您的服务引用不在启动项目中,那么当您刷新它时,您需要从项目中复制相关的客户端条目,并引用启动项目的Web配置客户端部分。

答案 2 :(得分:0)

您的客户与服务之间的合同不匹配:

服务合同:“Test.ITest”

<endpoint address="" 
          binding="basicHttpBinding" 
          bindingConfiguration="UserNameSoap" 
          bindingNamespace="https:/abc.com/Services/Test/"
          contract="Test.ITest" />

客户合同:“DevService.ITest”

<endpoint address="http://abc.com/Services/Test/Test.svc"
          binding="basicHttpBinding" 
          bindingConfiguration="BasicHttpBinding_ITest"
          contract="DevService.ITest" 
          name="BasicHttpBinding_ITest" />   

客户合同需要与服务合同相匹配 - 在本例中为Test.ITest。即使Test.ITestDevService.ITest是相同的代码,由于命名空间,它们也会被视为独立且不同的。

此外,您在绑定配置中指定的约90%的设置是默认设置,您可以安全地省略它们。