在 windows 表单的 HttpClient class 中使用 DelegatingHandler - 尚未设置内部处理程序
Using a DelegatingHandler in HttpClient class from windows forms - Inner handler has not been set
首先,我收到的错误消息如下: 内部处理程序尚未设置
我正在编写自定义消息处理程序来处理我的 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,我正在尝试这样做而不必更改此 class.. 因此奇怪的处理程序。这是服务 class.
中的示例方法
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
class 的实例。当我查看规范 here 时,它似乎是某种类型的 class 用于设置网络 api 服务。这很好,只是这段代码不是在 api.. 中执行的。它是在 windows 表单应用程序中执行的。委托处理程序正在 HttpClient
class 中使用,就像这样...
_client = new HttpClient(new CKEnterprise.Common.CkApiMessageHandler(email, password));
所以我的问题是:我怎样才能让这个 DelegatingHandler 在 Web api 项目的上下文之外完成它的工作?
正在进行更新。 看来我可以只使用 HttpClientHandler class
https://blogs.msdn.microsoft.com/henrikn/2012/08/07/httpclient-httpclienthandler-and-webrequesthandler-explained/
我应该早点意识到这一点,但是将内部处理程序设置为 HttpClient
使用的 default 处理程序是有意义的。因此,在 DelegatingHandler
的 child class 中,您应该将内部处理程序设置为 HttpClient
使用的默认处理程序,如下所示:
public CkApiMessageHandler(string email, string password, Guid moduleId)
{
_email = email;
_password = password;
_moduleId = moduleId;
InnerHandler = new HttpClientHandler();
}
或者您可以使用 HttpClientFactory
_client = HttpClientFactory.Create(new CKEnterprise.Common.CkApiMessageHandler(email, password))
阿德里安回答正确。您必须为自定义处理程序设置内部处理程序,但有更好的方法来设置内部处理程序。
public MyDelegatingHandler( ) : base( new HttpClientHandler( ) )
首先,我收到的错误消息如下: 内部处理程序尚未设置
我正在编写自定义消息处理程序来处理我的 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,我正在尝试这样做而不必更改此 class.. 因此奇怪的处理程序。这是服务 class.
中的示例方法 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
class 的实例。当我查看规范 here 时,它似乎是某种类型的 class 用于设置网络 api 服务。这很好,只是这段代码不是在 api.. 中执行的。它是在 windows 表单应用程序中执行的。委托处理程序正在 HttpClient
class 中使用,就像这样...
_client = new HttpClient(new CKEnterprise.Common.CkApiMessageHandler(email, password));
所以我的问题是:我怎样才能让这个 DelegatingHandler 在 Web api 项目的上下文之外完成它的工作?
正在进行更新。 看来我可以只使用 HttpClientHandler class https://blogs.msdn.microsoft.com/henrikn/2012/08/07/httpclient-httpclienthandler-and-webrequesthandler-explained/
我应该早点意识到这一点,但是将内部处理程序设置为 HttpClient
使用的 default 处理程序是有意义的。因此,在 DelegatingHandler
的 child class 中,您应该将内部处理程序设置为 HttpClient
使用的默认处理程序,如下所示:
public CkApiMessageHandler(string email, string password, Guid moduleId)
{
_email = email;
_password = password;
_moduleId = moduleId;
InnerHandler = new HttpClientHandler();
}
或者您可以使用 HttpClientFactory
_client = HttpClientFactory.Create(new CKEnterprise.Common.CkApiMessageHandler(email, password))
阿德里安回答正确。您必须为自定义处理程序设置内部处理程序,但有更好的方法来设置内部处理程序。
public MyDelegatingHandler( ) : base( new HttpClientHandler( ) )