客户端调用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>
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>