WCF - 使用 await 键调用函数抛出异常

WCF - call a function with await key throws an exception

我收到以下错误:

This IRandomAccessStream does not support the GetInputStreamAt method because it requires cloning and this stream does not support cloning.

我的客户运行 Microsoft 导航服务器的测试 azure 网络服务。

SOAP is my Service Reference

ISoapService.cs

public interface ISoapService
{
    Task<string> ShipGetNewBatchCode(string macAddress);
}

SoapService.cs

public class SoapService : ISoapService
{
    private SOAP.scan_PortClient soapClient;

    public SoapService()
    {
        ConfigurateSoapService();
    }

    public async Task<string> ShipGetNewBatchCode(string macAddress)
    {
        var retValue = "";

        try
        {
            var response = await soapClient.ShipGetNewBatchCodeAsync(macAddress);
            retValue = response.return_value;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(@"              ERROR {0}", ex.Message);
        }

        return retValue;
    }

    private void ConfigurateSoapService()
    {
        var timespan = new TimeSpan(0, 1, 0);
        var binding = new BasicHttpBinding();
        var uri = new Uri("http://***aka.westeurope.cloudapp.azure.com:*2/**/**/****/**/***");
        var endpoint = new EndpointAddress(uri);

        binding.Name = "scan_Binding";
        binding.CloseTimeout = timespan;
        binding.OpenTimeout = timespan;
        binding.ReceiveTimeout = timespan;
        binding.SendTimeout = timespan;
        //binding.AllowCookies = false;
        ///binding.BypassProxyOnLocal = false;
        //binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
        binding.MaxBufferPoolSize = 524288;
        binding.MaxReceivedMessageSize = 65536;
        //binding.MessageEncoding = WSMessageEncoding.Text;
        binding.TextEncoding = Encoding.UTF8;
        //binding.UseDefaultWebProxy = true;

        binding.ReaderQuotas.MaxDepth = 32;
        binding.ReaderQuotas.MaxStringContentLength = 8192;
        binding.ReaderQuotas.MaxArrayLength = 16384;
        binding.ReaderQuotas.MaxBytesPerRead = 4096;
        binding.ReaderQuotas.MaxNameTableCharCount = 16384;

        binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;

        soapClient = new SOAP.scan_PortClient(binding, endpoint);
        soapClient.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential("****", "*****");
    }
}

视图模型Class

var soapService = DependencyService.Get<ISoapService>();
var a = await soapService.ShipGetNewBatchCode("11");

首先,我建议考虑在 PCL 中使用 ServiceReference 来获取通用客户端。但无论如何......解决方案如下

    public async Task<string> ShipGetNewBatchCode(string macAddress)
    {
        var retValue = "";

        try
        {
            string uri = "http://youruri";
            Windows.Web.Http.Filters.HttpBaseProtocolFilter hbpf = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
            Windows.Security.Credentials.PasswordCredential pcred = new Windows.Security.Credentials.PasswordCredential(uri, 
                "userName", "password");
            hbpf.ServerCredential = pcred;

            Windows.Web.Http.HttpClient hc = new Windows.Web.Http.HttpClient(hbpf);
            Windows.Web.Http.HttpRequestMessage hreqm = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Get, new Uri(uri));
            Windows.Web.Http.HttpResponseMessage hrespm = await hc.SendRequestAsync(hreqm);
            if (hrespm.StatusCode == Windows.Web.Http.HttpStatusCode.Ok)
            {
                soapClient = new SOAP.scan_PortClient();
                var response = await soapClient.ShipGetNewBatchCodeAsync(macAddress);
                retValue = response.return_value;
            }
            else
            {
                //process error
            }
        }
        catch (Exception ex)
        {

        }

        return retValue;
    }

我通过删除以下引用解决了这个问题:

Visual C++ 2015 Runtime for Universal Windows Platform Apps

我更新了

Microsoft.NETCore.UniversalWindowsPlatform

至版本 v5.2.2