Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

c# - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

enter image description here

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...