Unable to resolve service for type 'System.String' while attempting to activate 'BuySell_20190423.Controllers.StockController' - asp.net-core

I have made many additions to this backend. Now the basic HTTPPOST from values controller copied to the stocks controller gives me this error in the chome window:
InvalidOperationException: Unable to resolve service for type
system.String' while attempting to activate
BuySell_20190423.Controllers.StockController'.
This is my stock controller post:
[HttpPost]
public void Post([FromBody]string value)
{
}
In postman I get the 500 internal error using this body,
{"UserName":"johndoe"}
Here is some of my startup.cs
namespace BuySell_20190423
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public static IConfiguration Configuration { get; private set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("CorsPolicy", corsBuilder =>
{
corsBuilder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}));
services.AddDbContext<Helpers.DataContext>(x => x.UseInMemoryDatabase("TestDb"));
services.AddDbContext<Models.StockContext>(opt => opt.UseInMemoryDatabase("item"));
services.AddDbContext<AppDbContext>(x => x.UseInMemoryDatabase("AppDb"));
// Auto Mapper Configurations
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
//services.AddScoped<Post>(_ => new MyService("value here"));
// configure strongly typed settings objects
var appSettingsSection = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);
// configure DI for application services
services.AddScoped<IUserService, UserService>();
// configure DI for string services
services.AddHttpClient();
app.UseCors("CorsPolicy");
app.UseHttpsRedirection();
app.UseMvc();
}
}
Here is the stocks controller constructor:
public StockController(String context, IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
_context = context;
}

You have to remove the string context parameter, the dependency injection system can't know what that should be.

Related

Difference between instance of HttpClient and IHttpClientFactory in .NET Core5

Below is my Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
And controller:
[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
private readonly HttpClient _httpClient;
private readonly IHttpClientFactory _httpClientFactory;
public TestController(HttpClient httpClient, IHttpClientFactory httpClientFactory)
{
_httpClient = httpClient;
_httpClientFactory = httpClientFactory;
}
[HttpGet]
[Route("getdata")]
public async Task GetData()
{
var baseAddress = "http://youtube.com";
var response = await _httpClient.GetAsync(baseAddress);
var client = _httpClientFactory.CreateClient();
response = await client.GetAsync(baseAddress);
}
}
As you can see I can get an instance of HttpClient in two ways:
By Injecting HttpClient
By Injecting IHttpClientFactory and then _httpClientFactory.CreateClient();
Though I am getting responses using both instances, my question is what is the difference between them? And when to use which one?
If you just post signal request.there's even no difference between injecting an instance of httpclient and creating a instance of httpclient with httpclientfactory .
If you use several instances of httpclient or reuse httpclient to post multiple requests, some problems may occur, using httpclient could handle these problems.
You could create httpclients with different settings as follows:
In startup.cs:
services.AddHttpClient("client_1", config =>
{
config.BaseAddress = new Uri("http://client_1.com");
config.DefaultRequestHeaders.Add("header_1", "header_1");
});
services.AddHttpClient("client_2", config =>
{
config.BaseAddress = new Uri("http://client_2.com");
config.DefaultRequestHeaders.Add("header_2", "header_2");
});
In your controller:
var client1 = _httpClientFactory.CreateClient("client_1");
var client2 = _httpClientFactory.CreateClient("client_2");

Custom IServiceProviderFactory in ASP.NET Core

I wrote a custom IServiceProviderFactory and installed it in Program.cs of a new app like this:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new PropertyInjectingContainerFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
It does lead to the correct configure function in Startup.cs getting called:
public void ConfigureContainer(PropertyInjectingContainerFactory.Builder builder)
{
builder.AddInjectAttribute<InjectDependencyAttribute>();
}
However, my created container only ever resolves two services: IConfiguration and IHost.
Everything else is resolved by the default container apparantly (for instance a service like ILogger<T> on a controller). What do I do wrong?
Here's the code for my custom factory - and please understand that I probably should be using an existing third-party container, but I also want to understand how this all fits together.
public class PropertyInjectingContainerFactory : IServiceProviderFactory<PropertyInjectingContainerFactory.Builder>
{
public Builder CreateBuilder(IServiceCollection services)
{
return new Builder(services);
}
public IServiceProvider CreateServiceProvider(Builder containerBuilder)
{
return containerBuilder.CreateServiceProvider();
}
public class Builder
{
internal readonly IServiceCollection services;
internal List<Type> attributeTypes = new List<Type>();
public Builder(IServiceCollection services)
{
this.services = services;
}
public Builder AddInjectAttribute<A>()
where A : Attribute
{
attributeTypes.Add(typeof(A));
return this;
}
public IServiceProvider CreateServiceProvider()
=> new PropertyInjectingServiceProvider(services.BuildServiceProvider(), attributeTypes.ToArray());
}
class PropertyInjectingServiceProvider : IServiceProvider
{
private readonly IServiceProvider services;
private readonly Type[] injectAttributes;
public PropertyInjectingServiceProvider(IServiceProvider services, Type[] injectAttributes)
{
this.services = services;
this.injectAttributes = injectAttributes;
}
// This function is only called for `IConfiguration` and `IHost` - why?
public object GetService(Type serviceType)
{
var service = services.GetService(serviceType);
InjectProperties(service);
return service;
}
private void InjectProperties(Object target)
{
var type = target.GetType();
var candidateProperties = type.GetProperties(System.Reflection.BindingFlags.Public);
var props = from p in candidateProperties
where injectAttributes.Any(a => p.GetCustomAttributes(a, true).Any())
select p;
foreach (var prop in props)
{
prop.SetValue(target, services.GetService(prop.PropertyType));
}
}
}
}

error 404 showing for each controller after user authorization via ldap

Scenario:
I'm implementing asp.net core 3.1 MVC project. I authorize my user via ldap Active Directory service. The user authenticates successfully and enter into my website. but after clicking on each menu item in order to see the related controller index it shows white page. I wrote on top of all my controller class [Authorize] keyword in order to let any authorized user to see all controllers.
My Problem is:
when user clicks on each menu item in home in order to see the related controller's index, it shows white page and when I publish my project on ldap server, it shows me 404 error. I appreciate if any one can suggest me a solution. It seems to the routing has problem but I'm not sure. I even wrote on top of my controller class the keyword [AllowAnonymous] but still I see white pages for index pages for each controller. Should I add anything to startup.cs for AutheticationHelper or CustomAuthenticationMiddleware as a service?
Here is my sign in method in account controller
namespace CSDDashboard.Controllers
{
[Route("[controller]/[action]")]
[AllowAnonymous]
public class AccountController : Controller
{
private readonly LdapUserManager _userManager;
private readonly LdapSignInManager _signInManager;
private readonly ILogger _logger;
public AccountController(
LdapUserManager userManager,
LdapSignInManager signInManager,
ILogger<AccountController> logger)
{
this._userManager = userManager;
this._signInManager = signInManager;
this._logger = logger;
}
[AllowAnonymous]
[HttpGet]
public async Task<IActionResult> Signin(string returnUrl = null)
{
// Clear the existing external cookie to ensure a clean login process
await this.HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
this.ViewData["ReturnUrl"] = returnUrl;
return this.View();
}
[AllowAnonymous]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Signin(SigninViewModel model, string returnUrl = null)
{
this.ViewData["ReturnUrl"] = returnUrl;
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "tehran.iri"))
{
// validate the user's credentials
//var result = ctx.ValidateCredentials(model.UserName, model.Password);
// try {
if (ctx.ValidateCredentials(model.UserName, model.Password))
{
// credentials are OK --> allow user in
HttpContext.Session.MarkAsAuthenticated(model.UserName);
//Added recently
Debug.Writeline(string.Format("Redirection to {0}", returnUrl);
return RedirectToLocal(returnUrl);
}
else
{
this.TempData["ErrorMessage"] = "The username and/or password are incorrect!";
return this.View(model);
// credentials aren't OK --> send back error message
}
}
}}}
Here is my middleware class and AuthenticationHelper class
public static class AuthenticationHelper
{
private const string SessionKey = "AuthenticationHelper.UserName";
public static void MarkAsAuthenticated(this Microsoft.AspNetCore.Http.ISession session, string authenticatedUserName)
{
session.SetString(SessionKey, authenticatedUserName);
}
public static ClaimsPrincipal GetAuthenticatedUser(this Microsoft.AspNetCore.Http.ISession session)
{
string authenticatedUserName = session.GetString(SessionKey);
if (string.IsNullOrEmpty(authenticatedUserName)) return null;
return new GenericPrincipal(new GenericIdentity(authenticatedUserName), Array.Empty<string>());
}
}
public class CustomAuthenticationMiddleware
{
private readonly RequestDelegate _next;
public CustomAuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
ClaimsPrincipal user = context.Session.GetAuthenticatedUser();
if (user != null) context.User = user;
await _next(context);
}
}
public static class CustomAuthenticationMiddlewareExtensions
{
public static IApplicationBuilder UseCustomAuthentication(this IApplicationBuilder builder)
{
return builder.UseMiddleware<CustomAuthenticationMiddleware>();
}
}
Here is my code in statrup.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<LdapSettings>(Configuration.GetSection("LdapSettings"));
services.AddDbContext<LdapDbContext>(options =>
options.UseSqlite(
Configuration.GetConnectionString("CSDDashboardContext")));
//-------------------------------------------------
services.AddIdentity<LdapUser, IdentityRole>()
.AddEntityFrameworkStores<LdapDbContext>()
.AddUserManager<LdapUserManager>()
.AddSignInManager<LdapSignInManager>()
.AddDefaultTokenProviders();
services.ConfigureApplicationCookie(options =>
{
options.Cookie.Name = "CSDDashboard";
options.LoginPath = "/Account/Signin"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
options.LogoutPath = "/Account/Signout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
options.SlidingExpiration = true;
options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
});
services.AddRazorPages();
services.AddTransient<ILdapService, LdapService>();
//-------------------------------------------------
services.AddControllersWithViews();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);//We set Time here
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
services.AddDistributedMemoryCache();
//Notice this is NOT the same class... Assuming this is a valid DBContext. You need to add this class as well.
services.AddDbContext<CSSDDashboardContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("CSDDashboardContext")));
services.AddDbContext<CSDDashboardContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("CSDDashboardContext")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// app.UseDeveloperExceptionPage(options);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSession();
app.UseRouting();
app.UseCustomAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
//Here are all of my controllers, but as it seems just I can uncomment one controller pattern here, I commented all the others
// pattern: "{controller=Applications}/{action=Index}/{id?}");
//pattern: "{controller=Home}/{action=Index}/{id?}");
// pattern: "{controller=ApiApplications}/{action=Index}/{id?}");
pattern: "{controller=Gates}/{action=Index}/{id?}");

Can't get Odata 7.1 working in web api (.net core 2.2)

I'm having trouble implementing OData in my Web API project.
I'm using .NET Core 2.2 and OData (Microsoft.AspNetCore.OData v7.1.0).
(I also have AutoMapper installed, but I don't think this is relevant)
The problem is that the response is never enriched with the extra properties (like odata.context and odata.count). Also, adding ?count=true doesn't do anything either.
Does anyone have any idea why this is happening?
This is my Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
/* AutoMapper config */
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
services.AddMvc(opt => opt.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddOData();
var connection = #"snipped-connection-string";
services.AddDbContext<AllOnBoardContext>
(options => options.UseSqlServer(connection));
/* CORS for local development, not ideal, I know */
services.AddCors(setup =>
{
setup.AddPolicy(AllowCorsPolicyName, builder =>
{
builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
});
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseCors(AllowCorsPolicyName);
app.UseMvc(routeBuilder =>
{
routeBuilder.Select().Filter().OrderBy().Expand().Count().MaxTop(10);
routeBuilder.MapODataServiceRoute("api", "api", GetEdmModel());
});
}
private static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Tenant>("Tenants");
return builder.GetEdmModel();
}
And this is my `Controller´ class:
public class TenantController : ControllerBase
{
private ITenantService tenantService;
public TenantController(ITenantService tenantService, IMapper mapper) : base(mapper, tenantService)
{
this.tenantService = tenantService;
}
[HttpGet]
[EnableQuery()]
public ActionResult<IEnumerable<TDto>> Get()
{
return Service.GetAll().ToList();
}
}
First of all, OData query parameters always start with a $. So in your case, it is ?$count=true.
Next, make sure that every Controller inherits from ODataController:
[ODataRoutePrefix("Tenants")]
public class TenantController : ODataController
{
private ITenantService tenantService;
public TenantController(ITenantService tenantService, IMapper mapper) : base(mapper, tenantService)
{
this.tenantService = tenantService;
}
[HttpGet]
[ODataRoute]
[EnableQuery]
public ActionResult<IEnumerable<TDto>> Get()
{
return Service.GetAll().ToList();
}
}
Annotations will help you to control OData. The prefix string of the ODataRoutePrefixAttribute specifies the OData URL path template that this action handles (in your case http://<host:port>/api/Tenants) and with the ODataRouteAttribute it is possible to specify the OData URL path template that this action handles.

asp.net core custom IRouter Dependency Injection

I'm creating a custom routing in asp.net core 2, where I check the path in a DB and update action and controller to the desired one.
I have this custom IRouter defined like this
public interface IRouteCustom : IRouter
{
}
public class RouteCustom : IRouteCustom
{
private readonly IRouter _innerRouter;
private readonly IMemoryCache _memoryCache;
private readonly IUnitOfWork _unitOfWork;
public RouteCustom(IRouter innerRouter, IMemoryCache memoryCache, IUnitOfWork unitOfWork)
{
_innerRouter = innerRouter ?? throw new ArgumentNullException(nameof(innerRouter));
_memoryCache = memoryCache;
_unitOfWork = unitOfWork;
}
public async Task RouteAsync(RouteContext context)
{
I check the routes in the DB using the _unitOfWork
}
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
Also I do the same here...
}
}
I have no problem with those functions and I'm able to select controller and action.
My problem is how to access the database, since I can't inject the IUnitOfWork dependency into de custom router.
I'm getting this error message:
'Cannot resolve 'IUnitOfWork' from root provider because it requires scoped service 'DbContext'.'
I have my ConfigureServices like this
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DbContext>(options => options.UseMySQL(configuration.GetConnectionString("DefaultClient")));
services.AddIdentity<Domain.EntitiesClient.Entities.ApplicationUser, Domain.EntitiesClient.Entities.ApplicationRole>().AddEntityFrameworkStores<DbContext>().AddDefaultTokenProviders();
services.AddScoped<IDbContext, DbContext>();
services.AddTransient<IUnitOfWork, UnitOfWork>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddMemoryCache();
services.AddMvc();
/*route custom*/
var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("es-ES"), new CultureInfo("it-IT") };
var optionsCulture = new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture("en-US", "en-US"), SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures };
optionsCulture.RequestCultureProviders = new IRequestCultureProvider[] { new RouteDataRequestCultureProvider() { RouteDataStringKey = "culture", Options = optionsCulture } };
services.AddSingleton(optionsCulture);
}
And the Configure
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.Routes.Add(new RouteCustom(routes.DefaultHandler
, routes.ServiceProvider.GetRequiredService<IMemoryCache>()
, app.ApplicationServices.GetService<IUnitOfWork>()
));
routes.MapRoute(name: "areas", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
});
}
The problem is here
app.ApplicationServices.GetService<IUnitOfWork>()
I need to inject the IUnitOfWork in order to check the database, but I don't know how to do it. In other Middlewares I could inject the IUnitOfWork directly in the function, but in this case I can't do it in the
public async Task RouteAsync(RouteContext context)
How can I achieve this? I'm sure I'm doing something wrong here, but I have been reading a lot of articles and can't figure out the way.
Thanks.
UPDATE: POSSIBLE SOLUTION
The only solution I can think is remove the injection into the IRouter and get the service "manually" inside the RouteAsync method.
public async Task RouteAsync(RouteContext context)
{
var unitOfWork = context.HttpContext.RequestServices.GetRequiredService<IUnitOfWork>()
var routes = unitOfWork.Router.GetAll();
...
}
This way we have access to the database and it works good.
Is it a good approach?