WCF服务调用异步功能

时间:2018-03-29 04:09:24

标签: c# wcf asynchronous async-await httprequest

我有一个异步函数,用于向服务器发送请求消息。功能如下:

class http
{
    public async Task<string> HttpRequest()
    {
        HttpRequestMessage request = GetHttpRequestMessage();
        var str1 = await ExecuteRequest(request);
        return str1;
    }

    private async Task<string> ExecuteRequest(HttpRequestMessage request)
    {
        string result = string.Empty;
        try
        {
            using (HttpClient client = new HttpClient())
            {
                var responses = await client.SendAsync(request);
                responses.EnsureSuccessStatusCode();
                result = await responses.Content.ReadAsStringAsync();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        return result;
    }

    private const string _DataTypeJson = @"application/json";
    private HttpRequestMessage GetHttpRequestMessage()
    {
        Dictionary<string, string> headers = GetHeadersUsedForToken();
        string str = "https://test.com//tokens";
        Uri uri = new Uri(str);

        HttpRequestMessage request = new HttpRequestMessage
        {
            RequestUri = uri,
        };

        if (null != headers)
        {
            foreach (string key in headers.Keys)
            {
                request.Headers.Add(key, headers[key]);
            }
        }

        // Hard code Accpt type is Json
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(_DataTypeJson));

        request.Method = HttpMethod.Get;
        return request;
    }

    private Dictionary<string, string> GetHeadersUsedForToken()
    {
        return new Dictionary<string, string>
        {
            { "id", "abc" },
            { "secret", "***" }
        };
    }
}

此功能在控制台项目中运行正常,但当我尝试将此功能移至WCF服务,并尝试在服务中调用HttpRequest()功能时,

[ServiceContract]
public interface IService1
{
    [OperationContract]
    Task<string> GetData();
}
public class Service1 : IService1
{
    public Task<string> GetData()
    {
        http test = new http();
        return test.HttpRequest();
    }       
}

抛出异常:

Message An error occurred while sending the request.
InnerException  {"The underlying connection was closed: An unexpected error occurred on a send."}

1 个答案:

答案 0 :(得分:1)

使用async / await,你需要让它始终保持异步。

使服务成为基于任务的异步服务

[ServiceContract]
public interface IService1 {
    [OperationContract]
    Task<string> GetData();
}

通过这种方式,可以使其余的代码始终保持异步。

public class http {
    public async Task<string> HttpRequestAsync() {
        var request = GetHttpRequestMessage();
        string str1 = await ExecuteRequest(request);
        Console.WriteLine(str1);
        return str1;
    }

    //...code removed for brevity as they are already Task-based
}

现在应该允许在服务实现中使用该函数

public class Service1 : IService1 {
    public Task<string> GetData() {
        http test = new http(); 
        return test.HttpRequestAsync();
    }
}

在原始示例中,提供的代码是混合异步和阻塞调用.Result,这可能会导致死锁

参考Async/Await - Best Practices in Asynchronous Programming

我还建议让HttpClient静态并重用它,而不是创建多个实例并处理它们。

参考keep an instance of HttpClient for the lifetime of your application for each distinct API that you connect to.

更新:

另一种可能性是被调用的URL是HTTPS。

在通过HttpClient

发出请求之前,请考虑应用以下内容
//Handle TLS protocols
System.Net.ServicePointManager.SecurityProtocol =
    System.Net.SecurityProtocolType.Tls
    | System.Net.SecurityProtocolType.Tls11
    | System.Net.SecurityProtocolType.Tls12;