NServiceBus.FileVersionRetriever.GetFileVersion C# (CSharp) Method

GetFileVersion() public static method

Retrieves a semver compliant version from a Type.
public static GetFileVersion ( Type type ) : string
type System.Type to retrieve version from.
return string
        public static string GetFileVersion(Type type)
        {
            if (!string.IsNullOrEmpty(type.Assembly.Location))
            {
                var fileVersion = FileVersionInfo.GetVersionInfo(type.Assembly.Location);

                return new Version(fileVersion.FileMajorPart, fileVersion.FileMinorPart, fileVersion.FileBuildPart).ToString(3);
            }

            var customAttributes = type.Assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);

            if (customAttributes.Length >= 1)
            {
                var fileVersion = (AssemblyFileVersionAttribute) customAttributes[0];
                Version version;
                if (Version.TryParse(fileVersion.Version, out version))
                {
                    return version.ToString(3);
                }
            }

            return type.Assembly.GetName().Version.ToString(3);
        }
    }

Usage Example

        public GenericHost(IConfigureThisEndpoint specifier, string[] args, List <Type> defaultProfiles, string endpointName, IEnumerable <string> scannableAssembliesFullName = null)
        {
            this.specifier = specifier;

            if (String.IsNullOrEmpty(endpointName))
            {
                endpointName = specifier.GetType().Namespace ?? specifier.GetType().Assembly.GetName().Name;
            }


            endpointNameToUse    = endpointName;
            endpointVersionToUse = FileVersionRetriever.GetFileVersion(specifier.GetType());

            if (scannableAssembliesFullName == null || !scannableAssembliesFullName.Any())
            {
                var assemblyScanner = new AssemblyScanner();
                assemblyScanner.MustReferenceAtLeastOneAssembly.Add(typeof(IHandleMessages <>).Assembly);
                assembliesToScan = assemblyScanner
                                   .GetScannableAssemblies()
                                   .Assemblies;
            }
            else
            {
                assembliesToScan = scannableAssembliesFullName
                                   .Select(Assembly.Load)
                                   .ToList();
            }

            profileManager = new ProfileManager(assembliesToScan, args, defaultProfiles);

            wcfManager = new WcfManager();
        }
All Usage Examples Of NServiceBus.FileVersionRetriever::GetFileVersion
FileVersionRetriever