I currently hard code a version number using a label but I want to be able to displayed the version number by auto incrementing it instead of a hard code text. How do I do that?
Based off of that example, how do I get the text label that displays the version number to automatically increment. The label is on a site master page.
Based off of that example, how do I get the text label that displays the version number to automatically increment. The label is on a site master page.
I might have wrongly assumed the project properties are set to auto increment on each build and all you want to do is read the current build version.
protected void Page_Load(object sender, EventArgs e)
{
Version version = Assembly.GetExecutingAssembly().GetName().Version;
lblBuildNumber.Text = version.ToString();
}
If you have a custom versioning scheme then, it's up to you to increment the version according to your business requirements, store the version, and fetch the version. If you are using the built in VS versioning then the above code will work.
Based off of that example, how do I get the text label that displays the version number to automatically increment. The label is on a site master page.
I might have wrongly assumed the project properties are set to auto increment on each build and all you want to do is read the current build version.
protected void Page_Load(object sender, EventArgs e)
{
Version version = Assembly.GetExecutingAssembly().GetName().Version;
lblBuildNumber.Text = version.ToString();
}
If you have a custom versioning scheme then, it's up to you to increment the version according to your business requirements, store the version, and fetch the version. If you are using the built in VS versioning then the above code will work.
I want to be able to auto version everytime I deploy to my production web server. Will the above code work for that?
I want to be able to auto version everytime I deploy to my production web server. Will the above code work for that?
The code shown displays the assembly version found in project properties. Right click the project and select properties. In the Application tab select the "Assembly Information" button. Here you can set the Assembly version. Insert an asterisk to auto-increment.
Based off of that example, how do I get the text label that displays the version number to automatically increment. The label is on a site master page.
I might have wrongly assumed the project properties are set to auto increment on each build and all you want to do is read the current build version.
protected void Page_Load(object sender, EventArgs e)
{
Version version = Assembly.GetExecutingAssembly().GetName().Version;
lblBuildNumber.Text = version.ToString();
}
If you have a custom versioning scheme then, it's up to you to increment the version according to your business requirements, store the version, and fetch the version. If you are using the built in VS versioning then the above code will work.
So I've added the label and set the label equal to the Assembly in the Page Load and setting the asterisk in the Assembly.cs. By doing it like this will this automatically show a new version every time the website is published or do I need to to do one (label)
or the other (set in Assembly.cs?
Also the current build number is 1.3.024 so how do I increment from there.
So I've added the label and set the label equal to the Assembly in the Page Load and setting the asterisk in the Assembly.cs. By doing it like this will this automatically show a new version every time the website is published or do I need to to do one (label)
or the other (set in Assembly.cs?
Again, the posted code takes advantage of the standard versioning scheme built into Visual Studio.
.NET also has a version type. I would use the built in feature rather than building a custom versioning system. Try going through the posted links to learn how versioning works in .NET
If you must use a custom build version system then it is up to you to craft a design. One idea is to use an XML or JSON file to persist the current version. Craft a command line utility to increment the version by deserializing the file contents in to
an object, increment the version according your your business logic, serialize the object, and save the file. A database would also work.
If you use the Version type in .Net then craft the Version.json file like so.
This console app reads the file, increments the build, then saves the file.
static void Main(string[] args)
{
Version version;
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText("Version.json"))
{
JsonSerializer serializer = new JsonSerializer();
version = (Version)serializer.Deserialize(file, typeof(Version));
}
Console.WriteLine("Current Version:\t{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3,'0'));
//Create a new Version and increment
Version newVersion = new Version(version.Major, version.Minor, version.Build + 1, version.Revision);
Console.WriteLine("Incremented Version:\t{0}.{1}.{2}", newVersion.Major, newVersion.Minor, newVersion.Build.ToString().PadLeft(3, '0'));
// serialize JSON directly to a file
using (StreamWriter file = File.CreateText("Version.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, newVersion);
}
}
The output
Current Version: 1.3.028
Incremented Version: 1.3.029
Press any key to continue . . .
Then in your web application simply read the file or database table and fetch the version when the application loads. Use the sample code above or update it to suit your needs.
bootzilla
Also the current build number is 1.3.024 so how do I increment from there.
I'm not sure how your build increment logic works but the code above should be enough for you to implement this logic on your own.
I used NewtonSoft to serialize the JSON. NewtonSoft is available from NuGet.
1.1.1.* should use the number of seconds since midnight.
1.1.. would use the would increment the 3rd component on each day (I assume this is the nuumber of days since a base date ?)
If I remember you had a difference between the C# compiler and the VB compiler in how those numbers are handled. Not sure if still true.
No this is all in C#, no VB.
As for the versioning number of seconds since midnight, I'm not sure what you mean there. The versioning number should increment during each build or publish.
So I've added the label and set the label equal to the Assembly in the Page Load and setting the asterisk in the Assembly.cs. By doing it like this will this automatically show a new version every time the website is published or do I need to to do one (label)
or the other (set in Assembly.cs?
Again, the posted code takes advantage of the standard versioning scheme built into Visual Studio.
.NET also has a version type. I would use the built in feature rather than building a custom versioning system. Try going through the posted links to learn how versioning works in .NET
If you must use a custom build version system then it is up to you to craft a design. One idea is to use an XML or JSON file to persist the current version. Craft a command line utility to increment the version by deserializing the file contents in to
an object, increment the version according your your business logic, serialize the object, and save the file. A database would also work.
If you use the Version type in .Net then craft the Version.json file like so.
This console app reads the file, increments the build, then saves the file.
staticvoidMain(string[] args){Version version;// deserialize JSON directly from a fileusing(StreamReader file =File.OpenText("Version.json")){JsonSerializer serializer =newJsonSerializer();
version =(Version)serializer.Deserialize(file,typeof(Version));}Console.WriteLine("Current Version:\t{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3,'0'));//Create a new Version and incrementVersion newVersion =newVersion(version.Major, version.Minor, version.Build+1, version.Revision);Console.WriteLine("Incremented Version:\t{0}.{1}.{2}", newVersion.Major, newVersion.Minor, newVersion.Build.ToString().PadLeft(3,'0'));// serialize JSON directly to a fileusing(StreamWriter file =File.CreateText("Version.json")){JsonSerializer serializer =newJsonSerializer();
serializer.Serialize(file, newVersion);}}
The output
CurrentVersion:1.3.028IncrementedVersion:1.3.029Press any key to continue...
Then in your web application simply read the file or database table and fetch the version when the application loads. Use the sample code above or update it to suit your needs.
bootzilla
Also the current build number is 1.3.024 so how do I increment from there.
I'm not sure how your build increment logic works but the code above should be enough for you to implement this logic on your own.
I used NewtonSoft to serialize the JSON. NewtonSoft is available from NuGet.
I appreciate this but this may not be exactly what I'm looking for. The number should be displayed on a login page, so according to this type of coding where am I going to show the version number?
I appreciate this but this may not be exactly what I'm looking for. The number should be displayed on a login page, so according to this type of coding where am I going to show the version number?
All the code does is fetch and write to a file. Can you explain why, given the source code, you are unable to read the file in a web app and write the value to a server control's Text property? Have you tried?
I appreciate this but this may not be exactly what I'm looking for. The number should be displayed on a login page, so according to this type of coding where am I going to show the version number?
All the code does is fetch and write to a file. Can you explain why, given the source code, you are unable to read the file in a web app and write the value to a server control's Text property? Have you tried?
The current version number is hardcoded on a SiteMaster page, which is essentially a login page, as I show in my original post in this thread. That version number is 1.3.024 currently. I can even start at 1.4.000 or whatever at this point. Going forward
I don't want it to be hardcoded and to automatically increment every time it is published to a production server. It needs to be displayed on the login page of the website. That's the reason why.
The current version number is hardcoded on a SiteMaster page, which is essentially a login page, as I show in my original post in this thread. That version number is 1.3.024 currently. I can even start at 1.4.000 or whatever at this point. Going forward I don't
want it to be hardcoded and to automatically increment every time it is published to a production server. It needs to be displayed on the login page of the website. That's the reason why.
I showed you two different ways with source code and documentation to solve this problem. One using standard Visual Studio and a custom version where the version is persisted in a file. I'm not sure what else I can do for you.
If you go with the custom solution above and place the version file in the application root, then this code will write the version to the page.
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Version version;
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(Server.MapPath("Version.json")))
{
JsonSerializer serializer = new JsonSerializer();
version = (Version)serializer.Deserialize(file, typeof(Version));
}
Application["Version"] = string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3, '0'));
}
}
All you have to do is run the console app to update the version file. Remember to set the path. You can kick off the console app by entering the path to the console app in the project properties -> Build Events. The console app will run each time there
is a build which will update the Version.json file.
The current version number is hardcoded on a SiteMaster page, which is essentially a login page, as I show in my original post in this thread. That version number is 1.3.024 currently. I can even start at 1.4.000 or whatever at this point. Going forward I don't
want it to be hardcoded and to automatically increment every time it is published to a production server. It needs to be displayed on the login page of the website. That's the reason why.
I showed you two different ways with source code and documentation to solve this problem. One using standard Visual Studio and a custom version where the version is persisted in a file. I'm not sure what else I can do for you.
If you go with the custom solution above and place the version file in the application root, then this code will write the version to the page.
publicclassGlobal:HttpApplication{voidApplication_Start(object sender,EventArgs e){// Code that runs on application startupRouteConfig.RegisterRoutes(RouteTable.Routes);BundleConfig.RegisterBundles(BundleTable.Bundles);Version version;// deserialize JSON directly from a fileusing(StreamReader file =File.OpenText(Server.MapPath("Version.json"))){JsonSerializer serializer =newJsonSerializer();
version =(Version)serializer.Deserialize(file,typeof(Version));}Application["Version"]=string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3,'0'));}}
All you have to do is run the console app to update the version file. Remember to set the path. You can kick off the console app by entering the path to the console app in the project properties -> Build Events. The console app will run each time there
is a build which will update the Version.json file.
Or you can use the standard Visual Studio versioning as recommend above.
Got a question about the Global.asax file. I get 'The name 'RouteTable' does not exist in the current context when I add that code to the file. I also get the same for RouteConfig , BundleConfig, BundleTable, JsonSerializer. What do I need to do to fix those
errors?
This is what my code looks like currently when I add the code you provided to it:
public class Global : System.Web.HttpApplication
{
public static IConverter PDFConverter;
protected void Application_Start(object sender, EventArgs e)
{
PDFConverter =
new ThreadSafeConverter(
new RemotingToolset<PdfToolset>(
new WinAnyCPUEmbeddedDeployment(
new TempFolderDeployment())));
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Version version;
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(Server.MapPath("Version.json")))
{
JsonSerializer serializer = new JsonSerializer();
version = (Version)serializer.Deserialize(file, typeof(Version));
}
Application["Version"] = string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3, '0'));
}
Do I just generate properties for all of those that have that error?
I'm not sure what you are doing.... Post your original Global.asax file and I'll the paste the code into the file to reduce the errors. Or just paste the highlighted code at the end of the Application_Start handler.
All I did was paste the code you provided into the Application_Start handler. This was the code in the handler before I pasted.
protected void Application_Start(object sender, EventArgs e)
{
PDFConverter =
new ThreadSafeConverter(
new RemotingToolset<PdfToolset>(
new WinAnyCPUEmbeddedDeployment(
new TempFolderDeployment())));
}
This is after(highlighted):
protected void Application_Start(object sender, EventArgs e)
{
PDFConverter =
new ThreadSafeConverter(
new RemotingToolset<PdfToolset>(
new WinAnyCPUEmbeddedDeployment(
new TempFolderDeployment())));
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Version version;
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(Server.MapPath("Version.json")))
{
JsonSerializer serializer = new JsonSerializer();
version = (Version)serializer.Deserialize(file, typeof(Version));
}
Application["Version"] = string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3, '0'));
}
I tried my previous suggestion which seems to work out of the box ie using
[assembly:
AssemblyVersion("1.0.*")] allows the CS compiler
to assign a build/revision to each new build (though this is not an increment, it uses what I told earlier to generate unique numbers each time you create a new DLL without having to care about the previous version number).
I tried my previous suggestion which seems to work out of the box ie using
[assembly:
AssemblyVersion("1.0.*")] allows the CS compiler
to assign a build/revision to each new build (though this is not an increment, it uses what I told earlier to generate unique numbers each time you create a new DLL without having to care about the previous version number).
using
System.Web.Routing;
This fixes RouteTable but I still get errors for RouteConfig, BundleConfig, BundleTable, and JsonSerializer. All the same error 'The name 'RouteConfig' does not exist in current context.
And yes I agree that using [assembly:
AssemblyVersion("1.0.*")]
will generate some random build number like 1.4.6865.30445 which is not what I want.
More generally you should have a "light bulb" on this kind of errors and you should then see suggestions such as using the full name ie someNameSpace.RouteConfig or to add a using NameSpace; directive at the top of your file. See https://dirkstrauss.com/visual-studio-2015-light-bulb/
It could be also that a namespace declaration is missing (AFAIK it is common to declare MvcApplication and RouteConfig in the same namespace in which case you won't have this issue).
More generally you should have a "light bulb" on this kind of errors and you should then see suggestions such as using the full name ie someNameSpace.RouteConfig or to add a using NameSpace; directive at the top of your file. See https://dirkstrauss.com/visual-studio-2015-light-bulb/
It could be also that a namespace declaration is missing (AFAIK it is common to declare MvcApplication and RouteConfig in the same namespace in which case you won't have this issue).
For the JsonSerializer error I get 'the type or namespace 'JsonSerializer' could not be found' and the light bulb suggestion is to generate a class for that in a new file. That doesn't sound right.
OK now I need to get the label or whatever on the master page to show the version number on the login page. Here is the code I have but it doesn't seem to work.
Using that span I don't get anything to show. I'm not using it in the footer. This is what I have the label for the word 'Build' and then the span tag:
Now that everything seems to be set up. When I build on that project the next version should show as 1.4.002, correct?
I will work if you followed the instructions outlined above.
Invoke the console app in the build event of the web project.
The console app is writing the file to the application root.
Not sure exactly how to do this. I'm not good with command lines at all.
So does this go in the pre-build event? If so do I call the path to the version.json file? and put C:/ProjectFolder/version.js in the event command line event?
I would put the path to the console app in the post_built event. Keep in mind, this is not a polished feature but it should be more than enough to get you started.
The concept is simple though. The post-build event kicks off the console app. The console app updates the version.json file in the web app.
The parts that are missing are placing the console app in a known location and using configuration to write the file location if this is a team feature. Also the solution increments the last value in the Version only. You'll need to modify the console
app to increment the other Version parts according to your business rules.
bootzilla
If so do I call the path to the version.json file? and put C:/ProjectFolder/version.js in the event command line event?
No, the console app writes the file. The post-build event uses the path to the Console app exe. I placed the exe in the web project's bin folder.
I would put the path to the console app in the post_built event. Keep in mind, this is not a polished feature but it should be more than enough to get you started.
The concept is simple though. The post-build event kicks off the console app. The console app updates the version.json file in the web app.
The parts that are missing are placing the console app in a known location and using configuration to write the file location if this is a team feature. Also the solution increments the last value in the Version only. You'll need to modify the console
app to increment the other Version parts according to your business rules.
bootzilla
If so do I call the path to the version.json file? and put C:/ProjectFolder/version.js in the event command line event?
No, the console app writes the file. The post-build event uses the path to the Console app exe. I placed the exe in the web project's bin folder.
OK I'm confused here. What goes in the post event? Is it C:/ProjectFolder/something.exe? What is this console app exe. Is that something that gets created during the build? I'm just trying to figure this out. You have to keep in mind I haven't done this
before so just giving me bits and pieces isn't going to help. You will need to be more specific.
I came up with this idea because you do not want to use the built-in VS feature and the versioning scheme is custom.
bootzilla
I haven't done this before so just giving me bits and pieces isn't going to help. You will need to be more specific.
I'm not sure what I can do for you if you feel that way. From my perspective, I've been more than specific but you ignore the advice, don't try the source code, or even read the posted links. Case in point, the use of NewtonSoft to serialize JSON including
the NuGet link.
It seems you want someone to craft an entire solution according to your requirements. You have to put in some work as well.
I came up with this idea because you do not want to use the built-in VS feature and the versioning scheme is custom.
bootzilla
I haven't done this before so just giving me bits and pieces isn't going to help. You will need to be more specific.
I'm not sure what I can do for you if you feel that way. From my perspective, I've been more than specific but you ignore the advice, don't try the source code, or even read the posted links. Case in point, the use of NewtonSoft to serialize JSON including
the NuGet link.
It seems you want someone to craft an entire solution according to your requirements. You have to put in some work as well.
Man you are incredibly difficult at times. I HAVENT DONE THIS BEFORE. What part of that do you not understand. You are going to have to break this down for me please. You didn't explain what exactly the console app is. Is it global.asax? Is it something
else. What goes in the build event specifically. I don't know that's why I'm asking. Telling me you are not sure what you can do for me is bs. YES YOU CAN. You are choosing not to for some reason. If you take the time to break down each part of what I need
to do in the build events that would be a very helpful start.
And no I don't want someone to craft the entire solution. That is complete bs as well. Where do you get these crazy ideas. This not even close to an entire solution. I need assistance on a very small piece that I have not done before. That's why I came
here to ask for help. I don't know anything about build events and I'm asking for help in that direction. You seem to not understand this. I'm asking for it very cordially. You are not and have been very difficult throughout this process.
So I'm going to ask again what do I put in the build event SPECIFICALLY. I don't know what to put there since I haven't done this before.
Can anyone assist me in finishing this please. I don't know much about post event builds and I need to know what to put in there and the person that pointed me in this direction doesn't seem to want to help anymore.
I would really appreciate someone assisting me with this.
The pre build event allows to launch an app each time you build the application. What runs would be a console app that you have to write and that would increment the build version (the sample above just use the AssemblyInfo.cs file).
It could still skip numbers as you may build several times before publishing the next version on the web site (which is likely why the default VS approach favors uniqueness and de-emphasize using consecutive numbers)
Or for now just include manually incrementing the build number as part of a post deployment procedure ?
The pre build event allows to launch an app each time you build the application. What runs would be a console app that you have to write and that would increment the build version (the sample above just use the AssemblyInfo.cs file).
It could still skip numbers as you may build several times before publishing the next version on the web site (which is likely why the default VS approach favors uniqueness and de-emphasize using consecutive numbers)
Or for now just include manually incrementing the build number as part of a post deployment procedure ?
First I appreciate the feedback. I also want to state that this is not my idea for incrementing. The person that wants it done this way is a client that I'm working for. If it were up to me I would just use the default method of 1.2.* in the assembly version
and go from there. This has to be a custom build so starting with build version 1.4.001 and then each build increments that last number so the next would be 1.4.002 and so on.
Based off of what mgehard said the console app has to be invoked in the post build event. I don't know much about it at all. I don't know specifically what code/syntax goes there. Does this make sense?
Can anyone assist me in finishing this please. I don't know much about post event builds and I need to know what to put in there and the person that pointed me in this direction doesn't seem to want to help anymore.
I would really appreciate someone assisting me with this.
Huh? Well, the threatening PMs of harm don't help and, like many of us, have other stuff to do.
Create you're own console application in Visual Studio and copy the main() method to your new console app. Be sure to add the NewtonSoft NuGet package.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleCs
{
class Program
{
static void Main(string[] args)
{
Version version;
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"./Version.json"))
{
JsonSerializer serializer = new JsonSerializer();
version = (Version)serializer.Deserialize(file, typeof(Version));
}
Console.WriteLine("Current Version:\t{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3, '0'));
//Create a new Version and increment
Version newVersion = new Version(version.Major, version.Minor, version.Build + 1, version.Revision);
Console.WriteLine("Incremented Version:\t{0}.{1}.{2}", newVersion.Major, newVersion.Minor, newVersion.Build.ToString().PadLeft(3, '0'));
// serialize JSON directly to a file
using (StreamWriter file = File.CreateText(@"./Version.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, newVersion);
}
}
}
}
Build the app - do not run the app. Copy the "Version.json" file to the console app's bin/debug folder. You can find the bin folder by right clicking the project in solution explorer and selecting "Open folder in file explorer". Then just open the bin/debug
folder. Run the Console app and it should update the Version.json file in the bin folder.
Update the following two lines of code by replacing "Version.json" with the fully qualified path to the Version.json file in your web application.
using (StreamReader file = File.OpenText(@"./Version.json"))
using (StreamWriter file = File.CreateText(@"./Version.json"))
It will look something like this... Note the "@".
using (StreamReader file = File.OpenText(@"C:\Path\to\the\Web\App\Version.json"))
Run the app again and it should update the Version.json file located in the web app.
Copy the console app (exe) to the web app's bin folder. Open the console app's bin/debug folder. Find the exe file that has the same name as your console app and copy the exe to your web app's bin folder. Also copy the *.exe.config file.
Open the web app's properties and go to the Build Events. Enter the exe file name; ie. MyConsoleApp.exe.
Now every time the project is build, the console app will run and update the Version.json File.
As PatriceSc suggested, this will update the Version every time the project is built. Not when deployed. But, you can trigger the console app whenever you like.
Lastly, this is not a polished production feature as it only increments the last number 1.0.XXX. I'm pretty sure you still need to implement your Versioning business logic. You have all the source code so modify it to suit your needs.
It's basically just a command line and you can use $(variable) values that are replaced before it runs so that you can transmit to this app parameters that depends on the current solution (such as the root folder for your solution etc...). It's likely rather
on the pre build event.
If I had to do that I wonder if I wouldn't consider this to be rather a "deployment counter". For example I would have perhaps a table with the actual version and/or DLL modification date (maybe not taking the time into account) to register in my db a new
"deployment" each time a new DLL is first used.
Member
193 Points
1128 Posts
How to have an auto incrementing version number (Visual Studio)?
Sep 27, 2018 07:25 PM|bootzilla|LINK
I currently hard code a version number using a label but I want to be able to displayed the version number by auto incrementing it instead of a hard code text. How do I do that?
Here is the current code for the label
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Sep 27, 2018 07:38 PM|mgebhard|LINK
Use reflection to get the build version.
https://stackoverflow.com/questions/909555/how-can-i-get-the-assembly-file-version
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Sep 27, 2018 08:15 PM|bootzilla|LINK
Based off of that example, how do I get the text label that displays the version number to automatically increment. The label is on a site master page.
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Sep 27, 2018 08:44 PM|mgebhard|LINK
I might have wrongly assumed the project properties are set to auto increment on each build and all you want to do is read the current build version.
If you have a custom versioning scheme then, it's up to you to increment the version according to your business requirements, store the version, and fetch the version. If you are using the built in VS versioning then the above code will work.
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 03, 2018 03:55 PM|bootzilla|LINK
I want to be able to auto version everytime I deploy to my production web server. Will the above code work for that?
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 03, 2018 05:20 PM|mgebhard|LINK
The code shown displays the assembly version found in project properties. Right click the project and select properties. In the Application tab select the "Assembly Information" button. Here you can set the Assembly version. Insert an asterisk to auto-increment.
https://dailydotnettips.com/specifying-assembly-information-in-visual-studio/
https://blog.jsinh.in/how-to-auto-increment-version-number-of-assembly-in-dotnet-csharp/#.W7T5H2hKhaQ
There are VS plugins to make this a bit easier but you'll need to read the docs to make sure the plugin is a good fit.
https://marketplace.visualstudio.com/items?itemName=PrecisionInfinity.AutomaticVersions
https://marketplace.visualstudio.com/items?itemName=PaulMelia.BuildVersionIncrementAdd-inVS2015VS15Preview
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 10, 2018 04:09 AM|bootzilla|LINK
So I've added the label and set the label equal to the Assembly in the Page Load and setting the asterisk in the Assembly.cs. By doing it like this will this automatically show a new version every time the website is published or do I need to to do one (label) or the other (set in Assembly.cs?
Also the current build number is 1.3.024 so how do I increment from there.
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 10, 2018 04:22 PM|mgebhard|LINK
Again, the posted code takes advantage of the standard versioning scheme built into Visual Studio. .NET also has a version type. I would use the built in feature rather than building a custom versioning system. Try going through the posted links to learn how versioning works in .NET
If you must use a custom build version system then it is up to you to craft a design. One idea is to use an XML or JSON file to persist the current version. Craft a command line utility to increment the version by deserializing the file contents in to an object, increment the version according your your business logic, serialize the object, and save the file. A database would also work.
If you use the Version type in .Net then craft the Version.json file like so.
This console app reads the file, increments the build, then saves the file.
The output
Then in your web application simply read the file or database table and fetch the version when the application loads. Use the sample code above or update it to suit your needs.
I'm not sure how your build increment logic works but the code above should be enough for you to implement this logic on your own.
I used NewtonSoft to serialize the JSON. NewtonSoft is available from NuGet.
https://www.newtonsoft.com/json/help/html/SerializeWithJsonSerializerToFile.htm#!
https://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm
https://www.nuget.org/packages/Newtonsoft.Json/
All-Star
48320 Points
18008 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 10, 2018 04:38 PM|PatriceSc|LINK
Hi,
VS should do that automatically. According to https://docs.microsoft.com/en-us/dotnet/api/system.reflection.assemblyversionattribute?redirectedfrom=MSDN&view=netframework-4.7.2 :
1.1.1.* should use the number of seconds since midnight.
1.1.. would use the would increment the 3rd component on each day (I assume this is the nuumber of days since a base date ?)
If I remember you had a difference between the C# compiler and the VB compiler in how those numbers are handled. Not sure if still true.
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 10, 2018 06:04 PM|bootzilla|LINK
No this is all in C#, no VB.
As for the versioning number of seconds since midnight, I'm not sure what you mean there. The versioning number should increment during each build or publish.
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 10, 2018 06:06 PM|bootzilla|LINK
I appreciate this but this may not be exactly what I'm looking for. The number should be displayed on a login page, so according to this type of coding where am I going to show the version number?
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 10, 2018 06:10 PM|mgebhard|LINK
All the code does is fetch and write to a file. Can you explain why, given the source code, you are unable to read the file in a web app and write the value to a server control's Text property? Have you tried?
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 10, 2018 06:18 PM|bootzilla|LINK
The current version number is hardcoded on a SiteMaster page, which is essentially a login page, as I show in my original post in this thread. That version number is 1.3.024 currently. I can even start at 1.4.000 or whatever at this point. Going forward I don't want it to be hardcoded and to automatically increment every time it is published to a production server. It needs to be displayed on the login page of the website. That's the reason why.
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 10, 2018 06:33 PM|mgebhard|LINK
I showed you two different ways with source code and documentation to solve this problem. One using standard Visual Studio and a custom version where the version is persisted in a file. I'm not sure what else I can do for you.
If you go with the custom solution above and place the version file in the application root, then this code will write the version to the page.
Version.json file
Global.asax
public class Global : HttpApplication { void Application_Start(object sender, EventArgs e) { // Code that runs on application startup RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); Version version; // deserialize JSON directly from a file using (StreamReader file = File.OpenText(Server.MapPath("Version.json"))) { JsonSerializer serializer = new JsonSerializer(); version = (Version)serializer.Deserialize(file, typeof(Version)); } Application["Version"] = string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3, '0')); } }
Master page
<footer> <p>© <%: DateTime.Now.Year %> - My ASP.NET Application <span><%=Application["Version"] %></span></p> </footer>
All you have to do is run the console app to update the version file. Remember to set the path. You can kick off the console app by entering the path to the console app in the project properties -> Build Events. The console app will run each time there is a build which will update the Version.json file.
https://msdn.microsoft.com/en-us/library/h7dhf0ty.aspx?f=255&MSPPError=-2147217396
Or you can use the standard Visual Studio versioning as recommend above.
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 03:11 PM|bootzilla|LINK
Got a question about the Global.asax file. I get 'The name 'RouteTable' does not exist in the current context when I add that code to the file. I also get the same for RouteConfig , BundleConfig, BundleTable, JsonSerializer. What do I need to do to fix those errors?
This is what my code looks like currently when I add the code you provided to it:
Do I just generate properties for all of those that have that error?
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 03:19 PM|mgebhard|LINK
I'm not sure what you are doing.... Post your original Global.asax file and I'll the paste the code into the file to reduce the errors. Or just paste the highlighted code at the end of the Application_Start handler.
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 03:28 PM|bootzilla|LINK
All I did was paste the code you provided into the Application_Start handler. This was the code in the handler before I pasted.
This is after(highlighted):
protected void Application_Start(object sender, EventArgs e) { PDFConverter = new ThreadSafeConverter( new RemotingToolset<PdfToolset>( new WinAnyCPUEmbeddedDeployment( new TempFolderDeployment()))); // Code that runs on application startup RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); Version version; // deserialize JSON directly from a file using (StreamReader file = File.OpenText(Server.MapPath("Version.json"))) { JsonSerializer serializer = new JsonSerializer(); version = (Version)serializer.Deserialize(file, typeof(Version)); } Application["Version"] = string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3, '0')); }
That's where I get the errors.
All-Star
48320 Points
18008 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 03:51 PM|PatriceSc|LINK
Seems you are missing at the top of your file :
using System.Web.Routing;
I tried my previous suggestion which seems to work out of the box ie using [assembly: AssemblyVersion("1.0.*")] allows the CS compiler to assign a build/revision to each new build (though this is not an increment, it uses what I told earlier to generate unique numbers each time you create a new DLL without having to care about the previous version number).
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 03:57 PM|bootzilla|LINK
using System.Web.Routing;
This fixes RouteTable but I still get errors for RouteConfig, BundleConfig, BundleTable, and JsonSerializer. All the same error 'The name 'RouteConfig' does not exist in current context.
And yes I agree that using [assembly: AssemblyVersion("1.0.*")] will generate some random build number like 1.4.6865.30445 which is not what I want.
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 05:01 PM|mgebhard|LINK
Yeah, you posted the non-highlighted code. Only post the highlighted code. That's why I highlighted it...
Remove this...
All-Star
48320 Points
18008 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 05:41 PM|PatriceSc|LINK
More generally you should have a "light bulb" on this kind of errors and you should then see suggestions such as using the full name ie someNameSpace.RouteConfig or to add a using NameSpace; directive at the top of your file. See https://dirkstrauss.com/visual-studio-2015-light-bulb/
It could be also that a namespace declaration is missing (AFAIK it is common to declare MvcApplication and RouteConfig in the same namespace in which case you won't have this issue).
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 05:46 PM|bootzilla|LINK
For the JsonSerializer error I get 'the type or namespace 'JsonSerializer' could not be found' and the light bulb suggestion is to generate a class for that in a new file. That doesn't sound right.
All-Star
48320 Points
18008 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 06:10 PM|PatriceSc|LINK
Seems you have to add the https://www.nuget.org/packages/newtonsoft.json/ package which is not already part of your project.
If unfamiliar with nuget see https://docs.microsoft.com/en-us/nuget/tools/package-manager-ui (uses the Visual Studio UI) or https://docs.microsoft.com/en-us/nuget/tools/package-manager-console (allows to launch nuget commands from a Visual Studio "console" window).
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 06:37 PM|bootzilla|LINK
Installed and it worked!
OK now I need to get the label or whatever on the master page to show the version number on the login page. Here is the code I have but it doesn't seem to work.
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 06:58 PM|mgebhard|LINK
Use a span tag as recommended in the code above since an ASP label renders as a span anyway.
If you want to use a server control then set the Text property in the Master Page code behind.
This markup is setting the "for" attribute
<asp:Label for =" <%=Application["Version"] %>" runat="server" ID="lblBuildNumber" ForeColor="White" Text="" Visible="true" />
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 07:09 PM|bootzilla|LINK
Using that span I don't get anything to show. I'm not using it in the footer. This is what I have the label for the word 'Build' and then the span tag:
How it looks on the page is 'Build: '
Using it this way I get ' server tags cannot contain <% %> constructs error
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 07:14 PM|mgebhard|LINK
You'll need to set a breakpoint and step through the code to make sure the application variable is set.
Did you copy the Version.json file to the application root?
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 07:18 PM|bootzilla|LINK
Not sure what you mean to make sure application variable is set. Isn't it already set on the global.asax page?
And yes it's on the project root folder, if that is what you mean.
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 07:28 PM|mgebhard|LINK
Maybe background color and font color are the same?
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 07:35 PM|bootzilla|LINK
That was it. I see version number Build: 1.4.001
Now that everything seems to be set up. When I build on that project the next version should show as 1.4.002, correct?
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 07:50 PM|mgebhard|LINK
I will work if you followed the instructions outlined above.
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 07:59 PM|bootzilla|LINK
Not sure exactly how to do this. I'm not good with command lines at all.
So does this go in the pre-build event? If so do I call the path to the version.json file? and put C:/ProjectFolder/version.js in the event command line event?
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 08:20 PM|mgebhard|LINK
I would put the path to the console app in the post_built event. Keep in mind, this is not a polished feature but it should be more than enough to get you started.
The concept is simple though. The post-build event kicks off the console app. The console app updates the version.json file in the web app.
The parts that are missing are placing the console app in a known location and using configuration to write the file location if this is a team feature. Also the solution increments the last value in the Version only. You'll need to modify the console app to increment the other Version parts according to your business rules.
No, the console app writes the file. The post-build event uses the path to the Console app exe. I placed the exe in the web project's bin folder.
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 08:26 PM|bootzilla|LINK
OK I'm confused here. What goes in the post event? Is it C:/ProjectFolder/something.exe? What is this console app exe. Is that something that gets created during the build? I'm just trying to figure this out. You have to keep in mind I haven't done this before so just giving me bits and pieces isn't going to help. You will need to be more specific.
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 08:49 PM|mgebhard|LINK
The console app idea was explained above.
https://forums.asp.net/post/6233357.aspx
I came up with this idea because you do not want to use the built-in VS feature and the versioning scheme is custom.
I'm not sure what I can do for you if you feel that way. From my perspective, I've been more than specific but you ignore the advice, don't try the source code, or even read the posted links. Case in point, the use of NewtonSoft to serialize JSON including the NuGet link.
It seems you want someone to craft an entire solution according to your requirements. You have to put in some work as well.
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 23, 2018 08:53 PM|bootzilla|LINK
Man you are incredibly difficult at times. I HAVENT DONE THIS BEFORE. What part of that do you not understand. You are going to have to break this down for me please. You didn't explain what exactly the console app is. Is it global.asax? Is it something else. What goes in the build event specifically. I don't know that's why I'm asking. Telling me you are not sure what you can do for me is bs. YES YOU CAN. You are choosing not to for some reason. If you take the time to break down each part of what I need to do in the build events that would be a very helpful start.
And no I don't want someone to craft the entire solution. That is complete bs as well. Where do you get these crazy ideas. This not even close to an entire solution. I need assistance on a very small piece that I have not done before. That's why I came here to ask for help. I don't know anything about build events and I'm asking for help in that direction. You seem to not understand this. I'm asking for it very cordially. You are not and have been very difficult throughout this process.
So I'm going to ask again what do I put in the build event SPECIFICALLY. I don't know what to put there since I haven't done this before.
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 24, 2018 01:50 PM|bootzilla|LINK
Can anyone assist me in finishing this please. I don't know much about post event builds and I need to know what to put in there and the person that pointed me in this direction doesn't seem to want to help anymore.
I would really appreciate someone assisting me with this.
All-Star
48320 Points
18008 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 24, 2018 02:35 PM|PatriceSc|LINK
See https://www.codeproject.com/Tips/1199557/Visual-Studio-Pre-Post-Build-Events-Command-Line and https://www.codeproject.com/articles/80862/autoincrement-version-in-visual-studio
The pre build event allows to launch an app each time you build the application. What runs would be a console app that you have to write and that would increment the build version (the sample above just use the AssemblyInfo.cs file).
It could still skip numbers as you may build several times before publishing the next version on the web site (which is likely why the default VS approach favors uniqueness and de-emphasize using consecutive numbers)
Or for now just include manually incrementing the build number as part of a post deployment procedure ?
Member
193 Points
1128 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 24, 2018 02:40 PM|bootzilla|LINK
First I appreciate the feedback. I also want to state that this is not my idea for incrementing. The person that wants it done this way is a client that I'm working for. If it were up to me I would just use the default method of 1.2.* in the assembly version and go from there. This has to be a custom build so starting with build version 1.4.001 and then each build increments that last number so the next would be 1.4.002 and so on.
Based off of what mgehard said the console app has to be invoked in the post build event. I don't know much about it at all. I don't know specifically what code/syntax goes there. Does this make sense?
All-Star
52241 Points
23304 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 24, 2018 03:03 PM|mgebhard|LINK
Huh? Well, the threatening PMs of harm don't help and, like many of us, have other stuff to do.
The following post has the a console app.
https://forums.asp.net/post/6233357.aspx
Create you're own console application in Visual Studio and copy the main() method to your new console app. Be sure to add the NewtonSoft NuGet package.
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Reflection; using System.Runtime.Serialization.Json; using System.Text; using System.Threading.Tasks; namespace ConsoleCs { class Program { static void Main(string[] args) { Version version; // deserialize JSON directly from a file using (StreamReader file = File.OpenText(@"./Version.json")) { JsonSerializer serializer = new JsonSerializer(); version = (Version)serializer.Deserialize(file, typeof(Version)); } Console.WriteLine("Current Version:\t{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3, '0')); //Create a new Version and increment Version newVersion = new Version(version.Major, version.Minor, version.Build + 1, version.Revision); Console.WriteLine("Incremented Version:\t{0}.{1}.{2}", newVersion.Major, newVersion.Minor, newVersion.Build.ToString().PadLeft(3, '0')); // serialize JSON directly to a file using (StreamWriter file = File.CreateText(@"./Version.json")) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(file, newVersion); } } } }
Build the app - do not run the app. Copy the "Version.json" file to the console app's bin/debug folder. You can find the bin folder by right clicking the project in solution explorer and selecting "Open folder in file explorer". Then just open the bin/debug folder. Run the Console app and it should update the Version.json file in the bin folder.
Update the following two lines of code by replacing "Version.json" with the fully qualified path to the Version.json file in your web application.
It will look something like this... Note the "@".
using (StreamReader file = File.OpenText(@"C:\Path\to\the\Web\App\Version.json"))
Run the app again and it should update the Version.json file located in the web app.
Copy the console app (exe) to the web app's bin folder. Open the console app's bin/debug folder. Find the exe file that has the same name as your console app and copy the exe to your web app's bin folder. Also copy the *.exe.config file.
Open the web app's properties and go to the Build Events. Enter the exe file name; ie. MyConsoleApp.exe.
Now every time the project is build, the console app will run and update the Version.json File.
As PatriceSc suggested, this will update the Version every time the project is built. Not when deployed. But, you can trigger the console app whenever you like.
Lastly, this is not a polished production feature as it only increments the last number 1.0.XXX. I'm pretty sure you still need to implement your Versioning business logic. You have all the source code so modify it to suit your needs.
All-Star
48320 Points
18008 Posts
Re: How to have an auto incrementing version number (Visual Studio)?
Oct 24, 2018 03:29 PM|PatriceSc|LINK
It's basically just a command line and you can use $(variable) values that are replaced before it runs so that you can transmit to this app parameters that depends on the current solution (such as the root folder for your solution etc...). It's likely rather on the pre build event.
If I had to do that I wonder if I wouldn't consider this to be rather a "deployment counter". For example I would have perhaps a table with the actual version and/or DLL modification date (maybe not taking the time into account) to register in my db a new "deployment" each time a new DLL is first used.