客户端调用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. 通过jquery触发select自身的change事件

    ###通过jquery触发select自身的change事件 1.通过js来去触发select的change事件 代码如下:包含了html部分和js部分 //html部分 <select cla ...

  2. Python 【类与对象】

    类与对象 把类的个例就叫做实例 (instance),可理解为“实际的例子”类是某个特定的群体,实例是群体中某个具体的个体 Python中的对象等于类和实例的集合:即类可以看作是对象,实例也可以看作是 ...

  3. PCA降维笔记

    PCA降维笔记 一个非监督的机器学习算法 主要用于数据的降维 通过降维, 可以发现更便 于人类理解的特征 其他应用:可视化:去噪 PCA(Principal Component Analysis)是一 ...

  4. spring cloud微服务实践一

    最近在学习spring框架.其中spring cloud在微服务方面很火,所以在学习过程中,也做一些记录. 注:这一个系列的开发环境版本为 java1.8, spring boot2.x, sprin ...

  5. web&http协议&django初识

    1.什么是web应用 ​ Web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件. ​ 应用程序有两种模式C/S.B/S ...

  6. (五)mybatis之一对一关系

    一.需求分析 需求:查询订单信息关联查询用户信息 分析:一条订单只能由一个消费者来下单,也就是说从订单的角度来说与消费者是一对一的关系. 二.建数据库表和实体对象 其中订单表中的字段user_id对应 ...

  7. IdentityServer3 使用记录

    官方教程:https://identityserver.github.io/Documentation/docsv2/overview/mvcGettingStarted.html 1.是否启用 SS ...

  8. 使用swagger在netcorewebapi项目中自动生成文档

    一.背景 随着前后端分离模式大行其道,我们需要将后端接口撰写成文档提供给前端,前端可以查看我们的接口,并测试,提高我们的开发效率,减少无效的沟通.在此情况下,通过代码自动生成文档,这种需求应运而生,s ...

  9. 使用shared memory 计算矩阵乘法 (其实并没有加速多少)

    #include "cuda_runtime.h" #include "device_launch_parameters.h" #include "d ...

  10. Android中BroadcastReceiver的使用

    1.Android中广播分为静态注册和动态注册 2.下面是一个简单静态注册的例子 创建一个继承BroadcastReceiver的子类 public class DeviceBootReceiver ...