客户端调用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. ${__setProperty(row,rowNum)};不能在import XXX后面使用;

    如下 ${__javaScript只能用一次调用 excel.CWResultFile.CWOutputFile.wOutputFile("/Users/iot/1.xls", & ...

  2. zabbix硬件监控以及服务

    大家好今天给大家带来zabbix3.4.8监控主机,那么最近由于我个人的关系.没有及时的更新文章所以,很抱歉那么今天我分享的内容是zabbix3.4.8监控服务器.本章的具体监控服务器如下: 服务器的 ...

  3. asp.net core-11.WebHost的配置

    1.添加空的web网站 ,在目录下添加settings.json文件,在控制台上输出json的信息 public class Program { public static void Main(str ...

  4. uboot 代码执行顺序

    ref:http://blog.chinaunix.net/uid-30352139-id-5128405.html uboot: 2014.07 1.1    U-boot相关文件 boards.c ...

  5. ActiveMQ 消息确认

    一.事务性会话:当一个事务被提交的时候,确认自动发生 ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("t ...

  6. 使用DANT做FTP的转发代理

    FTP不能直接使用nginx进行转发,想了一些办法,最后决定使用iptalbes做DNAT,相关于把这个机器做一台防火墙,做一个NAT 1.启用ip_forward vim /etc/sysctl.c ...

  7. linux重启php服务

  8. android 项目集成 微信支付

    0.环境 下载 libammsdk.jar 1.需要的常量值 public class Constant { /** 微信中用到的常量值 */ public static final class WX ...

  9. Angularjs 中 ng-repeat 循环绑定事件

    用ng-repeat循环是如果有ng-click之类的事件需要传入参数我们一般这样写 <span class='del' ng-click="RemoveCost({{item.Id} ...

  10. TODO页面

    功能:1.根据数据显示当前所未完成的事件, 2.可通过输入框进行事件的添加,可标记已完成的事件并进行删除,可修改已添加的事件. ps:插件引入均使用本地文件,需改用静态CDN. 效果: 代码实现: & ...