Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在同步方法thrwing错误中调用异步web api方法_C#_Asp.net_Asynchronous - Fatal编程技术网

C# 在同步方法thrwing错误中调用异步web api方法

C# 在同步方法thrwing错误中调用异步web api方法,c#,asp.net,asynchronous,C#,Asp.net,Asynchronous,我正在从C ASP.Net Web应用程序从同步方法调用异步Web api。我犯了一个错误。 发生了一个或多个错误。发送请求时出错。 基础连接已关闭:接收时发生意外错误。 无法从传输连接读取数据:远程主机强制关闭了现有连接 如果我使用异步方法从windows窗体应用程序调用相同的异步web api,web api将返回完美值。我必须在Web应用程序中使用同步方法,因此在Windows应用程序中成功测试的代码不能在Web应用程序中使用。 下面是代码的一部分 private string S

我正在从C ASP.Net Web应用程序从同步方法调用异步Web api。我犯了一个错误。 发生了一个或多个错误。发送请求时出错。 基础连接已关闭:接收时发生意外错误。 无法从传输连接读取数据:远程主机强制关闭了现有连接

如果我使用异步方法从windows窗体应用程序调用相同的异步web api,web api将返回完美值。我必须在Web应用程序中使用同步方法,因此在Windows应用程序中成功测试的代码不能在Web应用程序中使用。 下面是代码的一部分

    private string SECURE_API_Async(string WebApiURL, HttpContent httpContent, string[] apiNames, HTTP httpMethod, DictionaryEntry[] apiValues)
    {
        try
        {
            using (httpContent)
            {

                Last_HTTPRequestContent = httpContent == null ? "null" :  httpContent.ReadAsStringAsync().Result;
                HttpClient httpClient = new HttpClient(httpClientHandler);
                httpClient.BaseAddress = new Uri(WebApiURL);

                if (SessionTokenID != null)
                {
                    httpClient.DefaultRequestHeaders.Add("SessionTokenID", SessionTokenID);
                }

                httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"));
                string target_URI = "api";

                foreach (string apiName in apiNames)
                {
                    target_URI = string.Format("{0}/{1}",
                        target_URI,
                        apiName);
                }

                string target_URI_parameters = string.Empty;

                if (apiValues != null)
                {
                    target_URI_parameters = CreateParameterString(apiValues);
                }
                target_URI = (target_URI_parameters == string.Empty) ? target_URI : string.Format("{0}?{1}", target_URI, target_URI_parameters);

                _lastURL = httpClient.BaseAddress.ToString() + target_URI;
                Last_HTTPstatus = 0;
                Last_ReasonPhrase = string.Empty;
                Last_HTTPResponse = null;
                Last_HTTPResponseString = string.Empty;

                HttpResponseMessage response = null;

                try
                {
                    switch (httpMethod)
                    {
                        case HTTP.PUT:
                            {
                                response = httpClient.PutAsync(target_URI, httpContent).Result;
                                break;
                            }
调用PutAsync方法时出现错误

现在如果我将方法更改为

private **async** **Task<string>** SECURE_API_Async(string WebApiURL,       HttpContent httpContent, string[] apiNames, HTTP httpMethod, DictionaryEntry[] apiValues)
我得到了正确的回答。 我在网上搜索后学到的是

response = **await** httpClient.PutAsync(target_URI, httpContent);
当调用方法具有async关键字时,表示调用方法为aynchronous且

response = httpClient.PutAsync(target_URI, httpContent).**Result**;
当方法没有async关键字时,表示调用方法是同步的

应返回正确的响应消息。 但我的同步方法引发了潜在的连接错误。 有人能帮忙吗?
谢谢。

我传递了错误的凭证。因此,我得到一个错误,现有的连接被远程主机强制关闭。奇怪的错误。如果你在微软工作,其中一个令人头疼的问题就是。他们只是没有正确的错误。正确的错误应该是用户凭据错误

但问题是,如果使用Result,您可以调用异步方法 例如

HttpResponseMessage response=httpClient.PutAsynctarget_URI,httpContent.Result//来自同步方法的同步调用

HttpResponseMessage response=等待httpClient.PutAsynctarget_URI,httpContent//来自带有async关键字前缀的异步方法的同步调用

给出了相同的结果

response = httpClient.PutAsync(target_URI, httpContent).**Result**;