开发者

Silverlight WCF works with BasicHttpBinding, "HTTP 415 Unsupported Media Type" exception when using CustomBinding/BinaryMessageEncoding

开发者 https://www.devze.com 2023-01-15 02:44 出处:网络
I have a silverlight app that talks to a WCF service inside of an ASP.NET website. The following code works:

I have a silverlight app that talks to a WCF service inside of an ASP.NET website. The following code works:

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

But I really want to take advantage of "binary encoding". To run the service with binary encoding, you cannot use the BasicHttpBinding, you need to use a CustomBinding! The following code is used in the same place, but yields an HTTP 415 Unsupported media type status from the web server. In a debugging session, no breakpoints are reached on the server.

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

I need help finding out why this setting doesnt work! BTW here is the service section in my web config on the server side:

  <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" />


I figured out how to enable BinaryMessageEncoding, and I can confirm BinaryMessageEncoding works correctly in IIS and visual studio web dev debug server.

The problem was the all-too-common WCF configuration problem. My "service name" was an arbitrary name, but it SHOULD have been the fully qualified name of the service class. Instead of throwing an exception, WCF was exposing the service under some default behavior, and not warning me that the configuration was not valid.

<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>


I don't understand all Silverlight capabilities but at the moment I think your service doesn't work at all. Your custom binding does not have any transport binding element which is mandatory.

Try to change your binding:

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

精彩评论

暂无评论...
验证码 换一张
取 消