【问题标题】:Content Type application/soap+xml; charset=utf-8 was not supported by service内容类型应用程序/soap+xml; charset=utf-8 不受服务支持
【发布时间】:2013-03-11 15:26:33
【问题描述】:

我在尝试将 WCF 服务添加到 WCFTestClient 时收到以下错误。我在网上浏览了许多解决方案,但我无法让它工作。

有人可以帮我解决这些问题吗? 我还提供了我的服务配置文件:

内容类型 application/soap+xml; charset=utf-8 不受支持 service 客户端和服务绑定可能不匹配。这 远程服务器返回错误:(415)无法处理消息 因为内容类型'application/soap+xml; charset=utf-8' 不是 预期类型'text/xml;字符集=utf-8

代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file 
  must be added to the host's app.config file. System.Configuration does not 
  support config files for libraries. -->
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttp" allowCookies="true"
                 maxReceivedMessageSize="20000000"
                 maxBufferSize="20000000"
                 maxBufferPoolSize="20000000">
          <readerQuotas maxDepth="32"
               maxArrayLength="200000000"
               maxStringContentLength="200000000"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
     <service name="WCFTradeLibrary.TradeService">
        <endpoint address="" binding="basicHttpBinding"
            bindingConfiguration="basicHttp"
            contract="WCFTradeLibrary.ITradeService">          
         </endpoint>
     </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint 
          above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faul`enter code here`ts for 
          debugging purposes,  
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception info`enter code here`rmation -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

【问题讨论】:

  • 这是一个老问题,但它显示为谷歌的第一个答案。我只想指出错误消息可能具有误导性。潜在的错误可能完全不相关......这发生在我身上。
  • 向类添加构造函数时出现此错误。
  • 对我来说,问题是我需要为服务行为添加一个名称,然后将该行为分配给服务以使 SOAP 正常工作,尽管没有它,其余的工作仍然有效。在我的情况下,soap 需要元数据交换

标签: wcf


【解决方案1】:

我遇到了命名问题。服务名称必须与您的实现完全一致。如果不匹配,则默认使用 basicHttpBinding 导致 text/xml 内容类型。

您的班级名称位于两个位置 - SVC 标记和 CS 文件。

也检查端点合同 - 再次准确的接口名称,仅此而已。我已经添加了无法存在的程序集名称。

<service name="MyNamespace.MyService">
    <endpoint address="" binding="wsHttpBinding" contract="MyNamespace.IMyService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>

【讨论】:

  • 值得一提的是,案件也很重要。 “Namespace.Service”不同于“NAMEspace.Service”。
  • 这个答案让我想到了自己的问题。在重构期间,我的服务的命名空间发生了变化。如上所述,这导致从 wsHttpBinding 切换到 basicHttpBinding,并导致错误。一月干得好。
  • @Jeff - 我生命中的 2 个小时已经过去了。你的评论救了我。我的服务名称和合同都在停放项目之前进行了重构,并且已损坏。
【解决方案2】:

这是为我解决问题的 web.config 示例。注意绑定 name="TransportSecurity" messageEncoding="Text" textEncoding="utf-8">

【讨论】:

    【解决方案3】:

    在我的例子中,其中一个类没有默认构造函数 - 没有默认构造函数的类无法序列化。

    【讨论】:

    • 我很惊讶地发现这正是我遇到的问题。谢谢你。我本可以使用更好、更解释性的错误消息!
    • 我需要将这个纹身纹在我的手臂上,作为纪念品风格的提醒。我遇到这个问题的次数比我想承认的要多。
    • 另外:我还有几个只读属性(有一个 get,但没有设置)。即使使用新的默认 ctor 将它们保持为只读确实有意义,但序列化需要“setter”才能起作用,因此必须取消只读限制。
    【解决方案4】:

    我遇到了同样的问题,通过这样做将服务与服务行为“绑定”使其工作:

    为行为命名

    <serviceBehaviors>
            <behavior name="YourBehaviourNameHere">
    

    并参考您在服务中的行为

    <services>
      <service name="WCFTradeLibrary.TradeService" behaviorConfiguration="YourBehaviourNameHere">
    

    整个事情是:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    
      <system.web>
        <compilation debug="true" />
      </system.web>
      <!-- When deploying the service library project, the content of the config file must be added to the host's 
      app.config file. System.Configuration does not support config files for libraries. -->
      <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="basicHttp" allowCookies="true"
                     maxReceivedMessageSize="20000000"
                     maxBufferSize="20000000"
                     maxBufferPoolSize="20000000">
              <readerQuotas maxDepth="32"
                   maxArrayLength="200000000"
                   maxStringContentLength="200000000"/>
            </binding>
          </basicHttpBinding>
        </bindings>
        <services>
         <service name="WCFTradeLibrary.TradeService" behaviourConfiguration="YourBehaviourNameHere">
            <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttp" contract="WCFTradeLibrary.ITradeService">          
             </endpoint>
         </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="YourBehaviourNameHere">
              <!-- To avoid disclosing metadata information, 
              set the value below to false and remove the metadata endpoint above before deployment -->
              <serviceMetadata httpGetEnabled="true"/>
              <!-- To receive exception details in faul`enter code here`ts for debugging purposes, 
              set the value below to true.  Set to false before deployment 
              to avoid disclosing exception info`enter code here`rmation -->
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    
    </configuration>
    

    【讨论】:

    • 启用 includeExceptionDetailsInFaults 对我很有帮助。
    【解决方案5】:

    正如其他人所建议的那样,这是由于服务客户端不匹配而发生的。

    我遇到了同样的问题,什么时候调试才知道绑定不匹配。我指的是 BasicHttpBinding 而不是 WSHTTPBinding。就我而言,我指的是 BasicHttpWsHttp。我根据引用动态分配绑定。所以检查你的服务构造函数,如下所示

    Refer this image

    【讨论】:

      【解决方案6】:

      WCF 客户端尝试使用MTOM extension (MIME type application/soap+xml 传输SOAP XML in MTOM 发送其消息时可能会出现此错误,但服务只能理解普通的 SOAP 消息(它不包含 MIME 部分,仅HTTP 请求中的 text/xml 类型)。

      确保您根据正确的WSDL 生成了您的客户端代码。

      为了在服务器端使用 MTOM,更改配置文件添加 messageEncoding 属性:

      <binding name="basicHttp" allowCookies="true"
                   maxReceivedMessageSize="20000000"
                   maxBufferSize="20000000"
                   maxBufferPoolSize="20000000"
                   messageEncoding="Mtom" >
      

      【讨论】:

        【解决方案7】:

        我的情况有不同的解决方案。 客户端使用 basichttpsbinding[1],服务使用 wshttpbinding。

        我通过将服务器绑定更改为 basichttpsbinding 解决了这个问题。 另外,我必须通过添加将目标框架设置为 4.5:

          <system.web>
            <compilation debug="true" targetFramework="4.5" />
            <httpRuntime targetFramework="4.5"/>
          </system.web>
        

        [1] 通信通过 https。

        【讨论】:

          【解决方案8】:

          对我来说,由于涉及到大量的方法和类,所以很难确定问题。

          我所做的是从WebService接口中注释(删除)一些方法并尝试,然后注释另一组方法并尝试,我一直这样做直到找到导致问题的方法。

          在我的例子中,它使用了一个无法序列化的复杂对象。

          祝你好运!

          【讨论】:

          • 我不明白这是解决这个精确问题的方法,而是解决所有问题的方法。你能说得更具体点吗?
          • Rob 的重点是这个问题是由多种原因引起的,我花了两个多小时试图找出原因,但没有运气,这就是我使用这种技术的原因。我意识到一旦你抓住了导致问题的方法,就很容易找出原因。
          【解决方案9】:

          我遇到了同样的错误消息。我设法修复它:

          在我的情况下,错误是我错过了返回类的父类中的 [datacontract] 和 [datamember] 属性。错误消息具有误导性。

          [OperationContract]
          List<MyClass> GetData();
          
          [DataContract]
          public class MyClass : MyParentClass
          {       
              [DataMember]
              public string SomeString { get; set; }      
          }
          
          // Missing DataContract
          public class MyParentClass
          {
              // Missing DataMember
              public int SomeNumber { get; set; }
          
          }
          

          【讨论】:

          • 这是另一个问题,还是您是在暗示作者可能面临类似的问题?如果是后者,请编辑您的答案以使其更清楚。如果您要发布不同的问题,请将其作为单独的问题发布。
          • 特别感谢。我希望我的编辑使它更清楚。这可能是问题的解决方案。
          【解决方案10】:

          在我的情况下,同样的错误是由于丢失引起的

          [DataContract]
          ...
          [DataMember] 
          

          返回数据类型中的属性。

          检查并尝试添加它们,看看是否有帮助。

          【讨论】:

            【解决方案11】:

            我在跟踪日志中也遇到了同样的错误。我在 API 中新创建的函数抛出了同样的错误,但令我惊讶的是,旧函数表现良好。 问题是 - 我的合同数据成员几乎没有对象类型的变量。 soap-xml 无法很好地处理它,但是,我可以看到对象类型数组 (object[]) 没有任何问题地通过。只有一个简单的对象类型没有被soap解析。这可能是服务引发上述错误的另一个原因。

            【讨论】:

              【解决方案12】:

              我的问题是我们自己的集合类,它被标记为 [DataContract]。从我的角度来看,这是一种干净的方法,它与 XmlSerializer 一起工作得很好,但对于 WCF 端点它正在破坏,我们不得不将其删除。没有 XmlSerializer 仍然可以工作。

              不工作

              [DataContract]
              public class AttributeCollection : List<KeyValuePairSerializable<string, string>>
              

              工作

              public class AttributeCollection : List<KeyValuePairSerializable<string, string>>
              

              【讨论】:

                【解决方案13】:

                我在使用 WebServiceTemplate spring ws 时遇到了同样的错误 [err] org.springframework.ws.client.WebServiceTransportException:无法处理消息,因为内容类型'text/xml; charset=utf-8' 不是预期的类型 'application/soap+xml;字符集=utf-8'。 [415] [错误] 在 org.springframework.ws.client.core.WebServiceTemplate.handleError(WebServiceTemplate.java:665)。 我使用的 WSDL 有soap1.2 协议,默认情况下协议是soap1.1 . 当我使用下面的代码更改协议时,它正在工作

                 MessageFactory msgFactory = MessageFactory.newInstance(javax.xml.soap.SOAPConstants.SOAP_1_2_PROTOCOL);
                     SaajSoapMessageFactory saajSoapMessageFactory = new SaajSoapMessageFactory(msgFactory);
                     saajSoapMessageFactory.setSoapVersion(SoapVersion.SOAP_12);
                     getWebServiceTemplate().setMessageFactory(saajSoapMessageFactory);
                

                【讨论】:

                  【解决方案14】:

                  我遇到了同样的问题,并通过将 EnumMemberAttribute 用于枚举成员的属性来解决它。如果您使用枚举类型作为数据协定,并且其成员使用 DataMemberAttribute 属性,则会发生相同的错误。您必须对枚举成员使用 EnumMemberAttribute

                  【讨论】:

                    【解决方案15】:

                    我也遇到了同样的问题。就我而言,我使用的是 transfermode = streaming 和 Mtom。事实证明,我已经将我的一个变量(用于结构)命名为“HEADER”。这与作为 http 服务下载的一部分的消息元素 [http://tempuri.org/:HEADER] 冲突。显然,必须避免使用“保留”字样作为参数名称。

                    【讨论】:

                      【解决方案16】:

                      我必须将 ?wsdl 参数添加到 url 的末尾。 例如:http://localhost:8745/YourServiceName/?wsdl

                      【讨论】:

                        【解决方案17】:

                        在绑定部分检查识别到 web.config 的客户端配置文件

                        【讨论】:

                          猜你喜欢
                          • 2012-01-05
                          • 1970-01-01
                          • 2019-11-23
                          • 2015-03-26
                          • 2018-07-24
                          • 2020-01-08
                          • 2015-11-01
                          • 1970-01-01
                          • 2018-11-06
                          相关资源
                          最近更新 更多