Display app version in Blazor Webassembly - c#

I a trying to display the version on NavMenu bar of my blazor webassembly app.
Here is what i have tried:
#code {
protected string AppVersion => Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion;
}
When i run my application i get AppVersion as 1.0.0.
Is that correct?
Thank you for the comment.

Yes, this is correct. This is the default assembly version.
If you want to change the version number, you can add Version in the .csproj file:
<PropertyGroup>
<Version>2.0.0.0</Version>
</PropertyGroup>
When you perform a Build operation on the project, you can see that the AssemblyVersionAttribute, AssemblyInformationalVersionAttribute and AssemblyFileVersionAttribute in the ProjectName.AssemblyInfo.cs file(in \obj\Debug\net6.0) changes accordingly (the default is 1.0.0.0):
[assembly: System.Reflection.AssemblyFileVersionAttribute("2.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("2.0.0.0")]
[assembly: System.Reflection.AssemblyVersionAttribute("2.0.0.0")]

Related

GitVersion: Copyright attribute missing when publishing a single file with *GenerateAssemblyInfo* set to *false*

I am using GitVersion in my WPF-project. When publishing my app I use profile setting Produce single file which I want to stick to. Publish only runs successfully if I set im my .csproj-file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
</Project>
I also have set the Copyright information in my .csproj-file:
<Copyright>Copyright © ...</Copyright>
With GenerateAssemblyInfo set to true I can read the
Copyright attribute in my code by reflection:
var copyRightAttribute = (AssemblyCopyrightAttribute)Assembly.GetExecutingAssembly().GetCustomAttribute(typeof(AssemblyCopyrightAttribute));
Also in file-details it is visible:
With GenerateAssemblyInfo set to false publishing the Singe
file runs successfully but Copyright is missing in code and in file
details.
How can I have all at the same time?
Use GitVersion AND
publish a Single file AND
access Copyright attribute in code AND
see Copyright in file-details
When you disable automatic AssemblyInfo generation,
then you will need to include an AssemblyInfo.cs file yourself into your project,
holding the AssemblyCopyrightAttribute and others.
The ones in the .csproj file will not be considered anymore when it contains an <GenerateAssemblyInfo>false</GenerateAssemblyInfo>.
Note that this also includes the version numbers and more.
An AssemblyInfo.cs example:
using System.Reflection;
[assembly: AssemblyCopyright("Your Copyright Goes Here")]
[assembly: AssemblyTitle("Your Title")]
[assembly: AssemblyDescription("Your Description")]
[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]

error CS8773: "Feature 'global using directive' is not available in C# 9.0" after downgrade from net6.0 to net5.0

I have a project that was initially created for .NET 6 but then I needed to downgrade it to .NET 5.
I changed Target framework in Project Properties and tried to compile. As a result I received a bunch of the errors:
GlobalUsings.g.cs(2,1,2,29): error CS8773: Feature 'global using directive' is not available in C# 9.0. Please use language version 10.0 or greater.
File GlobalUsings.g.cs is created automatically and it reappears every time after compilation.
Finally I found that the reason is an extra property ImplicitUsings in the project file that is not supported by .net 5.0.
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
I needed to remove ImplicitUsings from the file.
remove <ImplicitUsings>enable</ImplicitUsings> in the csproj project file, then can build success
find this solution from here
Remove the tag indeed work.
But just change the value of it did the trick as well!
<ImplicitUsings>disable</ImplicitUsings>
To get rid of this error which is caused by downgrading below net6.0.
Remove the following items from the .csproj file:
<ImplicitUsings>
<Using Include="..." />
If you don't want to remove the ImplicitUsings or made changes to the project file. You can tell the build cli to disable it during build process by
dotnet build --configuration "Release" --framework "net5.0" /p:ImplicitUsings=false /p:PublishReadyToRun=false

C# Visual Studio 2019, Meta Numeric, Error: Could not load file or assembly

Visual Studio 2019
C#
Project 1:
Dependencies: Meta.Numerics 4.1.4. I added Meta Numerics via the Manage NuGet Packages for Solution
Project 1 is a Class Library
Will be compiled and .dll will be shared with an associate who will use it in the main application
Project 1 builds just fine using Debug.
using System;
using Meta.Numerics;
namespace LeakDetection
{
public class LeakDetectionOperations
{
public LeakDetectionOperations(int co = 24)
{ }
public int leakCheck()
{
double result = ComplexMath.Abs(10);
return 0;
}
}
}
Project 2
Dependencies: Project 1. Imported via Add references, browser, and selected the .dll from project 1
Project 2 is just a simple test project that I'm using to test the .dll object.
It runs, but throws an exception when it attempts to call the ABS function of Meta.Numerics.
using System;
using LeakDetection;
namespace LeakTest
{
class Program
{
static void Main(string[] args)
{
LeakDetectionOperations obj = new LeakDetectionOperations();
int ret;
ret = obj.leakCheck();
Console.WriteLine("Hello World!");
}
}
}
I've followed the instructions from the Meta.Numeric gitrepo regarding installation. The installation was done as they suggested to install the package. I've also cleaned build, and rebuilt fresh. I also changed from debug to release to see if there was anything related to the debug that was causing the error. As you can see at the above code, its fairly minimum, as this is not my actual code. Its a bit more elaborate, but rather than posting the full code, this is the minimum usable code that replicates the issue I'm having. Nothing from the Meta.numeric library is usable.
I usually work in Python, have some experience in C and C++, but I have used make files to compile in linux. Using C#, visual studio is fresh for me.
Any suggestions as to where I should look would be much appreciated.
UPDATE:
Per the suggestion by #kit, I've included the .csproj file for project 1 below
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Leak_Detection</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Meta.Numerics" Version="4.1.4" />
</ItemGroup>
</Project>
.csproj for project 2
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Reference Include="Leak Detection">
<HintPath>..\..\Leak Detection\Leak Detection\bin\Debug\netcoreapp3.1\Leak Detection.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
I was able to kind of replicate your issue (not exactly but I got the same kind of error). I believe your test project is in .NET framework. If I do not reference any nuget package in test project, failure occurs. But, if I install any nuget package and later on uninstall it, program runs successfully. For testing, I installed and uninstalled Newtonsoft.Json package. So, there is a bug in initialization of nuget package engine for .NET framework where in absence of any nuget packages, it is not able to resolve transitive nuget dependencies. If I create test project in .NET core, test project runs without any issue.
I have also put code in github
https://github.com/dheerajjain11/MissingDLLIssue/
MetaNumerics is .NET Standard library
MetaNumericsTest is .NET Framework Test project where I installed and uninstalled nuget package. Now, it runs successfully
MetaNumerics2 is .NET Framework Test project which fails
MetaCore is dotnet core project which runs without any issue and no workarounds
I don't know if this is normal or not, but what resolved the issue for me was installing the package via the Nuget package manager on both the Library and the Console application. Both builds need a reference.
What I was doing previously, was only installing the package for the library build. Seeing how the console application was then compiling with the .dll being referenced and called, the calling application also needed the package installed.

How can I capture the Package Version of a Dot Net Core assembly in startup code?

I know that all Dot Net Core projects get marked with a package version tag that appears in the CSProj file, like this:
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<StartupObject>MY.Program</StartupObject>
<TypeScriptToolsVersion>2.5</TypeScriptToolsVersion>
<AssemblyName>MY.PCA</AssemblyName>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Version>1.2.0</Version>
</PropertyGroup>
Is there a way to get access to that Version number field at Runtime? Namely in the Startup routines found in Startup.cs?
Add Microsoft.Extensions.PlatformAbstractions package as dependency and then use ApplicationEnvironment.ApplicationVersion property to get the version:
// using using Microsoft.Extensions.PlatformAbstractions;
ApplicationEnvironment app = PlatformServices.Default.Application;
string version = app.ApplicationVersion;
Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;

How to Manually Set Assembly Version

This one is giving me such a headache. We used to put things in the project properties under [assembly: AssemblyVersion("1.0.0.0")] I am totally fine with change so I do not care where it is. I understand they are going to a new standard for versions as well, also totally fine.
Plenty of documents out there point to the project.json file which is clearly a waste as this is no longer a legit file. More recent say add the following to your .csproj file:
<PropertyGroup>
<VersionPrefix>1.2.3</VersionPrefix>
<VersionSuffix>alpha</VersionSuffix>
</PropertyGroup>
Also a total waste. Only because I can not seem to be able to read it. The following always gives me 1.0.0.0.
PlatformServices.Default.Application.ApplicationVersion
Not to mention when I right-click in File Explorer and click Properties, then Details tab also always says 1.0.0.0.
So, how I can set the version of each assembly within my solution AND then read them later at runtime?
On method is to set either of these values in your project file:
<PropertyGroup>
<Version>1.3.5.7</Version>
<FileVersion>2.4.6.8</FileVersion>
</PropertyGroup>
And read them like this:
var fileVersion = Assembly.GetEntryAssembly()
.GetCustomAttribute<AssemblyFileVersionAttribute>()
.Version;
var informationalVersion = Assembly.GetEntryAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
It's worth noting that setting these value in the .csproj file does autogenerate (so don't try to edit it) a file similar to the legacy AssemblyInfo.cs containing something like this:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
//snip
[assembly: System.Reflection.AssemblyFileVersionAttribute("2.4.6.8")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.2.3")]
// Generated by the MSBuild WriteCodeFragment class.
Turns out this started working when .Net Core 2.0 came out. When you right-click on the Project and then click Properties there is a UI for it as seen below:
The Package Version, Assembly Version and Assembly File Version correspond to the Version, AssemblyVersion and FileVersion settings in the .csproj file respectively:
<PropertyGroup>
<Version>1.1.0</Version>
<AssemblyVersion>1.1.0.9</AssemblyVersion>
<FileVersion>1.1.0.9</FileVersion>
</PropertyGroup>
I then created a utility method in each project of my solution that does the following:
public static VersionInformationModel GetVersionInformation() {
var Result = new VersionInformationModel {
Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(),
BuildDate = System.IO.File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location),
Configuration = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyConfigurationAttribute>().Configuration,
TargetFramework = Assembly.GetExecutingAssembly().GetCustomAttribute<System.Runtime.Versioning.TargetFrameworkAttribute>().FrameworkName,
};
return Result;
}
So on an admin page of my site, I can know when version and other particulars about each project as it stands on whatever server I am looking at.

Categories