I am adding HttpClient to DI as follows in my startup.cs
services.AddHttpClient("main", x => { x.BaseAddress = new Uri("https://localhost:44342/"); x.DefaultRequestHeaders.Add("key", "value"); }); A little lower I add the validator to scope
services.AddTransient<IValidator<Domain.Form.Form>, Shared.Validators.Form.Form>();
The validator is defined as follows
public class Form : Base<Domain.Form.Form> { public Form(HttpClient httpClient) { RuleFor(x => x.Name).NotEmpty().MustAsync(async (name, cancellation) => { var rtrn = true; rtrn = !bool.Parse(await httpClient.GetStringAsync("api/forms/checkifnameexists?name=" + name)); return rtrn; }).When(x => x.Id == default).WithMessage("A form with the same name already exists"); } } The issue I am having is when the Validator hit, the HttpClient that is injected does not have the BaseAddress and headers set.
What am I doing wrong?
IHttpClientFactorythat allows you to get the named instance, which you are not doing here. As a result, I would expect a completely unrelated HttpClient to be usedclass Formthe same thing asDomain.Form.Form? Why do you have a class sharing the same name as its namespace?Baseis, it looks like you're embuing a single class with both data-transfer and data-validation responsibility - this seems unwise.