Can't find method app.UseStaticFiles() - c#

I'm following this guide and in step 4, I'm asked to add three lines to the project.json file (which I did and then ran dotnet restore getting a lot of updated packages).
When I enter the three lines in the Configure method, I get red lines on all of them. The methods aren't recognized, no intellisense provided etc.
I also noticed that in the example in the guide, the method signature only takes one parameter of IApplicationBuilder, whereas the one I got generated (using the yo aspnet command) looks like this.
Configure(IApplicationBuilder, IHostingEnvironment, ILoggerFactory);
I'm not sure how to resolve it. My guess is that there's a new version of something in the process (Yo, Generators, Core etc.) but I'm not entirely sure.
I've also found this blog where the method signature resembles the one I'm getting. However, the author of it suggest the same syntax that doesn't work for me. I'm guessing it's a matter of referencing the wrong libraries. How do I approach the issue?

For Asp.Net core MVC you need to install Nuget package
install-package "Microsoft.AspNetCore.StaticFiles"

That guide is outdated. The updated .Net core does not use project.json anymore which is unfortunate. Instead it is now part of csproj file. And to add the Static file library you have to add it to the project using nuget packet manager. And when you rebuild you will see an entry in csproj file for that library. I think the project.json was a great idea which was inline with core opt-in methodology, since it would allow intellisense to kick in to help you select from available libraries. And since csproj file cant be directly edited in solution you lose that feature.

Judging from the screenshots in the linked tutorial, its about ASP.NET Core RC1 (back then called ASP.NET 5 r1-final). You can easily recognize this on the package and namespace names. Microsoft.AspNet.* is used until rc1.
Starting with RC2 the packages were renamed to Microsoft.AspNetCore.* to make it clearer its a new framework and not that much compatible with legacy ASP.NET.
The UseIISPlatformHandler() isn't there anymore, it's now UseIISIntegration() within the Main(...) method:
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
And the packages the package is Microsoft.AspNetCore.Server.IISIntegration": "1.0.0" and "Microsoft.AspNetCore.Server.Kestrel": "1.0.1". For static files it's: "Microsoft.AspNetCore.StaticFiles": "1.0.0".
For the Configure overload: Configure(IApplicationBuilder); is default one, but you can add any other type which is registered with the dependency injection system (in ConfigureServices method), as it's a convention system (the startup.cs).

Related

Accessing SQL from Entity Framework Core Queries in ASP.NET Core

In a post in the last week or so, someone made reference to a post: http://rion.io/2016/10/19/accessing-entity-framework-core-queries-behind-the-scenes-in-asp-net-core/
The blog outlined using internals of EF to show the generated SQL for a given EF query. Having a tool like this is invaluable, and will help my EF dev team to write better code. However, as it uses internal and unsupported code, it will not build using EF 2.1.4. The reference to RelationalQueryModelVisitor is now gone, and the code will not build.
I am using using .net core 2.1 as well.
Is there another or similar approach available?
Thanks.
Use the class in this link, which does work in .NET Core 2.1. Yes, I know you said you tried it, but I just tried it and it worked, so there must be something else going wrong in your project. Tell us the compiler error you are getting and we can help further.
Here is what I did:
Created a new ASP.NET Core project and made sure it's targetting .NET Core 2.1.
Added Microsoft.EntityFrameworkCore version 2.1.4 from NuGet.
Created an IQueryableExtensions class and pasted the code.
It compiles.
The RelationalQueryModelVisitor class does still exist in .NET Core 2.1. The documentation shows it is still there (notice the "Entity Framework Core 2.1" in the top left of the docs) and the current source code on GitHub still shows it there.
Would using their logging functionality be sufficient?
https://learn.microsoft.com/en-us/ef/core/miscellaneous/logging
public static readonly LoggerFactory MyLoggerFactory
= new LoggerFactory(new[] {new ConsoleLoggerProvider((_, __) => true, true)});
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLoggerFactory(MyLoggerFactory) // Warning: Do not create a new ILoggerFactory instance each time
.UseSqlServer(...);
It's in the Microsoft.EntityFrameworkCore.Relational nuget package.

services.AddSwaggerGen() giving error

All I'm trying to do is add swagger to an ASP.Net Core application. I'm watching a tutorial and all I see them do is add services.AddSwaggerGen(); under the configure services area in the Startup.cs file. Like any normal service like MVC... But I get an error:
There is no argument given that corresponds to the required formal parameter 'setupAction'...
I don't see anyone supplying any kind of argument to services.AddSwaggerGen() so does anyone know what I'm missing here?
I've added the SwashBuckler.AspNetCore dependency so swagger is in the application. Just don't know why it's red and giving the above error.
I had problem, that
IServiceCollection does not contain a definition for 'AddSwaggerGen'
I turnes out, that I installed Swashbuckle.AspNetCore.Swagger nuget package instead of Swashbuckle.AspNetCore.
In .NET Core 3, there's some issues as discussed here. The solution is to add the following to the project file, replacing the prior version.
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc2" />
For the .Net core project, you need to install Four packages. Better to use Nuget package manager so it takes appropriate needed versions.
Microsoft.OpenApi
Swashbuckle.AspNetCore.Swagger
Swashbuckle.AspNetCore.SwaggerGen
Swashbuckle.AspNetCore.SwaggerUI
This happens because the implementation of AddSwaggerGen() extension method in ASP.NET Core requires you to provide Action<SwaggerGenOptions> argument which serves as setup action. For example:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});
You can learn more on how to setup Swagger with ASP.NET Core app here.
UPDATE:
In previous versions they had the AddSwaggerGen() extension method accepting no arguments, but this call was accompanied with call ConfigureSwaggerDocument(Action<SwaggerGenOptions> setupAction). I guess they just got rid of ConfigureSwaggerDocument and added setup action to AddSwaggerGen() method. That being said it seems your tutorial shows how to setup obsolete version of the Swagger.
Late answer, but as a new update on this question, I just noticed in order to make AddSwaggerGen works fine in .NET Core 3, you need to use OpenApiInfo instead of Info. So your new AddSwaggerGen should be something like this:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "The API", Version = "v1" });
});
Also you need to add the following to your using directives:
using Microsoft.OpenApi.Models;
Read more here https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-3.1&tabs=visual-studio
If you are experiencing this problem while trying to deploy a web app and adding the Swagger spec to API Management in Azure (from Visual Studio) -> check the local path for your repo - if it has a space in it, it will fail.
To fix - move your repo to a different location so the path has no space.
I was using version 6.1.4 of the Swashbuckle.AspNetCore package.
It was working for me by install Nuget package of "Swashbuckle.AspNetCore" in the current project.
https://github.com/domaindrivendev/Swashbuckle.AspNetCore

EF Core 1.1 to WebApi Core - Add-Migration fails

I think I’ve got all my dependencies running 1.1 properly but when I try to follow the steps here https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db I get an error running the Add-Migration command.
PM> Add-Migration InitialState
An error occurred while calling method ‘ConfigureServices’ on startup class ‘Startup’.
Consider using IDbContextFactory to override the initialization of the
DbContext at design-time. Error: This method could not find a user
secret ID because the application’s entry assembly is not set. Try
using the “.AddUserSecrets(Assembly assembly)” method instead. No
parameterless constructor was found on ‘ApplicationDbContext’. Either
add a parameterless constructor to ‘ApplicationDbContext’ or add an
implementation of ‘IDbContextFactory’ in the same assembly as
‘ApplicationDbContext’.
relevant sections of my project.json:
…
“Microsoft.EntityFrameworkCore”: “1.1.0”,
“Microsoft.EntityFrameworkCore.SqlServer”: “1.1.0”,
“Microsoft.EntityFrameworkCore.Design”: {
“type “: “build”,
“version”: “1.1.0”
},
“Microsoft.EntityFrameworkCore.Tools”: “1.1.0-preview4-final”
},
“tools”: {
“Microsoft.EntityFrameworkCore.Tools.DotNet”: “1.1.0-preview4-final”
},
My ApplicationDbContext does have the constructor:
public ApplicationDbContext(DbContextOptions options) : base(options)
{ }
and my Startup.cs does have the:
services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”)));
What else can it be?
The issue is related to the builder.AddUserSecrets() call. To fix perform the following steps:
Adding the user secret to the assembly (instead of just project.json) by adding attribute [assembly: UserSecretsId("aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed")] to Startup.cs
In Startup.cs replace builder.AddUserSecrets() with builder.AddUserSecrets<Startup>();
Reference: InvalidOperationException: Could not find 'UserSecretsIdAttribute' on assembly
An alternate solution:
I ran into the same problem and landed here. But I am writing an app from scratch comparing it at every step with the DotNetCore WebApp with Individual auth. that gets generated via wizard in VS 2017 hoping to follow latest practices. So it was strange when everything was identical in my code and the generated code and I was getting this exception. The generated code did not have Michael Dennis's suggested code in startup.cs, which does not mean he is wrong, it just means there was now a different way. Upon diving down further I found out that UserSecretsId was declared in myprojetname.csproj like follows:
<PropertyGroup>
<UserSecretsId>aspnet-mywebproect-006a401f-2fdc-4aad-abb1-12c00000004a</UserSecretsId>
</PropertyGroup>
After adding the entry in my project.csproj file, the issue was resolved.
Your issue is not related to EF Core, it's about User Secrets
Check your Startup.cs constructor - it contains AddUserSecrets() call. You can remove it. Later, when you read about User Secrets, you can add it back with correct configuration (I guess you have website template from 1.0.0, while referencing library 1.1.0 - it contains this bug fix)

Structuremap and ASP.NET core

I am using ASP.NET core and I'm trying to make use of structuremap for my IoC, but I seem to be having some issues. When I write a unit test that inits structuremap directly, everything works fine. When I print the configuration out to file, I can see that my setup is indeed registering everything correctly.
However, the populate seems to be giving me trouble.
I am trying to use StructureMap.Microsoft.DependencyInjection but I get an error when I build:
The dependency structuremap >= 4.4.0 could not be resolved.
I have StructureMap 4.4.1 installed in my project, including the project I installed the StructureMap.Microsoft.DependencyInjection library into (my Web API project).
So, I then took the files out of the project on github and loaded them into my solution, and removed the nuget package, but for some reason it is not working.
Here is a plunker with the relevant files
Ideally, I'd rather use the nuget package to do this, but I've never encountered a dependency issue when I have the actual dependency already installed.
EDIT: A few more details
When I write the results of container.WhatDoIHave() to a file, my classes are all shown correctly t0 be part of structuremap, however when I run container.AssertConfigurationIsValid(); is when I am getting errors about things correctly defined as reported by WhatDoIHave()
Here is what my configure method looks like
public IServiceProvider ConfigureIoC(IServiceCollection services)
{
var container = new Container();
container.Configure(config =>
{
// scan the webapi to register all the controllers
config.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
//this is an IoC configuration in another library that
// ties together a bunch of class libraries so they
// don't all have to be in my web project
IoC.BootStrapper.Configure(config, "MembershipReboot");
});
System.IO.File.WriteAllText(#"C:\users\{me}\documents\structuremapTest.txt", container.WhatDoIHave());
container.AssertConfigurationIsValid();
//Populate the container using the service collection
container.Populate(services);
return container.GetInstance<IServiceProvider>();
}
Rename the "StructureMap" package to "structuremap", seems like some weird issue with NuGet casing.
Cheers :)
See this issue on the StructureMap.Microsoft.DependencyInjection issue tracker:
The dependency structuremap >= 4.3.0 could not be resolved
https://github.com/structuremap/StructureMap.Microsoft.DependencyInjection/issues/17

Ninject & WebAPI: Getting started after install from NuGet

I installed the NuGet package Ninject Integration for WebApi2 via the Package Manager Console.
According to the wiki on the project's GitHub pages, this should have created a class called NinjectWebCommon in the App_Start folder. But it didn't.
That same GitHub wiki page explains what you should see so that you can add it yourself. So I tried creating the NinjectWebCommon class myself. The problem here is that I can't find the namespace for OnePerRequestModule in the following snippet.
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
The alternative, and according to that GitHub wiki page there's no effective difference between the two, is to modify the global.asax. So I tried this method and added some bindings like so
private void RegisterServices(IKernel kernel)
{
kernel.Bind<IEntityAccess>().To<EntityAccess>();
kernel.Bind<IDictionaryRepository>().To<DictionaryRepository>();
}
and found that my project builds, but when a request is sent to the WebAPI project it can't be found (i.e., I receive a 404 response).
So there's obviously some other piece of necessary wiring up which isn't in my project.
It appears that despite changing my global.asax to derive from NinjectHttpApplication, the global.asax is no longer being called.
Can anyone tell me what I might be missing? I've uninstalled and reinstalled the NuGet package a few times but the NinjectWebCommon class never appears, nor does my global.asax file ever get modified.
I've also read Ninject's own documentation but frustratingly this is a fairly large tutorial covering the basics of IoC and how Ninject operates rather than telling you how to get started with using Ninject.
There's also this SO post asking how to get started with Ninject for WebAPI, so it looks like something's amiss with their NuGet package.
And like that, I've just found the answer: there is an additional NuGet package which must be referenced:
Ninject.Web.WebApi.WebHost
Installing "Ninject integration for WebApi2" package is not sufficient.
This really should be more clearly explained.

Categories

Resources