I have this startup.cs file that adds the different services to my application. One of the services I need to add is AddStackExchangeRedisCache but I am getting an error that says
"IServiceCollection does not contain a definition for AddStackExchangeRedisCache and no accessible extension method AddStackExchangeRedisCache accepting a first argument of type IServiceCollection could be found (are you missing a using directive or an asssembly reference?)?"
I have the following stack exchange redis refernce in my CSPROJ
<PackageReference Include="StackExchange.Redis" Version="2.6.66" />
And the method where I am trying to add the service is as follows.
public void ConfigureServices(IServiceCollection services) {
services.AddStackExchangeRedisCache(options =>
{
// some code here
}
}
Install Microsoft.Extensions.Caching.StackExchangeRedis nuget package.
Related
Before I tested this line on a web application in core 5.
services.AddIdentity<Operator, IdentityRole>().AddEntityFrameworkStores<StorageContext>().AddDefaultTokenProviders();
This works fine in startup class.
Now I want to know how to implement it in windows form, Core 7. Because I just get this error -
'IServiceCollection' does not contain a definition for 'AddIdentity'
and no accessible extension method 'AddIdentity' accepting a first
argument of type 'IServiceCollection' could be found (are you missing
a using directive or an assembly reference?)
Am I missing assemblies? what are they?
this is my code now -
static IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddScoped<IStorageRepository, StorageRepository>();
services.AddDbContext<StorageContext>(option =>
{
option.EnableSensitiveDataLogging(true);
option.UseSqlServer(configuration["Data:Storage:ConnectionString"]);
});
services.AddIdentity<Operator, IdentityRole>().AddEntityFrameworkStores<StorageContext>().AddDefaultTokenProviders();
});
}
Does this AddIdentity class works in Winforms?
Just try to install Microsoft.AspNetCore.Identity package.
I had the same problem, just try to add this nugget package Microsoft.AspNetCore.Identity.UI. I add it and it start see it
The call is ambiguous between the following methods or properties:
'Microsoft.EntityFrameworkCore.MySQLDbContextOptionsExtensions.UseMySQL(Microsoft.EntityFrameworkCore.DbContextOptionsBuilder,
string,
System.Action<MySql.EntityFrameworkCore.Infrastructure.MySQLDbContextOptionsBuilder>)'
and
'Microsoft.EntityFrameworkCore.MySQLDbContextOptionsExtensions.UseMySQL(Microsoft.EntityFrameworkCore.DbContextOptionsBuilder,
string,
System.Action<MySql.Data.EntityFrameworkCore.Infraestructure.MySQLDbContextOptionsBuilder>)'
My code is
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<ProjectDataContext>(options => options.UseMySQL(_config.GetConnectionString("DefaultConnection")));
services.AddMvc();
}
From the error is is clear that you are referencing thes two packages in the using section:
using MySql.EntityFrameworkCore.Infrastructure.MySQLDbContextOptionsBuilder;
using MySql.Data.EntityFrameworkCore.Infraestructure.MySQLDbContextOptionsBuilder;
To resolve this, either remove the one you don't want to use, or (if both are needed in your class) call the intended method directly and not as an extension one. For example:
services.AddDbContextPool<ProjectDataContext>(options =>
MySql.EntityFrameworkCore.Infrastructure.MySQLDbContextOptionsBuilder.UseMySQL(options, _config.GetConnectionString("DefaultConnection")));
//or
services.AddDbContextPool<ProjectDataContext>(options =>
MySql.Data.EntityFrameworkCore.Infraestructure.MySQLDbContextOptionsBuilder.UseMySQL(options, _config.GetConnectionString("DefaultConnection")));
I am setting up Aws SQS in asp.net core 2.22 project. I am adding aws configuration lines in my Program.cs file. Here is my Program.cs file.
using Amazon.SQS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace analytics
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; set;}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddCors(c =>
{
c.AddPolicy("AllowOrigin", options =>
{
options.AllowAnyOrigin();
});
});
// AWS Configuration
var awsoptions = Configuration.GetAWSOptions();
services.AddDefaultAWSOptions(awsoptions);
services.AddAWSService<IAmazonSQS>();
// Worker Service
// services.AddHostedService<Worker>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
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.UseMvc();
app.UseCors(options =>
{
options.AllowAnyOrigin();
});
}
}
}
So i am receiving these errors on console
Startup.cs(40,52): error CS1061: 'IConfiguration' does not contain a definition for 'GetAWSOptions'
and no accessible extension method 'GetAWSOptions' accepting a first argument of type
'IConfiguration' could be found (are you missing a using directive or an assembly reference?)
[D:\OfficeProjects\beelinksanalytics\analytics.csproj]
Startup.cs(41,30): error CS1061: 'IServiceCollection' does not contain a definition for
'AddDefaultAWSOptions' and no accessible extension method 'AddDefaultAWSOptions' accepting a first
argument of type 'IServiceCollection' could be found (are you missing a using directive or an
assembly reference?) [D:\OfficeProjects\beelinksanalytics\analytics.csproj]
Startup.cs(42,30): error CS1061: 'IServiceCollection' does not contain a definition for
'AddAWSService' and no accessible extension method 'AddAWSService' accepting a first argument of
type 'IServiceCollection' could be found (are you missing a using directive or an assembly
reference?) [D:\OfficeProjects\beelinksanalytics\analytics.csproj]
Basically i want to implement Aws SQS service to listen queue messages and want to perform some actions based on those messages. So i am struggling to configure Aws Sdk and aws sqs in asp.net core 2.2 project
I didn't find any relevant documentation of how to configure aws sqs in asp.net core 2.2. Please tell me what's wrong with this?
just run this in your command line as mentioned #panagiotis-kanavos (select version that you want here)
dotnet add package AWSSDK.Extensions.NETCore.Setup --version 3.3.101
verify that the reference was added in your .csproj
this worked for me, I hope as well for you
I'm currently trying to create a Logger so I can inject it in Unit Tests. I'm following https://stackoverflow.com/a/43425633/1057052, and it used to work! I then moved the project and reestablished the dependencies, and now I'm getting
'ServiceCollection' does not contain a definition for 'AddLogging' and
no accessible extension method 'AddLogging' accepting a first argument
of type 'ServiceCollection' could be found (are you missing a using
directive or an assembly reference?)
There must be something silly I'm missing. Currently under ASP.NET Core 2.2, and I believe I have assigned the correct dependencies.
https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection?view=aspnetcore-2.2
https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.loggingservicecollectionextensions.addlogging?view=aspnetcore-2.2
I've been reinstalling for the past hour our so! Can't nail what the problem is
Here's the code:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Planificacion.UnitTest.Config
{
public class LoggerTestConfig
{
private LoggerTestConfig()
{
}
// https://stackoverflow.com/a/43425633/1057052
public static ILogger<T> GetLoggerConfig<T>() where T : class
{
var serviceProvider = new ServiceCollection()
.AddLogging()
.BuildServiceProvider();
var factory = serviceProvider.GetService<ILoggerFactory>();
return factory.CreateLogger<T>();
}
}
}
The image highlights that the dependency injection dll is referenced, but desired LoggingServiceCollectionExtensions.AddLogging Method as shown in the links provided, indicates
Namespace: Microsoft.Extensions.DependencyInjection
Assembly: Microsoft.Extensions.Logging.dll <----NOTE THIS
Which is not referenced as shown in the image.
Add a reference to the Microsoft.Extensions.Logging.dll assembly stated above.
I have the API configuration splitted into two projects (the principal and the secondary). In the principal Startup.cs, I configure Autofac, and in the secondary, I use the IServiceCollection returned by the principal.
For register an Autofac module, I should code:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var container = new ContainerBuilder();
container.Populate(services);
container.RegisterModule(new ApplicationModule());
return new AutofacServiceProvider(container.Build()); // Instead of 'return services;'
}
And if you see the returned type, it's IServiceProvider instead of IServiceCollection. How can I register a module, but return IServiceCollection? I have tried to cast from IServiceProvider to IServiceCollection, but an exception is raised.
It looks like to me your autofac setup is incorrect. Please have a look at what ive outlined below.
Program.cs
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureServices(collection => collection.AddAutofac()) //Important
.Build();
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void ConfigureContainer(ContainerBuilder builder)
{
container.RegisterModule(new ApplicationModule());
}
Because of the .ConfigureServices(collection => collection.AddAutofac()) line in your program cs the ConfigureContainer(ContainerBuilder builder) is automatically called (put a breakpoint :) ) in your startup class and the container is built and registered within your current api, if you replicate this across to your secondary api you wont have to worry about passing your service collection around from one api to another and each one will register its own dependencies.
Also ensure you have Autofac.Extensions.DependencyInjection nuget package installed!
As far as I see, your code is correct. You have to create autofac Module in your second project, and then register it in the first project by adding following code to the ConfigureServices method:
container.RegisterModule<YourModuleInSecondProject>();
Please find a good article about autofac basics here.
Also please mind that dotnet core comes with built-in dependency injector, you can find a simple tutorial here.