How to use .NET 4.6.1 with .NET Core - asp.net

I started a web project with .net core 2.1 and it works just fine.
But now a vendor says his server side component only works with .net 4.6.1
Can I now change the target framework on the project to 4.6.1 without rewriting the app?
The component helps export data to PDF and Excel, and uses many of the Standard Libraries. So I am at a loss on how to integrate this.
One idea is to create a separate API server that is on 4.6.1, for just the data export but the issue with that is security - the end user is logged into Server A, not Server B, so there are now security issues to deal with.
Maybe calling server B directly from Server A would be possible so I know the person is logged in, then returning the result back to the end user from Server A.
NOTE:Edit to 4.6.1 from 4.7.2 in the above in case that makes any difference.

Yes you can. Simply follow this here
Pulling code from the Stackoverflow link, you simply modify the .csproj file to suit your needs to the .NET Standard lib you're targeting.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
</PropertyGroup>
</Project>
By judging from the documentation provided by Microsoft and by a 3rd party service, you're advised to target .NET Standard for your .NET Core project in order to maximize compatibility with your vendor first. From there, you may most likely one to deploy a second server that completely targets .NET Core to roll out functions with modern APIs.
Take note that .NET Core 2.1 targets 4.6.1 onwards meaning that you will be writing a project that is unable to support anything older than that.

I want to note that the easiest way to run .netcore on the full .netframework is to start with such a project.
You can select .netcore/.net framework when you create the project. This will select all the correct libs for you to get started. Switching midstream can be done, but it takes fiddling with the libs to get the correct versions.

Related

Is it possible to change from .NET Framework 4.8 to 4.7.2

Is it possible to change from .NET Framework 4.8 to 4.7.2? In my "Turn Windows features on or off", I have ASP.NET 4.8 listed. I wish to change this to 4.7.2 I tried downloading and installing the developer pack from here (https://dotnet.microsoft.com/download/dotnet-framework/net472), but no luck on changing it inside "Windows features on or off".
I don't see any real particular issue by going back on the projects .net version.
The only requirement is that you have the correct version of .net installed on your computer. I suppose you "can" go into windows settings - and if the .net framework is not selected, then fine.
I would NOT suggest that you bother to disable say some given .net framework in the windows settings.
if you need a particular .net framework - then make sure it is installed on your development computer. once done, then in project settings you can select the framework - say like this:
So no, don't disable any .net framework. Sure, you can enable as such, but I would not mess or bother to remove existing frameworks.
About the ONLY real issues are:
If any library or referenced assemblies are LATER then the framework you select, then you can't use such tools, and external libraries.
And of course the target computer then has to have AT LEAST that level of .net installed.
In fact, in some cases, especially some web hosting providers - they are often VERY MUCH behind on what versions of .net are avaible.
And then of course often your have a 4-5 year old production server. They often don't allow updates to such servers to later versions of .net, so you are in your case - can you roll back, or choose a lower .net framework.
I certainly suggest that you adjust, pick, and get your development cycle working on the lowest min target framework. That way you not only avoid (even by accident) using some libraries and tools that can't run on that target web server.
Another FYI? If you select/change that framework in the VS project settings (as per above screen shot), I suggest that after doing so, you exit VS, and re-enter. I experienced some issues when not doing this exit of VS when downgrading the project.
You then want to do a clean project, and then do a re-build all. If that re-build all don't spit out any errors, then you are in rather good shape and should be ok.
Open the Project in Visual Studio >>> Go to Project Tab on Top >>> Select Project Properties >>> Change the Target Frameworkenter image description here from the Dropdown >>> Click Yes to Save the Same

Convert .Net Framework 4.6.2 project to .Net core project

I Have a solution which contains the bunch of class libraries which is developed by .Net framework 4.6.2. I have to convert those class libraries into .Net core. Is there any best and fastest way to convert instead for rewrite the code.
This appears to be an official Microsoft resource for doing the migration. Summarized below:
(recommended) Retarget all projects you wish to port to target the .NET Framework 4.7.2 or higher.
(recommended) Use the .NET Portability Analyzer to analyze your assemblies and see if they're portable to .NET Core.
(recommended) Install the .NET API analyzer into your projects to identify APIs throwing PlatformNotSupportedException on some platforms and some other potential compatibility issues.
Convert all of your packages.config dependencies to the PackageReference format with the conversion tool in Visual Studio.
Create new projects for .NET Core and copy over source files, or attempt to convert your existing project file with a tool.
Port your test code.
Most of BCL is still the same API-wise, so conversion is definitely viable for consideration. Yes, there may be incompatibilities in your code (or more often - with your dependencies) and the easiest way to check is to try building it with .net core.
For more details about when to convert (and when to rewrite) or about options of performing the conversion you could follow this guide: Upgrading to .NET Core and .NET Standard Made Easy.
The easiest way to switch a .net framework project to a .netcore project is to open the csproj file and change the TargetFramework from something like this
<TargetFramework>net462</TargetFramework>
to something like this
<TargetFramework>netcoreapp3.1</TargetFramework>
You could also change it to .net standard, in case you want compatibility between .net core and .net framework consumer projects, by changing it to this:
<TargetFramework>netstandard2.0</TargetFramework>
You could target multiple frameworks like so:
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks>
Ensure you use the correct version number and obviously depending on what this project already targets, things are going to break and will need fixing. For example, you can't use a .net framework class library with a .net core project.
A more detailed process is provided here:
https://learn.microsoft.com/en-us/dotnet/core/porting/
I just ran into the same error that you saw (as per your comment to #ImrePühvel) when I was trying to migrate a CLI project from .NET Framework to netcoreapp3.1:
"The expression "[Microsoft.Build.Utilities.ToolLocationHelper]::GetPathToStandardLibraries(_, netcoreapp3.1, '', x64, '', '')" cannot be evaluated. Input string was not in a correct format. C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets"
In my case, it was due to a misreading of the instructions.
The old framework had a tag:
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
This needs to be changed to:
<TargetFramework>netcoreapp3.1</TargetFramework>
NOT
<TargetFrameworkVersion>netcoreapp3.1</TargetFrameworkVersion>
I had simply changed v4.5 --> netcoreapp3.1 in the TargetFrameworkVersion tag without changing the tag name to TargetFramework.
So double-check that you changed:
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
to
<TargetFramework>netcoreapp3.1</TargetFramework>
(or whatever .NET Core version you want)
and NOT:
<TargetFrameworkVersion>netcoreapp3.1</TargetFrameworkVersion>

Project not compatible with netcoreapp2.0

I'm trying to add a full framework class library as a project reference to asp.net core 2.0 MVC project and getting the below error.
Project XYZ is not compatible with netcoreapp2.0 (.NETCoreApp,Version=v2.0).
Project XYZ supports: net462 (.NETFramework,Version=v4.6.2)
I have updated to the most recent version of Visual studio i.e, 15.3.5.
Is it even possible to reference 4.6.2 libraries in core 2.0 projects?
The first thing that you can try is to compile the library you want to consume as netstandard2.0.
Theoretically (according to the .net standard documentation), this will make it compatible with projects using net461 and later as well as netcoreapp2.0 and later.
In practice, sometimes you will end up with a problem with one of your dependencies that don't provide the same library version across different compilation targets.
In such cases you may simply need to add the .net core 2.0 as a target framework for the XYZ library.
The xml tag listing the targets is <TargetFrameworks> in the XYZ.csproj file and is not handled by the Gui of the project's properties.
So I would give a try at editing the XYZ.csproj by hand and add or replace what's listed as <TargetFrameworks> with netcoreapp2.0.
If you are adding it as additional target you need to separate them with ';' as in
<TargetFrameworks>net462;netstandard2.0;netcoreapp2.0</TargetFrameworks>
More details about this in this Microsoft doc.
Please keep in mind that this will trigger multiple compilations and will slow your build consequently...
It should be. Microsoft announced a ".NET Framework Compatibility Mode" with the release of .NET Standard 2.0. However, they didn't go into great detail about how it works exactly, or what to troubleshoot if it doesn't. Additionally, they only specific talk about it in relationship to Nuget packages, so it's possible there's some role Nuget is playing in the process, as well. Unfortunately, I've been unable to find any additional information about this feature outside of the announcement post.
That said, Microsoft's explicit recommendation is to not rely on the fact that your .NET Framework library may just happen to work in .NET Core; instead, you should be actively porting .NET Framework libraries you control to .NET Standard. I'd say you're likely going to spend more time trying to figure out why it doesn't "just work" than you would porting your code, so that it will definitely work, and be future-proof to boot.
The following solution worked for me.
Deleted bin and obj folders from all the projects in the solution, rebuild and if it still doesn't work try changing browser from debug options. for eg. If you already have chrome as default browser in Visual studio, switch to Edge or Firefox.

.net Framework to .net Core

Im doing a project with .net Framework , but to run on the linux i need .net Core..
I heard that it is possible to change .net Core to .net Framework only with changes on the project.csproj with this:
<TargetFramework>netcoreapp1.1</TargetFramework>
to
<TargetFramework>v4.5.2</TargetFramework>
and i tried to do the opposite , but i got some erros on the project...
There's a way to do that?
Thanks!
You can not switch a .NET Framework project to .NET Core that easy.
You might change an empty project but not one you already started coding.
Possibly you'll need to rewrite or start from scratch.
There are some major differences between two
Format of configuration files (web.config vs appsettings.json)
See this post on providing backward compatibility for App.config, appSettings.config and Web.config XML-based configuration providers
Used libraries
Startup files (Global.asax vs Startup.cs)
Lack of static objects in .Net Core. Like Session and Application objects -
which is a good thing btw.
Many of the .Net Framework libraries are dependant on
app.config/web.config files

How to Add Reference to System.Data.Services.Client in .Net 5 Project

I am trying to add search to an Asp.Net 5 project. The search uses the Bing Search API.
As per the instructions in the "Bing Search API – Quick Start and Code Samples" I have downloaded a file called "BingSearchContainer.cs". This file has references to System.Data.Services.Client. The file is too big to put here but can be downloaded at https://datamarket.azure.com/dataset/explore/getproxy/5ba839f1-12ce-4cce-bf57-a49d98d29a44.
I added references to System.Data.Services and System.Data.Services.Client as they were not included in the generic Asp.Net 5 (RC1) template I have used (in Visual Studio 2015) to create the site.
Although this removes the errors in the files themselves, the errors are still present in the error list and the project won't build or run.
If I hover over the using statement for System.Data.Services.Client at the top of the BingSearchContainer file it says ....DNX Core5.- Not Available.
Does anyone know how I can solve this?
You need to be aware of the platforms you're targeting. .NET Core is a new runtime, and there are no built-in libraries. Everything must be added (generally as a NuGet package), even things that were previously available from the Standard Libraries.
Check and see if the library you want is available on NuGet. If not, you'll need to find some sort of workaround or stop targeting .NET Core and just focus on the full .NET Framework.
Some workarounds
Locate a different package that does what you want and is available for both .NET Core and the full .NET Framework
Use System.Data.Services.Client on full .NET Framework and an alternative framework for .NET Core, and use compiler directives to target specific blocks of code at specific versions of the framework
Location the source for System.Data.Services.Client and try porting it to .NET Core. You should probably double check with Microsoft about this to see if they have plans to move it over already, as well as to see if there's anyone else that might help you with it
Just compile your project for .NET Framework, and don't compile for .NET Core

Resources