1

I have an application that manage IIS Application instances so I am looking for a kind of GIUD to identify each applications. This GUID must be created when the application is deployed in IIS and must be persistent to IIS/Windows updates/restarts.

I did not need the use of Microsoft.Web.Administration: I want a simple way, for each IIS application, it returns its unique ID (by a method called within it).

Here is an example of what I'm looking for and I'd like to have an unique id returned by this.????? :

public class MvcApplication : System.Web.HttpApplication
{
     string myUniqueID {
         get { return this.?????; }
     }
}

Thanks for help.

fnhieu
  • 13
  • 1
  • 6
  • 1
    Um.... [`Guid.NewGuid`](http://msdn.microsoft.com/en-us/library/system.guid.newguid(v=vs.110).aspx)? – tnw Feb 10 '14 at 15:37
  • Yeah, so make a GUID field and assign it a value on startup. Then return it in that method. – tnw Feb 10 '14 at 16:11
  • This value must be assigned by IIS in that case, and must not be the same if a deploy the same application in another IIS instance. So this value must be set when I create my IIS Application instance (in the IIS console) and must be saved whenever I restart IIS or the server. – fnhieu Feb 10 '14 at 16:23
  • We might do better if you told us what *problem* this persistent, unique identifier is meant to help you to solve. – Damien_The_Unbeliever Feb 13 '14 at 10:49
  • When I deploy my MVC.NET application, I would like a unique identifier is created and does not change if I turn off the server or if I make updates to Windows or IIS. This UID, then I want it back in my global.asax to manage instances of these applications that can be deployed on IIS, these UID in a centralized Oracle database and then managed in a third party application (logging, etc.). . So IIS UID is automatically generated during deployment and in which case I get it and in this case I would like to know how, or in another case I do not know how. – fnhieu Feb 13 '14 at 14:39

3 Answers3

1

HostingEnvironment.ApplicationID

https://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.applicationid(v=vs.110).aspx

jazzcat
  • 4,351
  • 5
  • 36
  • 37
0

You can use the assembly GUID for this purpose: In AssemblyInfo.cs, you can find

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("307E39B9-2C41-40CF-B29F-84C8BBCD6519")]

To read this value, you can use:

public static string AssemblyGUID
{
  get {
    var assembly = Assembly.GetExecutingAssembly();
    var attribute = (System.Runtime.InteropServices.GuidAttribute)assembly.GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), true)[0];
    var GUID = attribute.Value;
    return GUID;
  }
}

which is taken from another SO answer (you can find it here).


And if it is required, Visual Studio allows you to create a new GUID via menu Tools -> Create GUID - if you need a different one.

Or in C# you simply use

var newGuid=(Guid.NewGuid()).ToString();
Console.WriteLine(newGuid);

to create a new GUID.

Matt
  • 25,467
  • 18
  • 120
  • 187
0

I had to do something similar.

  1. Read the web.config file for a HostId setting. Preferably split your configuration file into two, with one config file that is local to the install, and doesn't get replaced upon upgrading to a new version of the website.
  2. If the HostId value doesn't exist in the web.config, call Guid.NewGuid() to generate a new value.
  3. Save the new value to the web config, preferably in the local section/file.
  4. Return the value.

Here is some psuedo-code:

    public Guid HostId
    {
        get
        {
            var result = GetSetting(ConfigFileLocalSettingList.HostId).TryToGuid();
            if (result == null)
            {
                result = Guid.NewGuid();
                SetSetting(ConfigFileLocalSettingList.HostId, result.ToString());
                Save();
            }
            return result.Value;
        }
    }
Casey Plummer
  • 2,629
  • 23
  • 20