Published version WPF .NET Core - c#

I am looking to use code similar to the below, but in a .NET Core WPF C# application. This is what I previously used.
public static string GetVersion()
{
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
{
Version myVersion = ApplicationDeployment.CurrentDeployment.CurrentVersion;
return $"Version {myVersion}";
}
return "Version not deployed";
}
Is there an equivalent in .NET Core?

There is a closed issue around this issue and ClickOnce deployment.
ApplicationDeployment class is not available - that class is implemented in System.Deployment assembly that is not part of .NET Core 3.1 or .NET 5.
We will have a way to expose some of the properties that the class enables, i.e. URL parameters and update status. But, that work could not be completed for .NET 5 release. We will enable it in one of .NET 6 previews. Here are the tracking issues: #27 and #53
Use of ApplicationDeployment class in your deployed ClickOnce application is not possible, but main ClickOnce experience is available. It includes: creating ClickOnce deployment (manifests), publishing the application, creating and publishing updates. Your application will get automatic update check (and update, if user accepts it) - this part is handled by ClickOnce runtime and the new Launcher tool that becomes the entry point of ClickOnce deployment for .NET Core 3.1 and .NET 5 applications.
Consequently, as for now there does not seem to be a way to do this in .NET Core directly.

You should be able to get the assembly version using System.Reflection.Assembly.GetExecutingAssembly().GetName().Version, as shown in
Reading Assembly version information of WPF application
For more click-once capabilities not ported to .net core3.1/5/6, look here https://github.com/derskythe/WpfSettings

There has been some progress on this in .NET. According to a comment by a Microsoft employee you can now detect these values via environment variables as is done in mage.exe. For example something like this:
bool.TryParse(Environment.GetEnvironmentVariable("ClickOnce_IsNetworkDeployed"), out bool isNetworkDeployed);
Version.TryParse(Environment.GetEnvironmentVariable("ClickOnce_CurrentVersion"), out Version currentVersion);
Most of the discussion on that GitHub issue is about .NET 7. On the other hand, some comments suggest this code has its own independent release schedule. I don't know enough to say if/when it will be available in Core.

Related

Resolve .NET runtime version with same approach for .NET and .NET Framework

We're developing several .NET web applications, both on the old (.NET Framework 4.8) and new stack (.NET 6). On production, we want to determine on which .NET runtime each application is running.
I could add something like RuntimeInformation.FrameworkDescription to the startup code of each web application and log it.
I'd like to know whether there is a more elegant solution, e. g. via a tool. I played around with both dotnet-monitor and dotnet-counters, but I didn't find any API which returns the .NET runtime version of the .NET process.
Thx!

Visual Studio - Cannot start ASP.NET Core project

Last few months I create console applications with .NET framework and C#.
Now I want to create ASP.NET Core MVC projects, but I cannot choose any .NET Core template.
If I click "Console App (.NET Core)" or "ASP.NET Core Web Application" or any other .NET Core template, I can see this error message:
So, the problem occurs if I choose "Core", other templates work fine.
I have already tried to find a solution on Google.
What could be the problem? I reinstalled VS few times but did not help.
.NET Core does not support COM. COM is a Windows-only thing, and everything in Core is cross-platform. You must run on the full framework if you need to utilize a COM library.
That said, Microsoft has recently release a preview NuGet with some Windows compatibility APIs. This may allow you to utilize COM; I haven't tried it. However, the stated purpose of the NuGet is to make it easier to migrate existing .NET Framework applications to .NET Standard/.NET Core, so it's not considered something you would continue to utilize long-term. Rather, the idea is that you would actively work to migrate code that is not compatible to equivalent .NET Standard/.NET Core APIs, and then eventually remove the package entirely. As a result, it's probably not a good idea to go this route, even if it does enable you to use the COM library, simply because you're not likely to be able to stop at some point in the future.
Long and short, just run on the full framework.

Azure function with .net core 2 class library

Since there is still no support for Webjobs in ASP.Net Core, i am going to use Azure functions instead.
I have 2 class libraries that are build with on .net core 2. i can reference them to my Azure function project without any issue but when i try to publish i get this error.
This is the reference from the Azure function to my lib
Any idea on how i can make this work? without changing my class lib?
Azure Functions doesn't currently support .Net Core 2 (more specifically, .Net Standard 2), however, it seems like the team is actively working on getting this support out...see here: Port runtime to .NET Core
Runtime will be ported to target Netstandard 2.0.
Depending on the functionality of your libraries, you may see if its possible to target instead Netstandard 1.3 which they do currently support
This issue will track the .NET Core port activities for porting the runtime to .NET core. Note that netstandard 1.3 assemblies can be used on Azure Functions and this is fully supported.
Update: added clarity based on #derape comments
.NET Core 2.0 is now supported by Azure Functions.
https://blogs.msdn.microsoft.com/appserviceteam/2017/09/25/develop-azure-functions-on-any-platform/
Since there is still no support for Webjobs in ASP.Net Core
My understanding is that the VS tooling is not ready yet, so you cannot have a project template targeted at that, or some easy Publish as webjobs menu item.
But as far as I got reading through blog posts, you can create a .NET Core console application, add some .cmd file to project root with content like:
REM run.cms - Webjob entry point
#echo off
dotnet MyNetCoreConsoleApplication.dll
include that in the set of published files (with project.json "publishOptions": { "include": [...] } and finally make the webjob point to that cmd script. See for example here, or search for azure webjobs dotnet core.

What is a vNext console application?

I installed Visual Studio 2015 RC to try out the ASP.Net vNext templates. In the "Web" section, I have noticed a console application that appears as
I decided I would mess around with it and I found some interesting points:
The default template does not specify Main() as static.
Many assemblies such as System.CodeDom and System.Net are not available.
Many methods such as System.Console.ReadKey cannot be used.
What are these vNext console applications? Why the restrictions and what are the uses of them?
Answers
What is a vNext console application?
It's a console application that runs within the new .NET runtime environment (DNX).
Why the restrictions and what are the uses of them?
The restrictions occur because you are targeting .NET Core (dnxcore50) instead of (or in addition to) the full .NET Framework (dnx451.) The use of those restrictions, as far as I know, is to allow cross-compatibility with a lot of different operating systems. That is, .NET Core has less functionality than the full framework does, because it is easier to be compatible with many systems that way. Overtime, those restrictions may fall away as more in made fully cross platform.
The default template does not specify Main() as static.
DNX comes with Microsoft.Framework.ApplicationHost. This default application host "knows how to find a public void Main method. This is the entry point used to set up the ASP.NET hosting layer..." It also still knows how to find the traditional static void Main method. An advantage of an instance Main method is that it lets us ask the runtime environment to inject services into our application.
Many assemblies such as System.CodeDom and System.Net are not available. Many methods such as System.Console.ReadKey cannot be used.
System.Console.ReadKey is available in dnx451 but not in dnxcore50. That's also true for System.Net the last time I checked. So, if you want to use those, make sure to target dnx451 instead of dnxcore50.
Want to remove the restrictions? Just delete the dnxcore50 entry from your project.json file. Then you'll only be targeting the full framework without restrictions.
See Also
https://msdn.microsoft.com/en-us/magazine/dn913182.aspx
'Console' does not contain a definition for 'ReadKey' in asp.net 5 console App
Using System.Net.Mail in ASP NET MVC 6 project
Those console applications are leveraging the new .NET execution environment (DNX, previously KRE). It includes a new project system and allows you to target different versions of the CLR.
One of those versions is CoreCLR which is a slim version of .NET. This version is modular and its libraries are distributed as a bunch of NuGet packages. You may need to include some additional packages when targeting .NET Core (dependencies section on your project.json file).
Nevertheless, some of the limitations may arise from the fact that not all the APIs are already migrated to .NET Core or from the fact that they won't be, since the API surface is smaller on .NET Core.
I'm slowly going through the new runtime and will update this post with relevant references.
Microsoft.Framework.Runtime.Sources.EntryPointExecutor->TryGetEntryPoint()
Has the piece of code that is picking out the Program class and its Main function

Do .NET 2.0 API Calls Work on .NET 4.0 Runtime by Default, or Do They Require Extra Setup Steps?

If the C# frontend team on my project makes a standalone program that only does .NET 2.0 API calls, but install on to a computer with .NET 4.0, will it work by default or must they do something to the setup or to the setup-building process (like check off certain checkboxes) to make it work?
See, I'm assuming that if I make .NET 2.0 API calls only, that it should just "work" on .NET 4.0.
Part 2 on this is -- user has .NET 4.0 and .NET 1.1, but not .NET 2.0. I assume .NET 2.0 should work, then, because it has .NET 4.0. Right?
Background -- I'm a Linux/PHP dev and haven't touched C# since 2001. My skills are stale. The client's C# frontend team on my project need some assistance on understanding compatibilities, I guess, and so I've been asked by my client to ask this question.
.NET 2.0 should work if you have .NET 4.0 installed on your computer. I got this from this link: Version Compatibility in the .NET Framework. Keep in mind though, that some objects have changed from 2.0 and 4.0. They might not have all the same properties and methods. It is always a good thing to refer to the MSDN documentation.
Edit new link: Another good reference about the element in the web.config/app.config file of your project: supportedRuntime

Categories