I am trying to create a WCF streaming example for testing purposes only, and I cannot be sure that it is actually streaming.
The example is very simple:
- The server returns large binary content (in this case, a PDF file)
- The client writes a large binary file to the file.
However, the problem seems to be that although I believe I have correctly configured the server and client for streaming:
- This is actually not streaming, because I'm in an
IOException with the message The maximum message size quota for incoming messages (65536) has been exceeded - Reads in increments of 1536 bytes, even when I set my stream buffer to 8192 (or any other size)
The full host code is here:
using System; using System.IO; using System.ServiceModel; using System.ServiceModel.Description; namespace WcfStreamingHost { internal class Program { private static void Main(string[] args) { BasicHttpBinding binding = new BasicHttpBinding(); binding.TransferMode = TransferMode.Streamed; binding.MaxBufferSize = 65536; binding.MaxReceivedMessageSize = 65536; binding.ReaderQuotas.MaxBytesPerRead = 65536; binding.SendTimeout = TimeSpan.FromMinutes(10); ServiceHost host = new ServiceHost(typeof (ContentProvider), new Uri("http://charles-m4600:1234/contentprovider")); host.Description.Behaviors.Add(new ServiceMetadataBehavior()); host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true; host.AddServiceEndpoint(typeof (IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); host.AddServiceEndpoint(typeof (IContentProvider), binding, "streamed"); host.Open(); Console.ReadKey(); } } [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)] public class ContentProvider : IContentProvider { #region IContentProvider Members [OperationBehavior(AutoDisposeParameters = true)] public Stream GetFile() { Stream stream = File.OpenRead("large_file.pdf"); return stream; } #endregion } [ServiceContract] public interface IContentProvider { [OperationContract] Stream GetFile(); } }
And the full client code is here:
using System; using System.IO; using System.ServiceModel; using WcfStreamingClient.LocalSvc; namespace WcfStreamingClient { internal class Program { private static void Main(string[] args) { BasicHttpBinding binding = new BasicHttpBinding(); binding.TransferMode = TransferMode.Streamed; binding.MaxBufferSize = 65536; binding.MaxReceivedMessageSize = 65536; binding.ReaderQuotas.MaxBytesPerRead = 65536; binding.ReceiveTimeout = TimeSpan.FromMinutes(10); EndpointAddress address = new EndpointAddress("http://charles-m4600:1234/contentprovider/streamed"); using (ContentProviderClient client = new ContentProviderClient(binding, address)) { using (Stream stream = client.GetFile()) { FileInfo file = new FileInfo("output.pdf"); if (file.Exists) { file.Delete(); } using (FileStream fileStream = file.Create()) { const int bufferLen = 8192; byte[] buffer = new byte[bufferLen]; int count = 0; int total = 0; while ((count = stream.Read(buffer, 0, bufferLen)) > 0) { fileStream.Write(buffer, 0, count); total += count; Console.Out.WriteLine("Read {0} bytes", total); } } } } } } }
I read several other posts on this issue, but cannot find any clues.
source share