2013-03-21 1595 views
34

我在尝试向WCFTestClient添加WCF服务时遇到了错误。我在网上浏览了一些解决方案,但是我无法完成它的工作。内容类型application/soap + xml; charset = utf-8不被服务支持

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

Content Type application/soap+xml; charset=utf-8 was not supported by service The client and service bindings may be mismatched. The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=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> 
+0

可能的重复:http://stackoverflow.com/questions/8087515/wcf-service-using-basichttpbinding-is-still-sending-content-type-text-xml?rq=1 – 2013-07-01 10:01:07

+0

这是一个老问题,但它显示为Google的第一个答案。我只想指出,错误信息可能会引起误解。根本的错误可能是完全无关的......这发生在我身上。 – sean717 2016-02-03 20:46:01

+0

当我向类中添加构造函数时,出现此错误。 – MC9000 2016-05-16 02:52:30

回答

3

我有同样的问题,得到了它的“绑定”德通过这样的服务行为,服务工作:

出了名的行为

<serviceBehaviors> 
     <behavior name="YourBehaviourNameHere"> 

并作出参考, NCE对自己的行为在服务

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

启用includeExceptionDetailsInFaults确实帮助了我。 – 2014-02-11 22:01:12

0

在我来说,同样的错误被遗漏在返回的数据类型

[DataContract] 
... 
[DataMember] 

属性造成的。

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

-3

检查确定到web.config在绑定区段

9

在这里,客户端配置文件是解决这个问题对我来说是web.config中的例子。在< 绑定名称=“TransportSecurity”注意messageEncoding =“TEXT” textEncoding =“UTF-8”>

3

在我的案例中的类没有默认的构造函数之一 - 无类默认构造函数不能被序列化。

+1

我很惊讶地发现这正是我遇到的问题。谢谢。我可以用更好,更具解释性的错误信息来完成! – arame3333 2016-10-20 13:10:19

+1

我需要将这个刺青纹身放在手臂上作为纪念风格的提醒。我比我想承认的更频繁地遇到这个问题。 – user942620 2016-12-15 16:51:55

1

WCF客户端尝试发送使用MTOM extension (MIME type application/soap+xml用于传送SOAP XML in MTOM)它的消息,可能会出现这种错误,但服务仅仅是能够理解正常的SOAP消息(它不包含MIME部分,仅文本/ xml类型在HTTP请求)。

确保您生成的客户端代码是正确的WSDL

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

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

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

你的课的名字是在两个地方 - SVC标记和CS文件。

检查端点合同 - 也是您的界面的确切名称,仅此而已。我已经添加了不能在那里的程序集名称。

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

值得一提的是,案件也很重要。 “Namespace.Service”与“NAMEspace.Service”不同。 – 2015-10-02 09:54:30

+1

这个答案导致我自己的问题。在refatoring期间,我的服务namepsace改变。如上所述,它导致从wsHttpBinding切换到basicHttpBinding,并导致错误。伟大的工作Jan. – Jeff 2016-10-11 19:14:38

+0

@Jeff - 我的人生2小时不见了。你的评论救了我。我的服务名称和合同在停放该项目之前都进行了重构,并且已被破坏。 – 2018-03-09 20:59:02

1

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

我通过更改服务器绑定到basichttpsbinding来解决问题。 此外,我不得不通过向目标框架设置为4.5:

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

[1]交际是通过https。

-1

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

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

} 
+1

这是另一个问题,还是你建议作者可能面临类似的问题?如果是后者,请编辑您的答案以使其更清晰。如果您发布的是其他问题,请将其作为单独问题发布。 – 2015-06-01 22:02:52

+0

注意谢谢。我希望我的编辑更清楚。这可能是问题的解决方案。 – 2015-06-02 13:03:50

0

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

我所做的就是从WebService接口中评论(删除)一些方法并尝试,然后评论另一堆方法并尝试,直到找到导致问题的方法。

在我的情况下,它使用了一个无法序列化的复杂对象。

祝你好运!

+0

我不明白这是如何解决这个确切的问题,而是解决所有问题。你可以说得更详细点吗? – 2017-05-08 09:30:33

+0

重点罗布是这个问题是由很多种原因造成的,我花了两个多小时试图找到没有运气的原因,这就是为什么我使用这种技术。我意识到一旦你发现导致问题的方法,就很容易找出原因。 – 2017-05-10 07:45:42

0

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

相关问题