客户端调用WCF服务出现以下错误:

“/”应用程序中的服务器错误。


远程服务器返回错误: (415) Unsupported Media Type。

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Net.WebException: 远程服务器返回错误: (415) Unsupported Media Type。

堆栈跟踪:

[WebException: 远程服务器返回错误: (415) Unsupported Media Type。]
System.Net.HttpWebRequest.GetResponse() +6449128
System.ServiceModel.Channels.HttpChannelRequest.WaitForReply(TimeSpan timeout) +55 [ProtocolException: 服务 http://localhost:1234/AreaService.svc 不支持内容类型 text/xml; charset=utf-8。客户端和服务绑定可能不匹配。]

  观察堆栈可以知道,这是因为客户端和服务端绑定不匹配造成的,代码如下:

服务端配置节点信息:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<!-- Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages -->
<service name="SOA.Services.SearchConfigBiz" behaviorConfiguration="a1">
<endpoint contract="SOA.Contract.ISearchConfig" binding="wsHttpBinding"/>
<endpoint contract="SOA.Contract.ISearchConfig" binding="mexHttpBinding" address="mex"/>
</service>
<service name="SOA.Services.SubjectBiz" behaviorConfiguration="a1">
<endpoint contract="SOA.Contract.ISubject" binding="wsHttpBinding"/>
<endpoint contract="SOA.Contract.ISubject" binding="mexHttpBinding" address="mex"/>
</service>
<service name="SOA.Services.AreaBiz" behaviorConfiguration="a1">
<endpoint contract="SOA.Contract.IArea" binding="wsHttpBinding"/>
<endpoint contract="SOA.Contract.IArea" binding="mexHttpBinding" address="mex"/>
</service>
<service name="SOA.Services.YearBiz" behaviorConfiguration="a1">
<endpoint contract="SOA.Contract.IYear" binding="wsHttpBinding"/>
<endpoint contract="SOA.Contract.IYear" binding="mexHttpBinding" address="mex"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="a1">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
客户端配置节点信息:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_SubjectContract" />
<binding name="BasicHttpBinding_AreaContract" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" />
<binding name="BasicHttpBinding_SearchConfigContract" />
<binding name="BasicHttpBinding_YearContract" />
</basicHttpBinding>
<wsHttpBinding>
<binding name="MetadataExchangeHttpBinding_SubjectContract">
<security mode="None" />
</binding>
<binding name="MetadataExchangeHttpBinding_AreaContract">
<security mode="None" />
</binding>
<binding name="MetadataExchangeHttpBinding_SearchConfigContract">
<security mode="None" />
</binding>
<binding name="MetadataExchangeHttpBinding_YearContract">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:1234/AreaService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_AreaContract" contract="AreaService.AreaContract"
name="BasicHttpBinding_AreaContract" />
<endpoint address="http://localhost:1234/SearchConfigService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_SearchConfigContract"
contract="SearchConfigService.SearchConfigContract" name="BasicHttpBinding_SearchConfigContract" />
<endpoint address="http://localhost:1234/SubjectService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_SubjectContract"
contract="SubjectService.SubjectContract" name="BasicHttpBinding_SubjectContract" />
<endpoint address="http://localhost:1234/YearService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_YearContract" contract="YearService.YearContract"
name="BasicHttpBinding_YearContract" />
</client>
</system.serviceModel>

所以,只需要将客户端和服务端的配置保持一致即可。

代码如下:

1.使用basicHttpBinding:

客户端配置节点信息:

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_SubjectContract" />
<binding name="BasicHttpBinding_AreaContract" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" />
<binding name="BasicHttpBinding_SearchConfigContract" />
<binding name="BasicHttpBinding_YearContract" />
</basicHttpBinding>
<wsHttpBinding>
<binding name="MetadataExchangeHttpBinding_SubjectContract">
<security mode="None" />
</binding>
<binding name="MetadataExchangeHttpBinding_AreaContract">
<security mode="None" />
</binding>
<binding name="MetadataExchangeHttpBinding_SearchConfigContract">
<security mode="None" />
</binding>
<binding name="MetadataExchangeHttpBinding_YearContract">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:1234/AreaService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_AreaContract" contract="AreaService.AreaContract"
name="BasicHttpBinding_AreaContract" />
<endpoint address="http://localhost:1234/SearchConfigService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_SearchConfigContract"
contract="SearchConfigService.SearchConfigContract" name="BasicHttpBinding_SearchConfigContract" />
<endpoint address="http://localhost:1234/SubjectService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_SubjectContract"
contract="SubjectService.SubjectContract" name="BasicHttpBinding_SubjectContract" />
<endpoint address="http://localhost:1234/YearService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_YearContract" contract="YearService.YearContract"
name="BasicHttpBinding_YearContract" />
</client>
</system.serviceModel> 服务端配置节点信息: <system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<!-- Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages -->
<service name="SOA.Services.SearchConfigBiz" behaviorConfiguration="a1">
<endpoint contract="SOA.Contract.ISearchConfig" binding="basicHttpBinding"/>
<endpoint contract="SOA.Contract.ISearchConfig" binding="mexHttpBinding" address="mex"/>
</service>
<service name="SOA.Services.SubjectBiz" behaviorConfiguration="a1">
<endpoint contract="SOA.Contract.ISubject" binding="basicHttpBinding"/>
<endpoint contract="SOA.Contract.ISubject" binding="mexHttpBinding" address="mex"/>
</service>
<service name="SOA.Services.AreaBiz" behaviorConfiguration="a1">
<endpoint contract="SOA.Contract.IArea" binding="basicHttpBinding"/>
<endpoint contract="SOA.Contract.IArea" binding="mexHttpBinding" address="mex"/>
</service>
<service name="SOA.Services.YearBiz" behaviorConfiguration="a1">
<endpoint contract="SOA.Contract.IYear" binding="basicHttpBinding"/>
<endpoint contract="SOA.Contract.IYear" binding="mexHttpBinding" address="mex"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="a1">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

使用basicHttpBinding

2.使用wsHttpBinding:

客户端配置节点信息:

<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="BasicHttpBinding_SubjectContract" />
<binding name="BasicHttpBinding_AreaContract" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" />
<binding name="BasicHttpBinding_SearchConfigContract" />
<binding name="BasicHttpBinding_YearContract" />
</wsHttpBinding>
<basicHttpBinding>
<binding name="MetadataExchangeHttpBinding_SubjectContract">
<security mode="None" />
</binding>
<binding name="MetadataExchangeHttpBinding_AreaContract">
<security mode="None" />
</binding>
<binding name="MetadataExchangeHttpBinding_SearchConfigContract">
<security mode="None" />
</binding>
<binding name="MetadataExchangeHttpBinding_YearContract">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:1234/AreaService.svc" binding="wsHttpBinding"
bindingConfiguration="BasicHttpBinding_AreaContract" contract="AreaService.AreaContract"
name="BasicHttpBinding_AreaContract" />
<endpoint address="http://localhost:1234/SearchConfigService.svc"
binding="wsHttpBinding" bindingConfiguration="BasicHttpBinding_SearchConfigContract"
contract="SearchConfigService.SearchConfigContract" name="BasicHttpBinding_SearchConfigContract" />
<endpoint address="http://localhost:1234/SubjectService.svc"
binding="wsHttpBinding" bindingConfiguration="BasicHttpBinding_SubjectContract"
contract="SubjectService.SubjectContract" name="BasicHttpBinding_SubjectContract" />
<endpoint address="http://localhost:1234/YearService.svc" binding="wsHttpBinding"
bindingConfiguration="BasicHttpBinding_YearContract" contract="YearService.YearContract"
name="BasicHttpBinding_YearContract" />
</client>
</system.serviceModel> 服务端配置节点信息: <system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<!-- Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages -->
<service name="SOA.Services.SearchConfigBiz" behaviorConfiguration="a1">
<endpoint contract="SOA.Contract.ISearchConfig" binding="wsHttpBinding"/>
<endpoint contract="SOA.Contract.ISearchConfig" binding="mexHttpBinding" address="mex"/>
</service>
<service name="SOA.Services.SubjectBiz" behaviorConfiguration="a1">
<endpoint contract="SOA.Contract.ISubject" binding="wsHttpBinding"/>
<endpoint contract="SOA.Contract.ISubject" binding="mexHttpBinding" address="mex"/>
</service>
<service name="SOA.Services.AreaBiz" behaviorConfiguration="a1">
<endpoint contract="SOA.Contract.IArea" binding="wsHttpBinding"/>
<endpoint contract="SOA.Contract.IArea" binding="mexHttpBinding" address="mex"/>
</service>
<service name="SOA.Services.YearBiz" behaviorConfiguration="a1">
<endpoint contract="SOA.Contract.IYear" binding="wsHttpBinding"/>
<endpoint contract="SOA.Contract.IYear" binding="mexHttpBinding" address="mex"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="a1">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

使用wsHttpBinding

WCF学习笔记二的更多相关文章

  1. WCF学习笔记之事务编程

    WCF学习笔记之事务编程 一:WCF事务设置 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元: WCF通过System.ServiceModel.TransactionFlowA ...

  2. WCF学习笔记之传输安全

    WCF学习笔记之传输安全 最近学习[WCF全面解析]下册的知识,针对传输安全的内容做一个简单的记录,这边只是简单的记录一些要点:本文的内容均来自[WCF全面解析]下册: WCF的传输安全主要涉及认证. ...

  3. WCF 学习笔记之双工实现

    WCF 学习笔记之双工实现 其中 Client 和Service为控制台程序 Service.Interface为类库 首先了解契约Interface两个接口 using System.Service ...

  4. WPF的Binding学习笔记(二)

    原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...

  5. AJax 学习笔记二(onreadystatechange的作用)

    AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...

  6. [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计

    源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...

  7. JMX学习笔记(二)-Notification

    Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...

  8. java之jvm学习笔记二(类装载器的体系结构)

    java的class只在需要的时候才内转载入内存,并由java虚拟机的执行引擎来执行,而执行引擎从总的来说主要的执行方式分为四种, 第一种,一次性解释代码,也就是当字节码转载到内存后,每次需要都会重新 ...

  9. WCF 学习笔记之异常处理

    WCF 学习笔记之异常处理 1:WCF异常在配置文件 <configuration> <system.serviceModel> <behaviors> <s ...

随机推荐

  1. Greenplum 5.21.1 集群安装部署

    简单来说GPDB是一个分布式数据库软件,其可以管理和处理分布在多个不同主机上的海量数据.对于GPDB来说,一个DB实例实际上是由多个独立的PostgreSQL实例组成的,它们分布在不同的物理主机上,协 ...

  2. Jetbrains系列产品2019.2.3最新激活方法

    Jetbrains系列产品2019.2.3最新激活方法[持续更新] 发表于 2018-08-25 | 分类于 软件调试 本站惯例:本文假定你知道Jetbrains家的产品.不知道可以问问搜索引擎. 大 ...

  3. malloc/free和new/delete详解与应用

    C++面试经常会问到关于malloc/free和new/delete的区别,网上有不同版本的解释,这里总结下并加上个人理解和使用. 两者相同点 1.都可以申请动态堆内存. 两者不同点 1.new/de ...

  4. Python列表排序方法reverse、sort、sorted详解

    python语言中的列表排序方法有三个:reverse反转/倒序排序.sort正序排序.sorted可以获取排序后的列表.在更高级列表排序中,后两中方法还可以加入条件参数进行排序. reverse() ...

  5. Apache Tomcat 安装与配置教程

    JDK的安装与配置 1. 从官网下载JDK https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213315 ...

  6. SAS学习笔记15 SAS导入数据(import txt csv xlsx spss)

  7. (转)从0移植uboot (一) _配置分析

    ref : https://www.cnblogs.com/xiaojiang1025/p/6106431.html 本人建议的uboot学习路线,先分析原有配置,根据现有的配置修改.增加有关的部分, ...

  8. SpringCloud Hystrix/Feign 整合 Hystrix 后首次请求失败解决方案

  9. MarkDown 语法记录

    Markdown是一种纯文本格式的标记语言.通过简单的标记语法,它可以使普通文本内容具有一定的格式. 为啥要用 MarkDown 呢? 优点 1.因为是纯文本,所以只要支持Markdown的地方都能获 ...

  10. pandas简介