简体   繁体   English

无法读取 WCF 中的字节 UWP 中的流传输模式

[英]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:我有一个 UWP 客户端应用程序,它使用 WCF 流传输模式与 Windows 服务应用程序对话:

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

I have used Stream objects as request and response parameters in the contract method.我在合约方法中使用了 Stream 对象作为请求和响应参数。

[OperationContract]
Stream RequestEncrypt(Stream data);

But I'm unable to read the byte array from the Stream object response in the UWP code:但我无法从 UWP 代码中的 Stream object 响应中读取字节数组:

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

In the above code, CopyTo method hangs forever.在上面的代码中, CopyTo 方法永远挂起。 Control never gets returned to the next line.控制永远不会返回到下一行。 But this works fine in a Windows Console client application.但这在 Windows 控制台客户端应用程序中运行良好。

Does anyone have an idea?有人有想法吗?

What does it mean that the CopyTo method hangs forever? CopyTo 方法永远挂起是什么意思? 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.这是 WCF 服务。

在此处输入图像描述

In UWP, I successfully obtained the Stream and converted it into a string.在UWP中,我成功得到了Stream并转换成字符串。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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