内容类型application / soap + xml;服务不支持charset = utf-8

时间:2013-03-21 22:02:42

标签: wcf

尝试将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>

16 个答案:

答案 0 :(得分:12)

我遇到了命名问题。服务名称必须是您的实现的名称。如果不匹配,则默认情况下使用basicHttpBinding生成text/xml内容类型。

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

检查端点合同 - 再次确切地说明您的界面名称,仅此而已。我添加了无法存在的程序集名称。

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

答案 1 :(得分:9)

以下是为我解决问题的web.config示例。注意&lt; 绑定名称=“TransportSecurity”messageEncoding =“Text”textEncoding =“utf-8”&gt;

答案 2 :(得分:5)

我遇到了同样的问题,通过执行此操作,将“绑定”服务与服务行为联系起来:

为行为命名

<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>

答案 3 :(得分:4)

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

答案 4 :(得分:2)

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

当调试得知绑定不匹配时,我遇到了同样的问题。我指的是BasicHttpBinding,而不是WSHTTPBinding。就我而言,我同时引用BasicHttp和WsHttp。我正在根据参考动态分配绑定。因此,请检查您的服务构造函数。

检查一次您的服务构造函数。 Check the below image

答案 5 :(得分:1)

WCF客户端尝试使用MTOM extension (MIME type application/soap+xml发送其消息用于传输SOAP XML in MTOM时,可能会发生此错误,但服务只能理解正常的SOAP消息(它没有' t包含MIME部分,HTTP请求中只包含text / xml类型。

请确保您针对正确的WSDL生成了客户端代码。

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

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

答案 6 :(得分:1)

我的案子有不同的解决方案。 客户端正在使用 basichttpsbinding [1],该服务正在使用wshttpbinding。

我通过将服务器绑定更改为basichttpsbinding来解决问题。 此外,我必须通过添加:

将目标框架设置为4.5
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>

[1]通信已经过了https。

答案 7 :(得分:0)

在我的情况下,同样的错误是由于缺少

引起的
[DataContract]
...
[DataMember] 

返回数据类型中的属性。

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

答案 8 :(得分:0)

我遇到了同样的错误消息。我设法解决了这个问题:

在我的情况下,错误是我错过了返回类的父类中的[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; }

}

答案 9 :(得分:0)

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

答案 10 :(得分:0)

我的问题是我们自己的集合类,该类被标记为[DataContract]。从我的角度来看,这是一种干净的方法,并且可以与XmlSerializer一起很好地工作,但是对于WCF端点,它正在中断,因此我们必须将其删除。 XmlSerializer仍然可以不使用。

不起作用

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

工作

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

答案 11 :(得分:0)

在使用WebServiceTemplate spring ws时遇到相同的错误 [err] org.springframework.ws.client.WebServiceTransportException:无法处理消息,因为内容类型为'text / xml;。 charset = utf-8'不是预期的类型'application / soap + xml; charset = utf-8'。 [415] [err],位于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);

答案 12 :(得分:0)

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

答案 13 :(得分:0)

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

答案 14 :(得分:-3)

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

答案 15 :(得分:-4)

检查绑定部分

上标识为web.config的客户端配置文件