ASP.NET 5 System.Runtime.Caching - asp.net-core

I am converting an existing .Net 4.5 MVC 5 project to a new ASP.NET 5 project. One of my files is referencing the System.Runtime.Caching namespace but on moving this file to the new project this namespace cannot be found.
I have added System.Runtime as a dependency in the new project, but the .Caching bit seems to be missing from this. Has anybody experienced a similar problem?

You need to bring in 'Microsoft.Extensions.Caching.Memory' using the following line in your project JSON.
"dependencies": {
"Microsoft.Extensions.Caching.Memory": "1.0.0"
}
Current documentation can be found here.
https://docs.asp.net/en/latest/performance/caching/memory.html

To use the System.Runtime.Caching namespace in an ASP.NET application, you must add a reference to the namespace.
To add a reference to the Website
In Solution Explorer, right-click the name of the Web site and then
click Add Reference.
Select the .NET tab, select System.Runtime.Caching, and then click
OK.
Ref: https://msdn.microsoft.com/en-us/library/ff477235(v=vs.110).aspx#Anchor_2

Related

The type name 'ConfigurationManager' could not be found in the namespace 'System.Configuration'. This type has been forwarded

I received this error in a .NET 5.0 project using Visual Studio 2019 (tried on Community/Student edition as well as Professional).
"The type name 'ConfigurationManager' could not be found in the namespace 'System.Configuration'. This type has been forwarded..."
Many of the answers I found said that you needed to add "using System.Configuration;" as well as add the assembly namespace as a reference through Project -> Add Reference. This answer did not work for me as there is no "References" folder or any option to "Add Reference" in the Project tab. There is also no "Assemblies" tab in the Reference Manager. Only "Projects," "Shared Projects," "COM," and "Browse."
How do I add System.Configuration as a reference so that I may use ConfigurationManager?
Edit: Although this answer may get rid of the error, this is not the "correct" answer as .NET 5 does not support ConfigurationManager. The other answers explain it best.
In Visual Studio 2019, click the Tools tab at the top.
Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution...
Search "System.Configuration.ConfigurationManager".
Install that to your project and the error should be gone.
System.Configuration.ConfigurationManager is not supported in .NET 5. See here for how you would migrate an app with a web.config file to the .NET Core pattern Migration Configuration
While it might be possible to use System.Configuration.Configuration manager in a .NET 5 application, it is a bad idea. The framework has integrated the new stuff with application startup and if you don't use it you are making yourself more work. The newer configuration patterns are easier to use and deploy into multiple environments. If you have an existing .NET classic app you want to port to .NET 5, follow that documentation from above. If this is a new application check out the Microsoft.Extensions.Configuration namespace.
.NEt 5 dosent Use Old ConfigurationManager you need to read from json
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment environment)
{
Configuration = new Configuration()
.AddJsonFile("config.json");
}
var options = ConfigurationBinder.Bind<AppSettings>(Configuration);
Console.WriteLine(options.SomeSetting);
For more info please refer
GitHub.com/aspnet/Options/test/Microsoft.Extensions.Options.Test/OptionsTest.cs

How to share assembly info in ASP.NET Core?

I'm developing a REST Api using ASP.NET Core. I used to shared assembly info across my solution using a shared file containing common attributes such as AssemblyCompany, AssemblyCopyright, AssemblyTrademark and AssemblyVersion. In this way all of my projects in a solution would be compiled with the same Assembly info attributes.
In ASP.NET Core these attributes are now defined in the project.json file. Is there a way to share these attributes in a similar way?
In case others get by here and are looking for a solution that works with .Net Core > 2.1 and the new .csproj project files:
After some research I got it working using Directory.Build.props file in a parent directory. You can find the sample code and some more information in this repository.
With the exception of AssemblyVersion, the assembly attributes you mentioned can still be defined in a shared file. This file can be added to the project under "buildOptions".
Example:
{
"buildOptions": {
"compile": "../shared/AssemblyInfo.cs"
}
}
See https://learn.microsoft.com/en-us/dotnet/articles/core/tools/project-json#buildoptions for more details.
AssemblyVersion can't be shared because it is always generated from the "version" value in project.json. "version" defaults to 1.0.0 if not specified.
Other attributes, such as AssemblyCopyright, will not be generated if their corresponding field is left empty in project.json.

Does the direct usage of an indirectly referenced assembly always work in ASP.NET 5?

I thought in ASP.NET 5 you'd have to rely on nuget packages, or the bin wrapper (which never worked for me btw.) if you want to reference an assembly that does not come from a project that is in the same solution as the web/dnx project.
Now I have an ASP.NET 5 RC1 project where I reference a 4.5.1 standard class library project from the same solution like this:
"frameworks": {
"dnx451": {
"dependencies": {
"MyProject.Utilities": "1.0.0-*"
}
}
}
(I don't use "dnxcore50": { })
Everything gets wrapped as expected, MyProjects.Utilities and its required assemblies. What I think is funny is that now in my web project I cannot only use MyProject.Utilities but also all the assemblies MyProject.Utilities references. It doesn't matter if I referenced the assembly via nuget from MyProject.Utilities or if I added an assembly via Add Reference -> Browse from MyProject.Utilities. I can use both types of indirectly referenced assemblies directly in my web project just so out of the box.
I think this is great, will this stay this way? Was this always meant to work this way?
No, this won't continue to work. In fact it stopped working with ASP.NET core RC 2. Dependencies now have to be specified explicityla as NuGet packages in the web project in order to use them from there.
Visual Studio 15 will probably allow to reference local dlls without the need for packages just like non core projets though.

How to Make ASP.Net 5 Reference Normal .Net Class Library that is not in your solution

Earlier I asked this question and got an answer that worked. In order to make your ASP.Net 5 application be able to reference a normal .Net class library that targets the .Net Framework 4.5, you must remove the ' "dnxcore50": { }' reference from your project.json file.
Great. That worked with a simple ClassLibrary project which did nothing.
Now I am trying to do it with more complicated class libraries. Class libraries which reference other NuGet Packages for instance (such as HtmlAgilityPack) and the same technique is not working.
This is very frustrating. I am a little dumbfounded that simply referencing a class library no longer works in the new version of ASP.Net.
One of the "features" that seems to be removed is the ability to reference a compiled DLL for a project that is not in your solution. The "Browse" button is gone from the Add Reference dialog in an ASP.Net 5 project:
Whereas a classic project still has the browse button:
Why? How do I reference a class library that is on my machine, yet I do not want to include in my project?
To reference a DLL on your machine but not in your Solution, you'll need to update the project.json as follows:
{
"frameworks" :
{
"dnx451" :
{
"bin" : { "assembly": "<path to dll>", "pdb": "<path to pdb if needed>" }
}
}
}
Unfortunately, much of the dnx tooling is not yet integrated into VS2015 (the teams are separate, and both are changing too quickly to keep up with each other particularly well), and the Asp.net documentation is still a work in progress.

Cannot create controller in VS 2012 MVC 4.0

When I create new project, create a new control have no problem. But when I'm using TFS in Visual Studios 2012 to get the latest code. All code is update, I cannot create new controller. This is my alert error from VS
could not load file or assembly'System.web.mvc, vesion = 3.0.0.0,culture = neutral, pulbickeytoken = 31bf3856ad364e35' or more of its this system cannot find the file specified.
Looks like you have a missing reference to System.Web.Mvc. Got to the "References" part of your project and add the missing reference.
Alternatively you could add the relevant System.Web.Mvc.Extensions Mvc 4 NuGet package which should add all references that your solution might be missing.
There is a discrepancy in your question between the error and your tag of MVC-4. So assuming you are aiming to use MVC 4.
In Visual Studio, go to the Package Manager Console via Tools -> Library Package Manager -> Package Manager Console. and run the following commands with the source : nuget.org as answered by Shiva in a Entity Framework question.
Edit:
Uninstall the previous version of MVC 3.0 and all references to it.
Uninstall-Package Microsoft.AspNet.Mv -Force
Upgrade to MVC 4.0 to match the tag you've added to the
question
Install-Package Microsoft.AspNet.Mvc -Version 4.0.30506
Check that Entity Framework is installed. Check the project file (*.csproj, *.vbproj) references by Right clicking the project file and opening it in a text editor such as notepad++.
Rebuild or Clean (optional) your project. When you have a full compile of the project such as getting the code from TFS, it will check all the references and throw this exception. When you compile the project again this error will most likely not appear as you are not doing a full compile.
Check the TFS version of the project file for any discrepancies.
I got the same problem, the cause was a "Security Update". After this update Project references to System.Web.Mvc.dll are no longer resolved because the assembly version of System.Web.Mvc.dll was incremented.
There are two ways to solve this issue, by installing a nuguet package or by manually updating the reference to System.Web.MVC.dll (don’t use the one in the GAC)
This blog post contains a detailed explanation:
http://blogs.msdn.com/b/webdev/archive/2014/10/16/microsoft-asp-net-mvc-security-update-broke-my-build.aspx