客户端调用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. db2 数据库配置HADR+TSA添加集群节点

    Db2配置HADR高可用+TSA添加集群节点 一.服务器资源 Master IP:10.78.10.1 数据库:dbclassSlave IP:10.78.10.2 数据库:dbclassVIP:10 ...

  2. c# 面向对象/继承关系设计

    继承 RTTI RTTI 概念 RTTI(Run Time Type Identification)即通过运行时类型识别,程序能够使用基类的指针或引用来检查着这些指针或引用所指的对象的实际派生类型. ...

  3. 怎么让桌面存到d盘

    1.找到桌面文件夹. (C:\Users\Administrator) [C盘],[用户].[“”系统账号“(如Administrator)文件夹],[桌面] 2.打开桌面文件夹的属性. 查看位置,修 ...

  4. MySQL提供的几种检索行数据的优化方式

    ICP(Index Condition Pushdown): 在MySQL5.6之前,存储引擎会通过遍历索引定位基表中 的行,然后返回给Server层,再去为这些数据进行WHERE后的条件过滤.MyS ...

  5. XDebug调试

    安装 访问Xdebug 点击download 找到RELEASES,点击 custom installation instructions. 在白色框框内填入phpinfo()出来的源码 点击Anal ...

  6. Vue起飞前的准备

    Vue起飞前的准备 一.什么是ECMAScript,以及es6的诞生? 1997年 ECMAScript 1.0 诞生 1999年12月 ECMAScript 3.0诞生,它 是一个巨大的成功,在业界 ...

  7. Linux文件删除,但是df -hT之后磁盘空间没有释放

    Linux 磁盘空间总是报警,查到到大文件,删除之后,df看到磁盘空间并没有释放. 查找了下发现系统对rm进行了alias   ,因为Linux对删除操作没有回收站机制,对rm操作进行了自定义,对删除 ...

  8. awr报告没有数据11.2.0.3

    有个朋友,反馈AWR没有数据: 咨询版本:oracle企业版本11.2.0.3 SQL> select * from v$version; BANNER -------------------- ...

  9. 使用隔离级别read committed隐式解决并发冲突

    1.使用rc的弊端:出现不可重复读 Oracle不可重复读 Oracle丢失修改 Oracle幻读 任何数据库的update  insert  delete都加排它锁 sql server的selec ...

  10. Html Agility Pack 使用 XPath 选择器

    想做一个爬虫程序,以前用的一直使用CSS选择器的html解析插件,最近做的项目想使用 Html Agility Pack 来做解析 Html Agility Pack使用 XPath 和 Linq 来 ...