I'm following along with the tutorial at c-sharpcorner to add JWT authentication and running into an issue. I've re-done the entire tutorial and come up with the same issue in the same place.
After adding the extension class AddJWTTokenServicesExtensions in the MyApplication.Extensions namespace, and constructing the builder in Program.cs via var builder = WebApplication.CreateBuilder(args);, buider.Services is not recognizing the method AddJWTTokenServices
builder.Services.AddJWTTokenServices(builder.Configuration);
Error:
'IServiceCollection' does not contain a definition for 'AddJWTTokenServices' and no accessible extension method 'AddJWTTokenServices' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)
Is the tutorial wrong in extending the class, or am I missing something obvious?
Related
I have package references to EventFlow and EventFlow.AspNetCore. But can't register service:
services.AddEventFlow(ef =>
{
ef.AddDefaults(typeof(Startup).Assembly);
ef.AddAspNetCoreMetadataProviders();
});
Receiving an error:
CS1061 'IServiceCollection' does not contain a definition for 'AddEventFlow' and no accessible extension method 'AddEventFlow' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)
Usings as follows:
using EventFlow.AspNetCore.Extensions;
using EventFlow.AspNetCore.Configuration;
using EventFlow;
using EventFlow.Extensions;
I had same problem. You need to add EventFlow.DependencyInjection package to your project. AddEventFlow method is defined there.
According to the docs for .NET Core 5, there's a method SetBasePath and it's widely used in a bunch of blogs (example 1, example 2, example 3 etc.). There's no notion of it being a weird gotcha or such. However, when I try the syntax below, it's marked red and claimed not to be there.
using System;
using Microsoft.Extensions.Configuration;
static void Main(string[] args)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
IConfigurationBuilder builder = new ConfigurationBuilder();
builder.SetBasePath(path);
}
I'm not sure why this happens not what to do about it.
Steps to reproduce:
Create a vanilla console application in .NET Core 5 in C#.
Paste in the code in Program.cs replacing the file's contents.
Try to compile or execute.
The error received is like this.
Error CS1061
'ConfigurationBuilder' does not contain a definition for 'SetBasePath' and no accessible extension method 'SetBasePath' accepting a first argument of type 'ConfigurationBuilder' could be found (are you missing a using directive or an assembly reference?)
As far i can understand, I have all the prerequisites in place.
Remember that multiple types can contribute methods to a single type through Extension Methods. And so SetBasePath was never a method on the IConfigurationBuilder interface.
And multiple Assemblies can contribute types to the same namespace, and the docs say the type that defines the SetBasePath extension method is in:
FileConfigurationExtensions Class Definition Namespace:
Microsoft.Extensions.Configuration Assembly:
Microsoft.Extensions.Configuration.FileExtensions.dll
FileConfigurationExtensions Class
Which you can easilly verify is not present in your dependencies. So you're missing a NuGet package. Turns out this one is easy to find: Microsoft.Extensions.Configuration.FileExtensions
AddDefaultIdentity throwing error
'IServiceCollection' does not contain a definition for 'AddDefaultIdentity' and
no accessible extension method 'AddDefaultIdentity' accepting a first argument
of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)
WebApiDotNet.AddDefaultIdentity does not exist in IServiceCollection
'IServiceCollection' does not contain a definition for 'AddDefaultIdentity' and no accessible extension method 'AddDefaultIdentity' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)
I reproduced the issue when I installed Microsoft.AspNetCore.Identity.UI package higher than version 3.0 in a 3.0 project. Note: AddDefaultIdentity extension method is used to add the default UI service for Razor Pages and MVC.
You could use AddIdentity instead of AddDefaultIdentity like below:
services.AddIdentity<ApplicationUser,IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
If you want to use AddDefaultIdentity, you should update the version of Microsoft.AspNetCore.Identity.UI to 3.0.
For the difference between AddDefaultIdentity and AddIdentity , refer to https://medium.com/#xsoheilalizadeh/asp-net-core-identity-deep-dive-stores-e0e54291b51d
I had to change my project from .NET Core 2.1 to .NET 4.7.1 and I fixed almost all errors except the following that are still eluding me
'IApplicationBuilder' does not contain a definition for 'UseHsts' and no extension method 'UseHsts' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
'IApplicationBuilder' does not contain a definition for 'UseAuthentication' and no extension method 'UseAuthentication' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
'IApplicationBuilder' does not contain a definition for 'UseCookiePolicy' and no extension method 'UseCookiePolicy' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
'IApplicationBuilder' does not contain a definition for 'UseHttpsRedirection' and no extension method 'UseHttpsRedirection' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
'IApplicationBuilder' does not contain a definition for 'UseSession' and no extension method 'UseSession' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
'IApplicationBuilder' does not contain a definition for 'UseStaticFiles' and no extension method 'UseStaticFiles' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
'IServiceCollection' does not contain a definition for 'AddAuthentication' and no extension method 'AddAuthentication' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)
'IServiceCollection' does not contain a definition for 'AddSession' and no extension method 'AddSession' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)
I am using Microsoft.AspNetCore.Builder and Microsoft.Extensions.DependencyInjection. What else do I need to use/install/add to get IApplicationBuilder and IServiceCollection to work?
In my WebApi.csproj file I changed the target framework from netcoreapp2.1 to net471.
Old:
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
New:
<PropertyGroup>
<TargetFramework>net471</TargetFramework>
</PropertyGroup>
Following Rik's answer, I searched for more packages and found I had to add all of the following NuGet Packages:
Microsoft.AspNetCore.Authentication
Microsoft.AspNetCore.Session
Microsoft.AspNetCore.HttpsPolicy
Microsoft.AspNetCore.CookiePolicy
Microsoft.AspNetCore.StaticFiles
After I did that I got no more error messages.
The 'AddSession' and 'UseAuthentication' errors can be fixed by using the following nuget packages.
Microsoft.AspNetCore.Session
Microsoft.AspNetCore.Authentication
First of all, switching to 4.7.1 isn't downgrading, it's moving to a different platform. Something you probably don't need to do, unless you want to reuse the code for a Winforms or WPF application.
The Microsoft.Extensions.* packages target .NET Standard 2.0, not just Core, so you can use them in the Full framework as well.
The packages and classes aren't tied to ASP.NET either, except hosting. I'm using them in console applications.
It also means that if your class libraries target .NET Standard 2.0 they can be used by both platforms without changing the target. Perhaps you could move most of the code to .NET Standard 2.0 libraries and leave just the configuration to runtime-specific projects
You don't strictly need hosting to use all the other extensions, although it does provide a convenient API similar to the ASP.NET Core code. You can write your own Startup class with Configure etc methods and call them explicitly. In the end, what you need is access to the IServiceCollection so you can get configured services and run them.
You can add a generic .NET host by using the Microsoft.Extensions.Hosting package. Apart from the common API it adds the ability to host long-running services to non-ASP.NET Core projects.
This blog post shows how you can use the Hosting package to create a console application that starts a long-running service, similar to a Windows service or daemon, eg :
public static async Task Main(string[] args)
{
var hostBuilder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton<IBusControl>(serviceProvider =>
{
return MassTransit.Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var host = cfg.Host(new Uri("rabbitmq://localhost"), h =>
{
h.Username("guest");
h.Password("guest");
});
});
});
services.AddScoped<IHostedService, MassTransitHostedService>();
});
await hostBuilder.RunConsoleAsync();
}
The .NET Generic Host goes into more detail and shows more examples of logging, DI, configuration etc.
UPDATE
ASP.NET Core is not tied to .NET Core. You can use it in Full Framework projects as well, simply by changing the target runtime in the Project creationg dialog
Try to add "Microsoft.aspNetCore.App" to your *.csproj file ,this should solve the problem .
<ItemGroup>
<FrameworkReference Include="Microsoft.aspNetCore.App" />
</ItemGroup>
Please install Microsoft.AspNetCore.ResponseCaching from nuget package. It can fix your problem. After that described functionality will be available under .net framework.
I am currently working on a project with SignalR. On my production server, it is missing an automatically generated js-file in ~/signalr/hubs.
I wanted to try this solution:
SignalR not working on production server
But when I try to add this line to my application_start method in global.asax, my project will not build anymore. Although intellisense knows and suggests the extension method!
I referenced the namespace with using SignalR;. I tried calling it as an extension method and calling it as a static method with the object instance as the first parameter.
The exact error messages:
When calling as extension method:
Error 1 'System.Web.Routing.RouteCollection' does not contain a definition for 'MapHubs' and no extension method 'MapHubs' accepting a first argument of type 'System.Web.Routing.RouteCollection' could be found (are you missing a using directive or an assembly reference?)
When calling as static method:
Error 1 The type or namespace name 'RouteExtensions' does not exist in the namespace 'SignalR' (are you missing an assembly reference?)
Anybody knows why this happens and how to solve the problem? Many thanks in advance!
I'm pretty sure that it could be one of these reasons:
Your project requires a reference to an assembly that's not added to the project.
Is your project using the .NET Framework version that's compatible with the referenced assembly?
With your given info, it would be hard to determine the exact reason, but I believe that I provided you some helpful hints.
The fact that intellisense suggests you anything like an extension method won't mean that the project has the required references to work properly.