What is the difference between the these two FileStreamResult from different classes? - c#

What is the difference between System.Web.Mvc's FileStreamResult and Microsoft.AspNetCore.Mvc's FileStreamResult? Is there any significant take away of using the latter over the first one?
Edit:
Is it possible to have .Net Core project but still use System.Web.Mvc? I don't know how but just asking for possibility here, is it or is not?

The System.Web.Mvc namespace is used the .NET Framework. If your project is targetting the .NET Framework, use this.
The Microsoft.AspNetCore.Mvc namespace is used in .NET Core. If your project is targetting .NET Core, use this.
Only one of them will be available to you, depending on your project.
You can have an ASP.NET Core project that targets the full .NET Framework, and then add a reference to System.Web.Mvc.dll directly. That might work. But I don't know why you would want to.
But if your ASP.NET Core project is targetting .NET Core, then there is no way to use System.Web.Mvc.

System.Web.Mvc namespace is used by the .NET Framework MVC. Microsoft.AspNetCore.Mvc is used by .NET Core MVC. Pick appropriatly for the type of project you are building.

Related

ISystemClock not available in netstandard2.0

I'm still getting to grips with .Net Standard vs .Net Core after many years of development ASP.NET Framework. I have set up a new Web Api "app" which targets netcoreapp3.1 framework along with a middle-tier/Business Logic ClassLib and Data Access ClassLib, both of which target .Net Standard for maximum future compatibility. However, I can't seem to use ISystemClock from the Microsoft.AspNetCore.Authentication namespace in the .Net Standard classlibs!
From reading this SO question, I believe this might be because netstandard2.0 might not implement Microsoft.AspNetCore.App framework. Is this correct?
If it doesn't, should I:
Provide my own IMySystemClock interface in my class libraries which
the "app" itself can implement a trivial concrete class for?; or
Change my class middle and data access tiers to netcoreapp3.1
framework (seems over kill and restrictive to do this)?
Something else? Maybe I am missing the point of .Net Standard`?
A service (like ISystemClock) to provide the current (real or mock) time seems quite a fundamental service so I'm unsure why it's not appearing in .net standard framework?
Thanks
BloodBaz
If you want to access that functionality from a library, change the target framework of your library to .NET Core 3.1. Libraries can be built in .NET Standard or .NET Core. You can't use .NET Core functionality within a .NET Standard library, but you can do the reverse. Use .NET Standard functionality within a .NET Core library.
Also as a side note, unless it's required for compatibility reasons, I recommend you switch to .NET Standard 2.1. Visual Studio defaults new projects to .NET Standard 2.0, but a ton of new functionality was added in .NET Standard 2.1.
I spent years in the .NET Framework, so I feel the confusion. It took me a while to get used to it. I have a huge project I recently migrated over from .NET Framework to .NET Standard / .NET Core. What I ended up doing was dividing my code up into three sections. A .NET Standard 2.1 library with all the non .NET Core specific common code, a .NET Core 3.1 library with all my .NET Core specific common code (which referenced my .NET Standard library), and my applications as .NET Core 3.1 projects (which referenced my .NET Core 3.1 library).
Make sure you take a read at the answer in this question as well: What is the difference between .NET Core and .NET Standard Class Library project types?

Referencing a .Net Core 3.1 project from a .Net Standard Class Library

So I have a .NET Core 3.1 project. I am trying to create a unit testing project to go along with it. I have created a .NET Standard 2.1 Class Library for this purpose. I'm then trying to add a reference to the main project, in order to be able to reference code from there. This isn't working, as I get the following compilation errors:
'..\MyProj\MyProj.csproj' targets 'netcoreapp3.1'. It cannot be
referenced by a project that targets '.NETStandard,Version=v2.1'.
Project MyProj is not compatible with netstandard2.1
(.NETStandard,Version=v2.1). Project MyProj supports: netcoreapp3.1
(.NETCoreApp,Version=v3.1) Test (test\Test)
So this does make sense to me, as I understand why .NET Core wouldn't be able to reference a .NET Standard project. But then how can I create a Unit Testing project? Is it possible to create a .NET Core 3.1 Class Library? From everything I have read, it seems like that doesn't exist.
How can I set up a unit test project that references the main project?
.NET Standard was created as an interface for different .NET implementations. For example, this allows you to share code between .NET Core and the full .NET framework. I would create your library as a .NET Standard 2.0 project and then you can reference it in your .NET Core 3.1 application and your unit testing project.
I only suggest .NET Standard as it is generally more portable than .NET Framework or .NET Core libraries. You'd be able to target more platforms such as Xamarin, desktop, or web with .NET Standard.
I think you just need to use .Net Core 3.1 in your project, not NetStandard. I use it this way and there is no troubles. I guess you don't really need your unit testing to be portable?

What is the assembly and namespace of AllowXRequestsEveryXSecondsAttribute?

I have an ASP.NET MVC application created with Visual Studio 2017. I've been trying to use the AllowXRequestsEveryXSecondsAttribute for a controller action, but I can't find the assembly and namespace where it is defined. Apparently, the attribute's class is not defined in .NET Core yet, but I'm using .NET Framework.
Does anybody know the assembly and namespace where the attribute's class is defined?
The AllowXRequestsEveryXSecondsAttribute is not part of .NET Framework nor .NET Core. It is actually comes from 3rd party outside Microsoft, particularly to showcase security best practices in .NET Framework.
Microsoft has clarified this in their ASP.NET Core github:
https://github.com/aspnet/AspNetCore/issues/6836
The library itself is in the form of project template, and it is only for .NET Framework mainly for .NET Framework 4.6.1 or later. Therefore it is not advisable to use this for your .NET Core project.
FYI, this is the link of the github repo that contains the implementation, usage and sample is at:
https://github.com/johnstaveley/SecurityEssentials

Why referencing a .Net standard nuget package in a .Net 4.7.2 project import a lot of .Net core lib?

I'm having a .Net 4.7.2 application, in which I want to reference the package OpcFoundation.NetStandard.Opc.Ua. This project as a list of dependencies for .Net 4.6 that is quite small.
But when I install it, I get like 50+ additional packages to install. Is there a way to reduce this? I feel that a lot of thoses classes are already existing in the full .Net project(System.Threading.Tasks/Timer/...).
Thank you
If you look at many of these types (which are supplied by .NET Standard packages, not .NET Core), you'll find that the specific version that's used against .NET 4.7.2 will be an empty assembly just containing lots of TypeForwardedTo attributes pointing right back at the full-flavour .NET Framework.
So you still end up using the exact types you always would have done. There's just extra indirections which allows .NET Standard to work with both .NET Framework, .NET Core and other .NET Standard implementations.

Using .Net Core 2.2 classes in 2.1

I need a specific functionality that was recently added into .Net Core 2.2 (new enhancements to GzipStream class in System.IO.Compression), however I'm using AWS Lambda and the latest version supported there is 2.1
Is there a way to extract the required DLL from 2.2 package and use it with 2.1 solution? I found the DLL and referenced it in my project, but it didn't seem to work (seems like it's simply ignored). Am i missing a step?
it is not possible because a .net DLL has written into it the version of .NET. So as the version is later than your project, the compiler will ignore it.
Even if it were possible, there would be no guarantee that it would not refer to other .net core 2.2 methods, which would make you look for more DLLs in that version.
maybe you should be change the project version to 2.2

Categories