Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad response (400) with a WCF streaming service configured programmatically

this if my first attempt at using streaming for WCF, and I am struggling with the dreadful "The remote server returned an unexpected response: (400) Bad Request" response.

The trace viewer says that this is a System.ServiceModel.ProtocolException with message "There is a problem with the XML that was received from the network. See inner exception for more details." The inner exception type says "The body of the message cannot be read because it is empty."

Leaving everything else equal, if I switch to buffered mode on the client side, I am able to debug into the server code!

For some reason, I have to configure my service programmatically, as follows:

    public IUniverseFileService OpenProxy(string serviceUrl)
    {
        Debug.Assert(!string.IsNullOrEmpty(serviceUrl));

        var binding = new BasicHttpBinding();
        binding.Name = "basicHttpStream";
        binding.MaxReceivedMessageSize = 1000000;
        binding.TransferMode = TransferMode.Streamed;

        var channelFactory = 
           new ChannelFactory<localhost.IUniverseFileService>(
              binding, 
              new EndpointAddress(serviceUrl));

        return channelFactory.CreateChannel();
    }

While the server is configured as follows:

 <system.serviceModel>
    <!-- BEHAVIORS -->
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!-- SERVICES -->
    <services>
      <service behaviorConfiguration="serviceBehavior" name="Org.Acme.UniverseFileService">
        <endpoint address="" 
                  binding="basicHttpBinding" 
                  name="basicHttpStream" 
                  bindingConfiguration="httpLargeMessageStream"
                  contract="Org.Acme.RemoteCommand.Service.IUniverseFileService" /> 
        <endpoint address="mex" 
                  binding="mexHttpBinding" 
                  bindingConfiguration="" name="mexStream" 
                  contract="IMetadataExchange"/>
      </service>
    </services>
    <!-- BINDINGS -->
    <bindings>
      <basicHttpBinding>
        <binding name="httpLargeMessageStream" 
                 maxReceivedMessageSize="2147483647" 
                 maxBufferPoolSize="2147483647"
                 maxBufferSize="2147483647" 
                 transferMode="Streamed"/>
      </basicHttpBinding>
    </bindings>

I appreciate your help!

Stefano

like image 997
Stefano Ricciardi Avatar asked Dec 14 '22 02:12

Stefano Ricciardi


2 Answers

Everything started to work when I changed the transfer mode from Streamed to StreamedResponse as follows:

binding.TransferMode = TransferMode.StreamedResponse;

Still I don't understand why this works and Streamed does not, and why I am able to both send and receive a file stream from the server.

like image 104
Stefano Ricciardi Avatar answered Dec 16 '22 17:12

Stefano Ricciardi


Streaming mode is not supported by ASP.NET development server. You need to deploy the service to IIS (or a WCF Service Application) to use Streaming mode.

like image 27
ozdemiray Avatar answered Dec 16 '22 18:12

ozdemiray