简体   繁体   中英

Unable to read bytes in WCF Streamed transfer mode in UWP

I have a UWP client application which talks to a Windows service application using WCF Streamed transfer mode:

var bnd = new NetTcpBinding(SecurityMode.None) { TransferMode = TransferMode.Streamed }

I have used Stream objects as request and response parameters in the contract method.

[OperationContract]
Stream RequestEncrypt(Stream data);

But I'm unable to read the byte array from the Stream object response in the UWP code:

using (var memStream = new MemoryStream())
{
    sourceStream.CopyTo(memStream);
    byteArray = memStream.ToArray();
}

In the above code, CopyTo method hangs forever. Control never gets returned to the next line. But this works fine in a Windows Console client application.

Does anyone have an idea?

What does it mean that the CopyTo method hangs forever? Has it encountered an exception? I tested and found no problems. Here is my demo:

[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    public interface ICalculator
    {
        [OperationContract]
        Stream RequestEncrypt();
    }
    public class Calcuator : ICalculator
    {
        public Stream RequestEncrypt()
        {
             
            string str = "i am a string";
            byte[] array = Encoding.ASCII.GetBytes(str);
            MemoryStream stream = new MemoryStream(array);
            StreamReader reader = new StreamReader(stream);
            return stream;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            // Step 1: Create a URI to serve as the base address.
            Uri baseAddress = new Uri("net.tcp://localhost:8000/GettingStarted/");

            // Step 2: Create a ServiceHost instance.
            ServiceHost selfHost = new ServiceHost(typeof(Calcuator), baseAddress);

            try
            {
                NetTcpBinding netTcpBinding = new NetTcpBinding(SecurityMode.None) { TransferMode = TransferMode.Streamed };
               
               
                // Step 3: Add a service endpoint.
                selfHost.AddServiceEndpoint(typeof(ICalculator), netTcpBinding, "CalculatorService");

                // Step 4: Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                // smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);

                Binding mexbinding = MetadataExchangeBindings.CreateMexTcpBinding();
                selfHost.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");

                // Step 5: Start the service.
                selfHost.Open();
                Console.WriteLine("The service is ready.");

                // Close the ServiceHost to stop the service.
                Console.WriteLine("Press <Enter> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();
                selfHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
        }
    }

This is WCF Service.

在此处输入图像描述

In UWP, I successfully obtained the Stream and converted it into a string.

UPDATE:

You can try to increase the message size quotas:

<bindings>
    <NetTcpBinding>
        <binding name="NetTcpBinding"
                 maxReceivedMessageSize="20000000" 
                 maxBufferSize="20000000"
                 maxBufferPoolSize="20000000">
            <readerQuotas maxDepth="32" 
                 maxArrayLength="200000000"
                 maxStringContentLength="200000000"/>
        </binding>
    </NetTcpBinding>
</bindings>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM