Sharing code between WPF & DNX Webapp - c#

I have a WPF application under the .Net framework 4.5 and a web app under the DNX Core.
I want to share some code between those two apps.
I tried to create a "portable classe library" ... with no result.
I tried to do it with a nuget package ... with no result (and too complex to dispatch modifications)
Thanks for your propositions

Normally when creating a portable class Library, you can be compatible with .NET 4.5 and Dnx project. The only problem is that .Net 4.5 relies on csproj when your Dnx project relies on project.json.
Thus you won't have it out of the box, check out your options here: What are my options for sharing code between DNX / ASP.NET 5 projects (project.json / xproj) and other C# projects (csproj) within a single solution?
Note that this solution will probably not work in the next version of dotnet core if they finally remove the project.json...

Related

WPF Control Library Target .NET Core 3 or .NET Framework?

I'm porting my WPF application from .NET Framework to .NET Core 3. I got this generic UI library that is published on NuGet, that is currently targeting .NET Framework v4.6.1. Now, if I just change the target to .NET Core 3, then it won't work anymore in .NET Framework projects. .NET Standard is designed to solve this kind of problem but I don't think it will help in the case of WPF projects.
What's the right thing to do here? Publish and manage 2 separate packages?
Then this library has 2 other derived libraries. If I create 2 separate packages for the base library, that means I'd have to publish and manage 2 of each derived libraries as well. This solution stinks. Got anything better?
A WPF application that targets .NET Core 3 may actually reference and consume a control library or NuGet package that targets .NET Framework 4.6.1.
You shouldn't have to do anything with the control library itself for this to work, assuming you don't use any APIs that is not present in .NET Core because then you will eventually get runtime errors.
Try to add a reference to the DLL or install the NuGet package as usual. It should work even if the NuGet package only contains a single assembly in lib\net461.

Why creating an NUnit project into my solution it use an old version of .NET framework (2.1)?

I am pretty new in C# and .NET and I have the following problem.
I created a NUnit (version 3.10.1) project in my solution. The thing that I can't understand is: why the framework version is the 2.1? Is not a very old version? If I try to change it I obtain older version, I am attaching a screenshot:
The strange thing is that the other project into my solution uses the .NET 4.5.2 framnework version.
Why this NUnit project is using an old framework version? there is a way to update it?
What is wrong or what am I missing?
You are targetting .NET Core, which is a completely different framework than the Full .NET Framework.
When you create a new project, you specify the framework to target. You created this one to target .NET Core.
To change your project to target Full framework 4.5.2:
Right click the csproj and select edit <yourprojectname>.csproj
Locate the <TargetFramework> element
Change it from netcoreapp2.1 to net452
Close the csproj file
For more info on .NET Core, you should have a look at the About .NET Core documentation.
.NET Core is an open-source, general-purpose development platform maintained by Microsoft and the .NET community on GitHub. It's cross-platform (supporting Windows, macOS, and Linux) and can be used to build device, cloud, and IoT applications.
And taken from the .NET Core on Wikipedia

How do I target .NET Standard 2.0 in a freshly created .NET Core 2.0 web app?

I've just created a fresh project using dotnet new web. My Google-foo may be failing me, but I didn't find anything relating to my answer (please link to another SO answer, or relevant documentation if I've missed something obvious).
If I want to ensure this new project is .NET Standard 2.0 compliant, what do I now do?
It is not inherently possible to run a netstandard project as an executable. Since netstandard was designed to be used for libraries.
In order to develop your web application entirely in netstandard2.0, you would have to create a separate project that targets either .NET Core or .NET Framework to execute your library that contains your web app (developed using .NET Standard).
1. Executable Project (ex: console app)
-- Target Framework: netcoreapp2.0 / net462
2. Web Application Project (library)
-- Target Framework: netstandard2.0
You can use the following steps to change the target framework of your project.
Step 1. Target the desired framework
Right-click on your project and select Edit *****.csproj
In the .csproj file, you need to replace the target framework to the .NET Framework.
Example .csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web"> //<-- note the .Web for the web template
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
For a list of the Target Framework Moniker (TFM) (ie, net47, netstandard2.0, netcoreapp2.0, etc.*) you can check this link out:
https://learn.microsoft.com/en-us/dotnet/standard/frameworks
Step 2. Run dotnet restore
Go to your output window and run dotnet restore.
Note: Sometimes Visual Studio may misbehave (depending on which update you have installed), so you may have to close and re-open your Visual Studio. Otherwise, sometimes a clean/re-build may do the trick.
Targeting both frameworks
You can pick one or the other, or even target both frameworks.
<TargetFrameworks>netcoreapp2.0; net47</TargetFrameworks> //<-- note the plural form!
NET Standard is for class libraries. Applications must target netcoreapp* where * is a version number. The following shows the compatibility matrix: https://learn.microsoft.com/en-us/dotnet/standard/net-standard
For example, .NET Core 2 can consume .NET Standard version 2 and below.

.Net Framework and .Net Core in same solution

I have an application targeting .NET Framework, and now I need to develop a library in .NET Core.
I plan to put both projects in the same solution and add the .NET Core library as a reference in the .NET Framework project.
Can this be done? If the answer is yes, how can this be done?
There is an article published in April 2017 on how to support both runtimes from the same solution:
https://learn.microsoft.com/en-us/dotnet/core/porting/project-structure
It boils down to 2 methods:
Having both frameworks configured in the same project (e.g. *.csproj):
Replace existing projects with a multi-targeted .NET Core project.
Having a separate project for each framework:
Keep existing projects and create a .NET Core project.
We are actively discussing which way to go in pythonnet, since the project supports .NET Framework on Windows and Mono on Linux/OSX. Now we are adding cross-platform pythonnet support:
https://github.com/pythonnet/pythonnet/pull/518

How do I reference a .NET Framework project in a .NET Core project?

I'd really like to start using .NET Core and slowly migrate applications and libraries to it. However, I can't realistically upgrade my entire code base to use .NET Core and then go through the process of testing and deploying a plethora of applications in production.
As an example, if I create a new .NET Core application and try to reference one of my .NET Framework projects I get the following:
The following projects are not supported as references: -
Foobar.NetFramework has target frameworks that are incompatible with
targets in current project Foobar.NetCore.
Foobar.NetCore: .NETCoreApp,Version=v1.0
Foobar.NetFramework: .NETFramework,Version=v4.5
Is it possible to create a new .NET Core application and reference my existing .NET Framework libraries? If so, what's the process for doing that? I've spent hours going through Microsoft's documentation and searching their issues on GitHub, but I can't find anything official on how to achieve this or what their long-term vision is for this process.
Old question, but with the release of .NetStandard 2.0 and .netcore 2.0 and vs2017.3, the game has changed.
You can use the Full .NET Framework (TFM) with .NetCore 2.0, but how?
In Visual Studio 2017.3, you can reference the Full .NET Framework (any version) directly from within a .NetCore2 project.
You can build the .NetStandard2 class library and reference your TFM. Then reference your .NetStandard2 library from your .NetCore2 project.
For example, referencing json.net net45 from .NetStandard2.
Browse to the folder and select version net45 (not netstandard1.3)
See the dependency in the image below, no yellow warning as you see.
Even if a Nuget library is not ready to be ported to .Netstandard 2, you can use any API in the library that is compliant to net461.
Quoting for the .NET Core 2/Standard 2.0 announcement with links:
.NET Core 2.0 is able to freely reference libraries that have been built for .NET Framework up to version 4.6.1
However, some libraries may fail at run time if they try to use API methods that aren't available on .NET Core
Reference: .NET Core App target .NET framework 4.5.2 on Linux
A need to use third-party .NET libraries or NuGet packages not available for .NET Core
So only in cases where the libraries or NuGet packages use technologies that aren't available in .NET Standard/.NET Core, you need to use the .NET Framework.
Reference: Choosing between .NET Core and .NET Framework for server apps
You can now reference .NET Framework libraries from .NET Standard libraries using Visual Studio 2017 15.3. This feature helps you migrate .NET Framework code to .NET Standard or .NET Core over time (start with binaries and then move to source). It is also useful in the case that the source code is no longer accessible or is lost for a .NET Framework library, enabling it to be still be used in new scenarios.
Reference: Announcing .NET Core 2.0
Yes, we are currently attempting the same thing. The trick is to make sure that you are supporting the same .NET frameworks. Inside your project.json file, make sure the framework matches the framework of the project you wish to include. For example:
"frameworks": {
"net46": { --This line here <<<<
"dependencies": {
"DomainModel": {
"target": "project"
},
"Models": {
"target": "project"
}
}
}
},
FYI: You might need to change the framework of your .NET Core or your older projects to achieve this. .NET Core can be changed just by editing the project.json file as seen above. You can so the same in .NET projects by right clicking the project and opening properties. Change the framework level there.
Once you have matched the two project frameworks then you should be able to include them. Good Luck!
We delayed migrations as long as could as it seemed daunting as first. But we got an insistent client who wanted to migrate ASAP.
So we migrated their Fintech Web App developed on .NET Framework 4.8 Web Forms to .NET 6 Razor Page. Our team scoured though hundreds of online resources & spoke to Microsoft Tech Support before we started the project. Hope the high-level walkthrough of our journey help you plan your migrations.
Our .NET Framework Website consisted of 1 .NET Web Forms project and 12 Class Libraries.
Here is how we did it.
Refactored the .NET Framework 4.8 Web Forms code
We ensured that the Web Forms code behind did not have a single line of service or business logic code. When we did find some business logic code in the web forms code behind, we refactored it, by moving it to the class libraries.
Created new .NET Standard projects
We created a new .Standard 2.0 Class library project for every .NET Framework 4.8 Class Library. If the original project was called "FintechProjectName.StockMarketClient", we named the .NET standard project "FintechProjectName.StockMarketClient.Standard".
Copied all files from .NET framework to .NET standard
We copied all the class files from .NET framework to .NET standard projects. We then removed all the .NET framework class libraries from the solution and added references to the new class libraries. All projects compiled on the 1st try itself and all our test cases too passed with minor changes.
Create new .NET 6 Web App Project
We created a new .NET 6 Web App Project. We had to entirely redo the front-end as there is no direct path for migrating Web Forms to Razor Pages. This was the only project which took us about 1 month to migrate.
Reference .NET standard class libraries in the new .NET 6 website
We copied all the .NET Standard libraries to this new solution containing the Razor Pages web site. Added the references and got it to work.
Move from .NET Standard to .NET 6 class libraries
Once the new website was up and running, with all test cases passed, we did the last step in the process which was the simplest. Created .NET 6 class library projects for each of the .NET standard libraries and named the projects appropriately. Copied all class files from .NET standard projects to their corresponding .NET 6 projects. Then we removed the .NET Standard libraries and added references to the new class libraries.
Overall project timelines were about a month and a half, most of it spend on Razor Pages implementation using the same html design.
Note:
If you are using any 3rd party library which does not have a .NET standard or .NET 5 version, then you are out of luck. You will need to find a replacement nuget package and recode your application to use this new library.
In my case with .net6 referencing framework 4.8 library ( both winforms), the trick seems to be to add the reference to the framework dll as a shared reference.

Categories

Resources