使用CustomBinding / BinaryMessageEncoding时,Silverlight WCF使用BasicHttpBinding,“HTTP 415 Unsupported Media Type”异常

时间:2010-09-05 23:23:01

标签: silverlight wcf

我有一个Silverlight应用程序,它与ASP.NET网站内的WCF服务进行通信。以下代码有效:

        var service = new ChannelFactory<IService>(new BasicHttpBinding()
                                                                    {
                                                                        MaxReceivedMessageSize = int.MaxValue
                                                                    }, 
                                                                    new EndpointAddress(Settings.ServiceUrl)).CreateChannel();

但我真的想利用“二进制编码”。要使用二进制编码运行服务,您不能使用BasicHttpBinding,您需要使用CustomBinding!以下代码在同一位置使用,但从Web服务器生成HTTP 415 Unsupported媒体类型状态。在调试会话中,服务器上没有到达断点。

            var service = new ChannelFactory<IService>(new CustomBinding(new BinaryMessageEncodingBindingElement(), new HttpTransportBindingElement()
                                                                                                                                 {
                                                                                                                                     MaxReceivedMessageSize = int.MaxValue
                                                                                                                                 }),
                                                                    new EndpointAddress(Settings.ServiceUrl)).CreateChannel(); 

我需要帮助找出为什么这个设置不起作用! BTW这是我服务器端web配置中的服务部分:

  <system.serviceModel>
<bindings>
  <customBinding>
    <binding name="myBinding">
      <binaryMessageEncoding />
      <httpTransport authenticationScheme="Negotiate"/>
    </binding>
  </customBinding>
</bindings>
<services>
  <service name="myService">
    <endpoint address="" binding="customBinding"  bindingConfiguration="myBinding" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="wcfServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

2 个答案:

答案 0 :(得分:3)

我想出了如何启用BinaryMessageEncoding,我可以确认BinaryMessageEncoding在IIS和visual studio web dev调试服务器中正常工作。

问题是非常常见的WCF配置问题。我的“服务名称”是一个任意名称,但它应该是服务类的完全限定名称。 WCF没有抛出异常,而是在某些默认行为下公开服务,并没有警告我配置无效。

<bindings>
<customBinding>
        <binding name="myBinding" >
          <binaryMessageEncoding >
            <!--readerQuotas are used to set upper limits on message payload-->
            <readerQuotas 
              maxDepth="2147483647" 
              maxStringContentLength="2147483647" 
              maxArrayLength="2147483647" 
              maxBytesPerRead="2147483647" 
              maxNameTableCharCount="2147483647"
            />
          </binaryMessageEncoding>
          <httpTransport authenticationScheme="Ntlm"  maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647"/>
        </binding>
      </customBinding>
    </bindings>
    <services>
      <!--the service name must be the fully-qualified name of the service class-->
      <service name="MyProgram.DataService">
        <!--this line allows metatada exchange-->
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
        <endpoint address="" binding="customBinding" bindingConfiguration="myBinding" contract="MyProgram.IDataService" />
      </service>
    </services>

答案 1 :(得分:0)

我不了解所有Silverlight功能,但目前我认为您的服务根本不起作用。您的自定义绑定没有任何必须的传输绑定元素。

尝试更改绑定:

<bindings> 
  <customBinding> 
    <binding name="myBinding"> 
      <binaryMessageEncoding /> 
      <httpTransport />
    </binding> 
  </customBinding> 
</bindings>