在Windows窗体中使用HttpClient类中的DelegatingHandler - 尚未设置内部处理程序

时间:2017-04-29 23:01:14

标签: c# asp.net-web-api

首先,我收到的错误消息如下: 尚未设置内部处理程序

我正在编写自定义消息处理程序来处理我的API的身份验证Cookie超时。例如,如果我的代码库调用API然后接收401,那么它应该重试登录URL以获取更新的cookie。我计划如下完成:

public class CkApiMessageHandler : DelegatingHandler
{
    private readonly string _email;
    private readonly string _password;
    private const string _loginPath = "Account/Login";

    public CkApiMessageHandler(string email, string password)
    {
        _email = email;
        _password = password;

        //InnerHandler = new HttpControllerDispatcher(null);
    }

    protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {

        var response = await base.SendAsync(request, cancellationToken);

        if(response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
        {
            Logging.Logger.LogInformation("Authentication cookie timed out");
            var baseAddress = request.RequestUri.Host;
            var loginPath = baseAddress + _loginPath;

            var originalRequest = request;

            var loginRequest = new HttpRequestMessage(new HttpMethod("POST"), loginPath);
            var dict = new Dictionary<string, string>();
            dict.Add("Email", _email);
            dict.Add("Password", _password);

            var form = new FormUrlEncodedContent(dict);
            loginRequest.Content = form;
            loginRequest.Headers.Clear();

            foreach(var header in originalRequest.Headers)
            {
                loginRequest.Headers.Add(header.Key, header.Value);
            }

            var loginResponse = await base.SendAsync(loginRequest, cancellationToken);

            if (loginResponse.IsSuccessStatusCode)
            {
                Logging.Logger.LogInformation("Successfully logged back in");
                return await base.SendAsync(originalRequest, cancellationToken);
            }
            else
            {
                Logging.Logger.LogException(new Exception("Failed to log back in"), "Could not log back in");
            }
        }

        return response;
    }


}  

我正在将用于直接访问数据库的旧服务转换为访问web api的服务,并且我试图在不必更改此类的任何消费者的情况下进行此操作。因此,奇怪的处理程序。以下是服务类的示例方法。

 public void UpdateClientInstallDatabaseAndServiceData(string dbAcronym, string clientVersion, string clientEnginePort, Guid installationId, string sqlserver)
    {
        var dict = new Dictionary<string, string>();
        dict.Add("dbAcronym", dbAcronym);
        dict.Add("clientVersion", clientVersion);
        dict.Add("clientEnginePort", clientEnginePort);
        dict.Add("desktopClientId", installationId.ToString());
        dict.Add("sqlServerIp", sqlserver);

        var form = new FormUrlEncodedContent(dict);

        _client.PostAsync(_apiPath + "/UpdateClientInstallDatabaseAndServiceData", form);
    }  

因此,如果上面的代码失败了401,服务将自动重新登录并重试原始代码,而服务的消费者不必检查请求并重新登录。消费者不应该知道它正在处理一个网络API。

我的问题是我的消息处理程序期望设置InnerHandler,这需要HttpConfiguration类的实例。当我查看规范here时,它似乎是用于设置web api服务的某种类。这很好,除了这个代码没有在api中执行..它正在Windows窗体应用程序上执行。委托处理程序正在HttpClient类中使用,如此......

_client = new HttpClient(new CKEnterprise.Common.CkApiMessageHandler(email, password));  

所以我的问题是如何让这个DelegatingHandler在web api项目的上下文之外完成它的工作?

进行更新。看起来我可以只使用HttpClientHandler类 https://blogs.msdn.microsoft.com/henrikn/2012/08/07/httpclient-httpclienthandler-and-webrequesthandler-explained/

3 个答案:

答案 0 :(得分:27)

我应该早点意识到这一点,但将内部处理程序设置为HttpClient使用的默认处理程序是有意义的。因此,在DelegatingHandler的子类中,您应该将内部处理程序设置为HttpClient使用的默认处理程序,如下所示:

    public CkApiMessageHandler(string email, string password, Guid moduleId)
    {
        _email = email;
        _password = password;
        _moduleId = moduleId;
        InnerHandler = new HttpClientHandler();

    }

答案 1 :(得分:2)

阿德里安答案是正确的。您必须为自定义处理程序设置一个内部处理程序,但是有一种更好的方法来设置内部处理程序。

public MyDelegatingHandler( ) : base( new HttpClientHandler( ) )

答案 2 :(得分:0)

或者您可以使用HttpClientFactory

_client  = HttpClientFactory.Create(new CKEnterprise.Common.CkApiMessageHandler(email, password))

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/httpclient-message-handlers#adding-message-handlers-to-the-client-pipeline