When configuring services within MVC app you can set the compatibility version:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
How can I retrieve this version later in my own code to determine which compatibility version is in use?
There does not appear to be a corresponding GetCompatibilityVersion method anywhere and google/stackoverflow search was not my friend.
Any help appreciated.
The MVC compatibility version is actually stored in an instance of a class called MvcCompatibilityOptions. You can retrieve this object by locating it through the IoC container that is being used by the application – either ASP.NET Core's built-in one or a third party one.
For example, with the default IoC, you can retrieve it like this:
var compatibilityVersion = app.ApplicationServices.GetService<IOptions<MvcCompatibilityOptions>>().Value.CompatibilityVersion;
app is an instance of IApplicationBuilder.
Related
We have a system that we're moving from .NET Framework to .NET Core.
One piece of this is a logging system that we configure at startup using SimpleInjector. So in
App_Start\SimpleInjectorConfig we have:
private static void InitializeContainer(Container container)
{
var application = System.Web.Hosting.HostingEnvironment.SiteName;
var instance = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
// .. use application and instance in configuring the logging system
}
And the problem, of course, is that in .NET Core there isn't any System.Web.Hosting.
I've been browsing around, and I haven't found a way of getting an equivalent to HostingEnvironment.SiteName in .NET Core - at startup, before any endpoints are active.
Any ideas?
Check the discussion around SiteName on these two .net Core Git tickets. and that will help you to take the decision weather it is really required.
github.com/dotnet/aspnetcore/issues/7400 and
github.com/dotnet/aspnetcore/issues/17069
I'm upgrading an ASP.NET Core application from Framework 2.2 to 3.1. It also uses Entity Framework Core.
In the Startup.ConfigureServices method, there is this code:
services.AddEntityFrameworkNpgsql()
.AddDbContext<MainDbContext>(options => options
.UseNpgsql(Configuration.GetConnectionString("MainDbContext")));
Everything was fine with .NET Core 2.2. With .NET Core 3.1, I get this warning on every application start:
'AddEntityFramework*' was called on the service provider, but 'UseInternalServiceProvider' wasn't called in the DbContext options configuration. Remove the 'AddEntityFramework*' call as in most cases it's not needed and might cause conflicts with other products and services registered in the same service provider.
Looking up the UseInternalServiceProvider method, it looks like that should be called on the options to pass on the main service provider. Unfortunately, at this point, the service provider does not exist yet. It is just about to be built.
I don't understand what the problem is and what this warning wants to tell me, but failed to do. How can I make that warning go away? The web doesn't know about this message yet.
Remove AddEntityFrameworkNpgsql. The docs explain that :
Calling this method is no longer necessary when building most applications, including those that use dependency injection in ASP.NET or elsewhere. It is only needed when building the internal service provider for use with the method. This is not recommend other than for some advanced scenarios.
The actual Getting Started page For Npgsql shows there's no need for anything extra :
simply place the following in your ConfigureServices method in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// Other DI initializations
services.AddDbContext<BloggingContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("BloggingContext")));
}
I am trying to insert the localization using PO file. My project is on Asp.Net core and uses Orchard core.
I am following this guide - Configure Portable Object but I have a problem with the initial registration of the localization.
I should add the following code:
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
Technically, my project should have service.AddMvc() by default, but I am using services.AddOrchardCms() instead.
When I try to call the first code to register the localization:
...
services.AddOrchardCms();
services.AddMvc()..AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
...
I receive an error as the application "Can not find the index page" (It actually does not exist as I am using services.AddOrchardCms() and I think they have a conflict).
And, of course, if I don't insert the AddViewLocalization() the PO files don't work.
Does anyone know how can I solve this problem?
AddOrchardCms is internally calling AddMvc, by calling it again you are overwriting Orchard pipline with basic MVC pipeline. Try calling:
services
.AddOrchardCms()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
instead.
I created a asp.net core rc2 web application with user identity, however i'm confused how the account controller class is getting it's arguments, usermanager, signinmanager? Where are they being passed in from? I follow the call stack and I get external code, what external code is passing in these objects? Help me understand, how these 2 objects are being initialized.
In your Startup.cs you will see a call to this method
services.AddIdentity<ApplicationUser, IdentityRole>()
Afte reading the links on dependency injection suggested by #AndrésRobinet you can actually see where the services are being wired up.
This extension method lives in `IdentityServiceCollectionExtensions - You can then go and look at the source code for this method call (.NET core is on github):
line 67 of the AddIdentity method
services.TryAddScoped<SignInManager<TUser>, SignInManager<TUser>>();
what external code is passing in these objects?
Right-click on External code and click Show External Code - now you can get an idea of what is happening under the hood. the code down to and including the Kestrel webserver is also browsable/downloadable on github
image truncated
I'm new to ASP.NET Web API projects and also new to Couchbase. I'm trying to follow the instructions here: https://github.com/couchbaselabs/Linq2Couchbase/blob/master/docs/bucket-context.md
Using a blank project and the code provided, I get the error: "No parameterless constructor defined for this object". I know that I need to "inject" the BucketContext in some way, but I don't know where to put that, any ideas?
Perhaps following this tutorial will help you understand the Couchbase SDK in a bit more detail and let you understand the initialisation "challenge" you have.
http://blog.couchbase.com/2015/november/couchbase-dotnet-client-sdk-tutorial
In short, Couchbase Cluster is a "heavy" object and it's recommended to keep the object for the lifetime of the app. In WEB API that means that init should be done on app start. Depending on what version of ASP.NET you are using (ASP.NET 4.5 or ASP.NET vNEXT) init is done/recommended to be done different places.
ASP.NET 4.5 = global.asax
vNEXT = APP_START folder (look for other initialisations)
The above project/tutorial will explain step by step how to do the init.
When init is in place, linq2couchbase should work :)
Please let me know if this helped.