首页 文章

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

提问于
浏览
42

尝试将WCF服务添加到WCFTestClient时,我收到错误以下错误 . 我在网上经历了一些解决方案,但我无法让它工作 .

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

内容类型申请/ soap xml;服务不支持charset = utf-8客户端和服务绑定可能不匹配 . 远程服务器返回错误:(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>

14 回答

  • 0

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

    您的类的名称位于两个位置 - SVC标记和CS文件 .

    检查 endpoints Contract - 再次确切地说明您的界面名称,仅此而已 . 我添加了组件名称,它不能存在 .

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

    以下是为我解决问题的web.config示例 . 注意< binding name="TransportSecurity" messageEncoding="Text" textEncoding="utf-8">

  • 1

    我有同样的问题,通过执行此操作,通过“绑定”服务行为与服务行为工作:

    为行为命名

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

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

  • 0

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

    当调试知道绑定中存在不匹配时,我遇到了同样的问题 . 我指的是BasicHttpBinding而不是WSHTTPBinding . 就我而言,我也指的是BasicHttp和WsHttp . 我根据引用动态分配绑定 . 所以检查你的服务构造函数 .

    检查您的服务构造器一次 . Check the below image

  • 0

    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" >
    
  • 1

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

    我通过将服务器绑定更改为basichttpsbinding来解决问题 . 此外,我必须通过添加以下内容将目标框架设置为4.5:

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

    [1]通信结束了https .

  • 1

    对我来说,由于涉及大量方法和类,因此很难确定问题 .

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

    就我而言,它使用的是一个无法序列化的复杂对象 .

    祝好运!

  • -1

    在我的情况下,错误导致同样的错误

    [DataContract]
    ...
    [DataMember]
    

    返回的数据类型中的属性 .

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

  • 2

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

  • 9

    我的问题是我们自己的集合类,它被标记为[DataContract] . 从我的角度来看,这是一个干净的方法,它适用于XmlSerializer,但对于WCF endpoints ,它正在破坏,我们不得不删除它 . XmlSerializer仍然没有 .

    Not working

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

    Working

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

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

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

    我不得不补充一下?wsdl参数到url的末尾 . 例如:http://localhost:8745/YourServiceName/?wsdl

  • 4

    检查绑定部分上标识为web.config的客户端配置文件

相关问题