WCF - 使用await键调用函数会引发异常

时间:2016-11-15 12:26:57

标签: wcf xamarin async-await uwp navision

我收到以下错误:

  

此IRandomAccessStream不支持GetInputStreamAt方法   因为它需要克隆,而且这个流不支持克隆。

我的客户运行microsoft nav服务器的测试天蓝色网络服务。

  

SOAP是我的服务参考

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("****", "*****");
    }
}

ViewModel Class

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

2 个答案:

答案 0 :(得分:1)

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

  

通用Windows平台应用程序的Visual C ++ 2015运行时

我更新了

  

Microsoft.NETCore.UniversalWindowsPlatform

到版本v5.2.2

答案 1 :(得分:0)

首先,我建议考虑在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;
    }

enter image description here