Commit 80c9a39c52b283570beeee3a1e6c0af7233f302f
1 parent
2352c68d
- Hangfire segítségével megoldásra került, hogy az ütemezett riportokat a web alkalmazás futtatja,
tehát nem kell a service hozzá; - ez a funkció az xml paraméterek között ki/be kapcsolható; - értelemszerűen, ha ki van kapcsolva, akkor a szervizt kell a megfelelő módon használni! - a webalkalmazás automatikus indítása és folyamatos futtatása is beépítésre került
Showing
3 changed files
with
225 additions
and
199 deletions
Show diff stats
Vrh.Web.Reporting/Global.asax.cs
| @@ -15,14 +15,16 @@ using Hangfire; | @@ -15,14 +15,16 @@ using Hangfire; | ||
| 15 | using Microsoft.Web.Administration; | 15 | using Microsoft.Web.Administration; |
| 16 | using Vrh.iScheduler; | 16 | using Vrh.iScheduler; |
| 17 | using Vrh.Logger; | 17 | using Vrh.Logger; |
| 18 | - | 18 | +using Vrh.Web.HangfireBootstrapperNS; |
| 19 | namespace Vrh.Web.Reporting | 19 | namespace Vrh.Web.Reporting |
| 20 | { | 20 | { |
| 21 | public class MvcApplication : System.Web.HttpApplication | 21 | public class MvcApplication : System.Web.HttpApplication |
| 22 | { | 22 | { |
| 23 | protected void Application_Start() | 23 | protected void Application_Start() |
| 24 | { | 24 | { |
| 25 | - (new DCLogEntry(LogLevel.Information, "Vrh.Web.Reporting.MvcApplication.Application_Start")).Write(); | 25 | + var methodfullname = $"{typeof(HangfireBootstrapper).FullName}.{System.Reflection.MethodBase.GetCurrentMethod().Name}"; |
| 26 | + | ||
| 27 | + (new DCLogEntry(LogLevel.Information, methodfullname)).Write(); | ||
| 26 | AreaRegistration.RegisterAllAreas(); | 28 | AreaRegistration.RegisterAllAreas(); |
| 27 | FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | 29 | FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); |
| 28 | RouteConfig.RegisterRoutes(RouteTable.Routes); | 30 | RouteConfig.RegisterRoutes(RouteTable.Routes); |
| @@ -51,7 +53,7 @@ namespace Vrh.Web.Reporting | @@ -51,7 +53,7 @@ namespace Vrh.Web.Reporting | ||
| 51 | /// Here NameOfTheAssemblyContainingTheAutostartProviderClass is the name of (for example the dll), but without the extension. | 53 | /// Here NameOfTheAssemblyContainingTheAutostartProviderClass is the name of (for example the dll), but without the extension. |
| 52 | ////////////////////////////////////////////////////////////////////////////////////// | 54 | ////////////////////////////////////////////////////////////////////////////////////// |
| 53 | 55 | ||
| 54 | - (new ApplicationPreload())._Preload(); | 56 | + (new HangfireBootstrapper()).Initialize(); |
| 55 | 57 | ||
| 56 | //Vrh.Web.Menu inicializáló beállításai | 58 | //Vrh.Web.Menu inicializáló beállításai |
| 57 | Vrh.Web.Menu.Global.CustomerLogo = "~/Content/Images/Menu_LearLogo.jpg"; | 59 | Vrh.Web.Menu.Global.CustomerLogo = "~/Content/Images/Menu_LearLogo.jpg"; |
| @@ -63,7 +65,8 @@ namespace Vrh.Web.Reporting | @@ -63,7 +65,8 @@ namespace Vrh.Web.Reporting | ||
| 63 | 65 | ||
| 64 | protected void Application_End(object sender, EventArgs e) | 66 | protected void Application_End(object sender, EventArgs e) |
| 65 | { | 67 | { |
| 66 | - (new DCLogEntry(LogLevel.Information, "Vrh.Web.Reporting.MvcApplication.Application_End")).Write(); | 68 | + var methodfullname = $"{typeof(HangfireBootstrapper).FullName}.{System.Reflection.MethodBase.GetCurrentMethod().Name}"; |
| 69 | + (new DCLogEntry(LogLevel.Information, methodfullname)).Write(); | ||
| 67 | HangfireBootstrapper.Instance.Stop(); | 70 | HangfireBootstrapper.Instance.Stop(); |
| 68 | } | 71 | } |
| 69 | /// <summary> | 72 | /// <summary> |
| @@ -77,199 +80,4 @@ namespace Vrh.Web.Reporting | @@ -77,199 +80,4 @@ namespace Vrh.Web.Reporting | ||
| 77 | //int i = 1; | 80 | //int i = 1; |
| 78 | } | 81 | } |
| 79 | } | 82 | } |
| 80 | - public class ApplicationPreload : System.Web.Hosting.IProcessHostPreloadClient | ||
| 81 | - { | ||
| 82 | - public void Preload(string[] parameters) | ||
| 83 | - { | ||
| 84 | - (new DCLogEntry(LogLevel.Information, "Vrh.Web.Reporting.ApplicationPreload")).Write(); | ||
| 85 | - _Preload(); | ||
| 86 | - } | ||
| 87 | - public void _Preload() | ||
| 88 | - { | ||
| 89 | - SetupAutoStart(typeof(ApplicationPreload)); | ||
| 90 | - HangfireBootstrapper.Instance.SqlDBconnectionstring = Vrh.XmlProcessing.ConnectionStringStore.GetSQL(HANGFIRESQLDBCONNECTIONSTRINGNAME); | ||
| 91 | - HangfireBootstrapper.Instance.Start(); | ||
| 92 | - Vrh.iScheduler.Monitor.InitHangfire(HangfireBootstrapper.Instance._backgroundJobServer); | ||
| 93 | - } | ||
| 94 | - | ||
| 95 | - public const string HANGFIRESQLDBCONNECTIONSTRINGNAME = "MAINDBLOG4PRO"; | ||
| 96 | - public const string CONFIGFILEDIRECTORY = @"inetsrv\config"; | ||
| 97 | - public const string CONFIGFILENAME = @"applicationHost.config"; | ||
| 98 | - //public const string FILEPATH = @"C:\temp\applicationHost.config"; | ||
| 99 | - public const string POOLNAME = "Log4ProIS_REPORTING"; | ||
| 100 | - public const string WEBAPPNAME = "/Log4ProIS-REPORTING"; | ||
| 101 | - public const string WEBSITENAME = "Default Web Site"; | ||
| 102 | - public const string AUTOSTARTPROVIDERNAME = "ApplicationPreload"; | ||
| 103 | - public const string AUTOSTARTPROVIDERTYPENAME = "Vrh.Web.Reporting.ApplicationPreload"; | ||
| 104 | - public const string AUTOSTARTPROVIDERASSEMBLYNAME = "Vrh.Web.Reporting"; | ||
| 105 | - | ||
| 106 | - public static bool SetupAutoStart(Type serviceAutoStartProviderType) | ||
| 107 | - { | ||
| 108 | - const string SYSTEMAPPLICATIONHOST_ELEMENT = "system.applicationHost"; | ||
| 109 | - const string APPLICATIONPOOLS_ELEMENT = "applicationPools"; | ||
| 110 | - const string SITES_ELEMENT = "sites"; | ||
| 111 | - const string SERVICEAUTOSTARTPROVIDERS_ELEMENT = "serviceAutoStartProviders"; | ||
| 112 | - const string SITE_ELEMENT = "site"; | ||
| 113 | - const string APPLICATION_ELEMENT = "application"; | ||
| 114 | - const string ADD_ELEMENT = "add"; | ||
| 115 | - const string NAME_ATTRIBUTE = "name"; | ||
| 116 | - const string TYPE_ATTRIBUTE = "type"; | ||
| 117 | - const string SERVICEAUTOSTARTENABLED_ATTRIBUTE = "serviceAutoStartEnabled"; | ||
| 118 | - const string SERVICEAUTOSTARTPROVIDER_ATTRIBUTE = "serviceAutoStartProvider"; | ||
| 119 | - const string APPLICATIONPOOL_ATTRIBUTE = "applicationPool"; | ||
| 120 | - const string PATH_ATTRIBUTE = "path"; | ||
| 121 | - const string AUTOSTART_ATTRIBUTE = "autoStart"; | ||
| 122 | - const string STARTMODE_ATTRIBUTE = "startMode"; | ||
| 123 | - | ||
| 124 | - string applicationpoolname = GetCurrentApplicationPoolName(); | ||
| 125 | - string websitename = System.Web.Hosting.HostingEnvironment.SiteName; | ||
| 126 | - string webapplicationname = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath; | ||
| 127 | - string autostartprovidername = (websitename + "____" + applicationpoolname + "____" + webapplicationname).Replace(" ", "_").Replace("-", "_").Replace("/", "_"); | ||
| 128 | - string serviceAutoStartProviderTypeFullName = serviceAutoStartProviderType.FullName; | ||
| 129 | - string serviceAutoStartProviderAssassemblyName = serviceAutoStartProviderType.Assembly.GetName().Name; | ||
| 130 | - var le = new DCLogEntry(LogLevel.Information, "Vrh.Web.Reporting.MvcApplication.SetupAutoStart"); | ||
| 131 | - try | ||
| 132 | - { | ||
| 133 | - string configfilepath = Path.Combine(NativeSystemPath, CONFIGFILEDIRECTORY, CONFIGFILENAME); | ||
| 134 | - le.AddDataField("configfilepath", configfilepath); | ||
| 135 | - le.AddDataField("applicationpoolname", applicationpoolname); | ||
| 136 | - le.AddDataField("websitename", websitename); | ||
| 137 | - le.AddDataField("webapplicationname", webapplicationname); | ||
| 138 | - le.AddDataField("serviceAutoStartProviderTypeFullName", serviceAutoStartProviderTypeFullName); | ||
| 139 | - le.AddDataField("serviceAutoStartProviderAssassemblyName", serviceAutoStartProviderAssassemblyName); | ||
| 140 | - | ||
| 141 | - | ||
| 142 | - var filecontent = System.IO.File.ReadAllText(configfilepath); | ||
| 143 | - XElement configxml = XElement.Parse(filecontent, LoadOptions.PreserveWhitespace); //XElement.Load(configfilepath); | ||
| 144 | - | ||
| 145 | - XElement myconfigrootxml = configxml.Element(XName.Get(SYSTEMAPPLICATIONHOST_ELEMENT)); | ||
| 146 | - XElement myapplicationpooladdelement = myconfigrootxml?.Element(XName.Get(APPLICATIONPOOLS_ELEMENT))? | ||
| 147 | - .Elements(XName.Get(ADD_ELEMENT))?.FirstOrDefault(e => e.Attribute(XName.Get(NAME_ATTRIBUTE)).Value == applicationpoolname); | ||
| 148 | - XElement mysiteapplicationelement = myconfigrootxml?.Element(XName.Get(SITES_ELEMENT))? | ||
| 149 | - .Elements(XName.Get(SITE_ELEMENT))?.FirstOrDefault(e => e.Attribute(XName.Get(NAME_ATTRIBUTE)).Value == websitename) | ||
| 150 | - .Elements(XName.Get(APPLICATION_ELEMENT))?.FirstOrDefault(e => e.Attribute(XName.Get(PATH_ATTRIBUTE)).Value == webapplicationname && e.Attribute(XName.Get(APPLICATIONPOOL_ATTRIBUTE)).Value == applicationpoolname); | ||
| 151 | - if (myapplicationpooladdelement == null) return false; | ||
| 152 | - if (mysiteapplicationelement == null) return false; | ||
| 153 | - | ||
| 154 | - bool configchanged = false; | ||
| 155 | - configchanged = SetOrAddAttribute(AUTOSTART_ATTRIBUTE, myapplicationpooladdelement, "true") || configchanged; | ||
| 156 | - configchanged = SetOrAddAttribute(STARTMODE_ATTRIBUTE, myapplicationpooladdelement, "AlwaysRunning") || configchanged; | ||
| 157 | - configchanged = SetOrAddAttribute(SERVICEAUTOSTARTENABLED_ATTRIBUTE, mysiteapplicationelement, "true") || configchanged; | ||
| 158 | - configchanged = SetOrAddAttribute(SERVICEAUTOSTARTPROVIDER_ATTRIBUTE, mysiteapplicationelement, autostartprovidername) || configchanged; | ||
| 159 | - | ||
| 160 | - XElement myautostartproviderselement = null; | ||
| 161 | - XElement myautostartprovideraddelement = null; | ||
| 162 | - configchanged = SetOrAddElement(SERVICEAUTOSTARTPROVIDERS_ELEMENT, myconfigrootxml, "", out myautostartproviderselement) || configchanged; | ||
| 163 | - configchanged = SetOrAddElementWithSelectorAttribute(ADD_ELEMENT, myautostartproviderselement, NAME_ATTRIBUTE, autostartprovidername, "", out myautostartprovideraddelement) || configchanged; | ||
| 164 | - configchanged = SetOrAddAttribute(TYPE_ATTRIBUTE, myautostartprovideraddelement, $"{serviceAutoStartProviderTypeFullName},{serviceAutoStartProviderAssassemblyName }") || configchanged; | ||
| 165 | - | ||
| 166 | - if (configchanged) | ||
| 167 | - { | ||
| 168 | - //configxml.Save(configfilepath); | ||
| 169 | - System.IO.File.WriteAllText(configfilepath, configxml.ToString()); | ||
| 170 | - } | ||
| 171 | - le.AddDataField("configchanged", configchanged); | ||
| 172 | - //le.AddDataField("myconfigrootxml", myconfigrootxml); | ||
| 173 | - le.AddSuccessResult("SUCCESS"); | ||
| 174 | - return true; | ||
| 175 | - } | ||
| 176 | - catch (Exception ex) { le.AddExceptionResult(ex); le.SetLogLevel(LogLevel.Error); return false; } | ||
| 177 | - finally { le.Write(); } | ||
| 178 | - } | ||
| 179 | - private static bool SetOrAddElement(string elementname, XElement elementcontainer, string elementvalue, out XElement myelement) | ||
| 180 | - { | ||
| 181 | - bool configchanged = false; | ||
| 182 | - myelement = elementcontainer?.Element(XName.Get(elementname)); | ||
| 183 | - if (myelement == null) { myelement = new XElement(XName.Get(elementname), ""); elementcontainer.Add(myelement); configchanged = true; } | ||
| 184 | - if (myelement.Value != elementvalue) { myelement.Value = elementvalue; configchanged = true; } | ||
| 185 | - return configchanged; | ||
| 186 | - } | ||
| 187 | - private static bool SetOrAddElementWithSelectorAttribute(string elementname, XElement elementcontainer, string selectorattributename, string selectorattributevalue, string elementvalue, out XElement myelement) | ||
| 188 | - { | ||
| 189 | - bool configchanged = false; | ||
| 190 | - myelement = elementcontainer?.Elements(XName.Get(elementname))?.FirstOrDefault(e => e.Attribute(XName.Get(selectorattributename)).Value == selectorattributevalue); | ||
| 191 | - if (myelement == null) | ||
| 192 | - { | ||
| 193 | - myelement = new XElement(XName.Get(elementname), ""); | ||
| 194 | - elementcontainer.Add(myelement); | ||
| 195 | - SetOrAddAttribute(selectorattributename, myelement, selectorattributevalue); | ||
| 196 | - configchanged = true; | ||
| 197 | - } | ||
| 198 | - if (myelement.Value != elementvalue) { myelement.Value = elementvalue; configchanged = true; } | ||
| 199 | - return configchanged; | ||
| 200 | - } | ||
| 201 | - private static bool SetOrAddAttribute(string attrname, XElement attrcontainer, string attrvalue) | ||
| 202 | - { | ||
| 203 | - bool configchanged = false; | ||
| 204 | - XAttribute attr = attrcontainer.Attribute(XName.Get(attrname)); | ||
| 205 | - if (attr == null) { attr = new XAttribute(XName.Get(attrname), ""); attrcontainer.Add(attr); configchanged = true; } | ||
| 206 | - if (attr.Value != attrvalue) { attr.Value = attrvalue; configchanged = true; } | ||
| 207 | - return configchanged; | ||
| 208 | - } | ||
| 209 | - public static string NativeSystemPath | ||
| 210 | - { | ||
| 211 | - get | ||
| 212 | - { | ||
| 213 | - if (Environment.Is64BitOperatingSystem) | ||
| 214 | - { | ||
| 215 | - return System.IO.Path.Combine( | ||
| 216 | - Environment.GetFolderPath(Environment.SpecialFolder.Windows), | ||
| 217 | - "Sysnative"); | ||
| 218 | - } | ||
| 219 | - return Environment.GetFolderPath(Environment.SpecialFolder.System); | ||
| 220 | - } | ||
| 221 | - } | ||
| 222 | - | ||
| 223 | - public static string GetCurrentApplicationPoolName() | ||
| 224 | - { | ||
| 225 | - string appPoolName = string.Empty; | ||
| 226 | - foreach (Application app in (new ServerManager()).Sites[System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName()].Applications) | ||
| 227 | - { | ||
| 228 | - if (app.Path == HttpRuntime.AppDomainAppVirtualPath) { appPoolName = app.ApplicationPoolName; } | ||
| 229 | - } | ||
| 230 | - return appPoolName; | ||
| 231 | - } | ||
| 232 | - } | ||
| 233 | - public class HangfireBootstrapper : IRegisteredObject | ||
| 234 | - { | ||
| 235 | - public static readonly HangfireBootstrapper Instance = new HangfireBootstrapper(); | ||
| 236 | - public BackgroundJobServer _backgroundJobServer { get; set; } | ||
| 237 | - public string SqlDBconnectionstring { get; set; } = null; | ||
| 238 | - private readonly object _lockObject = new object(); | ||
| 239 | - private bool _started; | ||
| 240 | - | ||
| 241 | - | ||
| 242 | - private HangfireBootstrapper() | ||
| 243 | - { | ||
| 244 | - } | ||
| 245 | - | ||
| 246 | - public void Start() | ||
| 247 | - { | ||
| 248 | - lock (_lockObject) | ||
| 249 | - { | ||
| 250 | - if (_started) return; | ||
| 251 | - _started = true; | ||
| 252 | - | ||
| 253 | - HostingEnvironment.RegisterObject(this); | ||
| 254 | - | ||
| 255 | - GlobalConfiguration.Configuration | ||
| 256 | - .UseSqlServerStorage(SqlDBconnectionstring) | ||
| 257 | - // Specify other options here | ||
| 258 | - ; | ||
| 259 | - | ||
| 260 | - _backgroundJobServer = new BackgroundJobServer(); | ||
| 261 | - } | ||
| 262 | - } | ||
| 263 | - | ||
| 264 | - public void Stop() | ||
| 265 | - { | ||
| 266 | - lock (_lockObject) | ||
| 267 | - { | ||
| 268 | - if (_backgroundJobServer != null) { _backgroundJobServer.Dispose(); } | ||
| 269 | - HostingEnvironment.UnregisterObject(this); | ||
| 270 | - } | ||
| 271 | - } | ||
| 272 | - | ||
| 273 | - void IRegisteredObject.Stop(bool immediate) { Stop(); } | ||
| 274 | - } | ||
| 275 | } | 83 | } |
| @@ -0,0 +1,217 @@ | @@ -0,0 +1,217 @@ | ||
| 1 | +using System.Text; | ||
| 2 | +using System.Threading.Tasks; | ||
| 3 | +using System; | ||
| 4 | +using System.Collections.Generic; | ||
| 5 | +using System.Diagnostics; | ||
| 6 | +using System.IO; | ||
| 7 | +using System.Linq; | ||
| 8 | +using System.Net; | ||
| 9 | +using System.Threading; | ||
| 10 | +using System.Web; | ||
| 11 | +using System.Web.Hosting; | ||
| 12 | +using System.Web.Mvc; | ||
| 13 | +using System.Web.Optimization; | ||
| 14 | +using System.Web.Routing; | ||
| 15 | +using System.Xml.Linq; | ||
| 16 | +using Hangfire; | ||
| 17 | +using Microsoft.Web.Administration; | ||
| 18 | +using Vrh.Logger; | ||
| 19 | + | ||
| 20 | +namespace Vrh.Web.HangfireBootstrapperNS | ||
| 21 | +{ | ||
| 22 | + public class HangfireBootstrapper : IRegisteredObject, IProcessHostPreloadClient | ||
| 23 | + { | ||
| 24 | + public void Preload(string[] parameters) | ||
| 25 | + { | ||
| 26 | + var methodfullname = $"{typeof(HangfireBootstrapper).FullName}.{System.Reflection.MethodBase.GetCurrentMethod().Name}"; | ||
| 27 | + (new DCLogEntry(LogLevel.Information, methodfullname)).Write(); | ||
| 28 | + Initialize(); | ||
| 29 | + } | ||
| 30 | + public void Initialize() | ||
| 31 | + { | ||
| 32 | + SetupAutoStart(typeof(HangfireBootstrapper)); | ||
| 33 | + HangfireBootstrapper.Instance.SqlDBconnectionstring = Vrh.XmlProcessing.ConnectionStringStore.GetSQL(HANGFIRESQLDBCONNECTIONSTRINGNAME); | ||
| 34 | + HangfireBootstrapper.Instance.Start(); | ||
| 35 | + Vrh.iScheduler.Monitor.InitHangfire(HangfireBootstrapper.Instance._backgroundJobServer); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + private const string HANGFIRESQLDBCONNECTIONSTRINGNAME = "MAINDBLOG4PRO"; | ||
| 39 | + private const string CONFIGFILEDIRECTORY = @"inetsrv\config"; | ||
| 40 | + private const string CONFIGFILENAME = @"applicationHost.config"; | ||
| 41 | + //public const string FILEPATH = @"C:\temp\applicationHost.config"; | ||
| 42 | + private const string POOLNAME = "Log4ProIS_REPORTING"; | ||
| 43 | + private const string WEBAPPNAME = "/Log4ProIS-REPORTING"; | ||
| 44 | + private const string WEBSITENAME = "Default Web Site"; | ||
| 45 | + private const string AUTOSTARTPROVIDERNAME = "ApplicationPreload"; | ||
| 46 | + private const string AUTOSTARTPROVIDERTYPENAME = "Vrh.Web.Reporting.ApplicationPreload"; | ||
| 47 | + private const string AUTOSTARTPROVIDERASSEMBLYNAME = "Vrh.Web.Reporting"; | ||
| 48 | + | ||
| 49 | + private static bool SetupAutoStart(Type serviceAutoStartProviderType) | ||
| 50 | + { | ||
| 51 | + const string SYSTEMAPPLICATIONHOST_ELEMENT = "system.applicationHost"; | ||
| 52 | + const string APPLICATIONPOOLS_ELEMENT = "applicationPools"; | ||
| 53 | + const string SITES_ELEMENT = "sites"; | ||
| 54 | + const string SERVICEAUTOSTARTPROVIDERS_ELEMENT = "serviceAutoStartProviders"; | ||
| 55 | + const string SITE_ELEMENT = "site"; | ||
| 56 | + const string APPLICATION_ELEMENT = "application"; | ||
| 57 | + const string ADD_ELEMENT = "add"; | ||
| 58 | + const string NAME_ATTRIBUTE = "name"; | ||
| 59 | + const string TYPE_ATTRIBUTE = "type"; | ||
| 60 | + const string SERVICEAUTOSTARTENABLED_ATTRIBUTE = "serviceAutoStartEnabled"; | ||
| 61 | + const string SERVICEAUTOSTARTPROVIDER_ATTRIBUTE = "serviceAutoStartProvider"; | ||
| 62 | + const string APPLICATIONPOOL_ATTRIBUTE = "applicationPool"; | ||
| 63 | + const string PATH_ATTRIBUTE = "path"; | ||
| 64 | + const string AUTOSTART_ATTRIBUTE = "autoStart"; | ||
| 65 | + const string STARTMODE_ATTRIBUTE = "startMode"; | ||
| 66 | + | ||
| 67 | + string applicationpoolname = GetCurrentApplicationPoolName(); | ||
| 68 | + string websitename = System.Web.Hosting.HostingEnvironment.SiteName; | ||
| 69 | + string webapplicationname = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath; | ||
| 70 | + string autostartprovidername = (websitename + "____" + applicationpoolname + "____" + webapplicationname).Replace(" ", "_").Replace("-", "_").Replace("/", "_"); | ||
| 71 | + string serviceAutoStartProviderTypeFullName = serviceAutoStartProviderType.FullName; | ||
| 72 | + string serviceAutoStartProviderAssassemblyName = serviceAutoStartProviderType.Assembly.GetName().Name; | ||
| 73 | + var methodfullname = $"{typeof(HangfireBootstrapper).FullName}.{System.Reflection.MethodBase.GetCurrentMethod().Name}"; | ||
| 74 | + var le = new DCLogEntry(LogLevel.Information, methodfullname); | ||
| 75 | + try | ||
| 76 | + { | ||
| 77 | + string configfilepath = Path.Combine(NativeSystemPath, CONFIGFILEDIRECTORY, CONFIGFILENAME); | ||
| 78 | + le.AddDataField("configfilepath", configfilepath); | ||
| 79 | + le.AddDataField("applicationpoolname", applicationpoolname); | ||
| 80 | + le.AddDataField("websitename", websitename); | ||
| 81 | + le.AddDataField("webapplicationname", webapplicationname); | ||
| 82 | + le.AddDataField("serviceAutoStartProviderTypeFullName", serviceAutoStartProviderTypeFullName); | ||
| 83 | + le.AddDataField("serviceAutoStartProviderAssassemblyName", serviceAutoStartProviderAssassemblyName); | ||
| 84 | + | ||
| 85 | + | ||
| 86 | + var filecontent = System.IO.File.ReadAllText(configfilepath); | ||
| 87 | + XElement configxml = XElement.Parse(filecontent, LoadOptions.PreserveWhitespace); //XElement.Load(configfilepath); | ||
| 88 | + | ||
| 89 | + XElement myconfigrootxml = configxml.Element(XName.Get(SYSTEMAPPLICATIONHOST_ELEMENT)); | ||
| 90 | + XElement myapplicationpooladdelement = myconfigrootxml?.Element(XName.Get(APPLICATIONPOOLS_ELEMENT))? | ||
| 91 | + .Elements(XName.Get(ADD_ELEMENT))?.FirstOrDefault(e => e.Attribute(XName.Get(NAME_ATTRIBUTE)).Value == applicationpoolname); | ||
| 92 | + XElement mysiteapplicationelement = myconfigrootxml?.Element(XName.Get(SITES_ELEMENT))? | ||
| 93 | + .Elements(XName.Get(SITE_ELEMENT))?.FirstOrDefault(e => e.Attribute(XName.Get(NAME_ATTRIBUTE)).Value == websitename) | ||
| 94 | + .Elements(XName.Get(APPLICATION_ELEMENT))?.FirstOrDefault(e => e.Attribute(XName.Get(PATH_ATTRIBUTE)).Value == webapplicationname && e.Attribute(XName.Get(APPLICATIONPOOL_ATTRIBUTE)).Value == applicationpoolname); | ||
| 95 | + if (myapplicationpooladdelement == null) return false; | ||
| 96 | + if (mysiteapplicationelement == null) return false; | ||
| 97 | + | ||
| 98 | + bool configchanged = false; | ||
| 99 | + configchanged = SetOrAddAttribute(AUTOSTART_ATTRIBUTE, myapplicationpooladdelement, "true") || configchanged; | ||
| 100 | + configchanged = SetOrAddAttribute(STARTMODE_ATTRIBUTE, myapplicationpooladdelement, "AlwaysRunning") || configchanged; | ||
| 101 | + configchanged = SetOrAddAttribute(SERVICEAUTOSTARTENABLED_ATTRIBUTE, mysiteapplicationelement, "true") || configchanged; | ||
| 102 | + configchanged = SetOrAddAttribute(SERVICEAUTOSTARTPROVIDER_ATTRIBUTE, mysiteapplicationelement, autostartprovidername) || configchanged; | ||
| 103 | + | ||
| 104 | + XElement myautostartproviderselement = null; | ||
| 105 | + XElement myautostartprovideraddelement = null; | ||
| 106 | + configchanged = SetOrAddElement(SERVICEAUTOSTARTPROVIDERS_ELEMENT, myconfigrootxml, "", out myautostartproviderselement) || configchanged; | ||
| 107 | + configchanged = SetOrAddElementWithSelectorAttribute(ADD_ELEMENT, myautostartproviderselement, NAME_ATTRIBUTE, autostartprovidername, "", out myautostartprovideraddelement) || configchanged; | ||
| 108 | + configchanged = SetOrAddAttribute(TYPE_ATTRIBUTE, myautostartprovideraddelement, $"{serviceAutoStartProviderTypeFullName},{serviceAutoStartProviderAssassemblyName }") || configchanged; | ||
| 109 | + | ||
| 110 | + if (configchanged) | ||
| 111 | + { | ||
| 112 | + //configxml.Save(configfilepath); | ||
| 113 | + System.IO.File.WriteAllText(configfilepath, configxml.ToString()); | ||
| 114 | + } | ||
| 115 | + le.AddDataField("configchanged", configchanged); | ||
| 116 | + //le.AddDataField("myconfigrootxml", myconfigrootxml); | ||
| 117 | + le.AddSuccessResult("SUCCESS"); | ||
| 118 | + return true; | ||
| 119 | + } | ||
| 120 | + catch (Exception ex) { le.AddExceptionResult(ex); le.SetLogLevel(LogLevel.Error); return false; } | ||
| 121 | + finally { le.Write(); } | ||
| 122 | + } | ||
| 123 | + private static bool SetOrAddElement(string elementname, XElement elementcontainer, string elementvalue, out XElement myelement) | ||
| 124 | + { | ||
| 125 | + bool configchanged = false; | ||
| 126 | + myelement = elementcontainer?.Element(XName.Get(elementname)); | ||
| 127 | + if (myelement == null) { myelement = new XElement(XName.Get(elementname), ""); elementcontainer.Add(myelement); configchanged = true; } | ||
| 128 | + if (myelement.Value != elementvalue) { myelement.Value = elementvalue; configchanged = true; } | ||
| 129 | + return configchanged; | ||
| 130 | + } | ||
| 131 | + private static bool SetOrAddElementWithSelectorAttribute(string elementname, XElement elementcontainer, string selectorattributename, string selectorattributevalue, string elementvalue, out XElement myelement) | ||
| 132 | + { | ||
| 133 | + bool configchanged = false; | ||
| 134 | + myelement = elementcontainer?.Elements(XName.Get(elementname))?.FirstOrDefault(e => e.Attribute(XName.Get(selectorattributename)).Value == selectorattributevalue); | ||
| 135 | + if (myelement == null) | ||
| 136 | + { | ||
| 137 | + myelement = new XElement(XName.Get(elementname), ""); | ||
| 138 | + elementcontainer.Add(myelement); | ||
| 139 | + SetOrAddAttribute(selectorattributename, myelement, selectorattributevalue); | ||
| 140 | + configchanged = true; | ||
| 141 | + } | ||
| 142 | + if (myelement.Value != elementvalue) { myelement.Value = elementvalue; configchanged = true; } | ||
| 143 | + return configchanged; | ||
| 144 | + } | ||
| 145 | + private static bool SetOrAddAttribute(string attrname, XElement attrcontainer, string attrvalue) | ||
| 146 | + { | ||
| 147 | + bool configchanged = false; | ||
| 148 | + XAttribute attr = attrcontainer.Attribute(XName.Get(attrname)); | ||
| 149 | + if (attr == null) { attr = new XAttribute(XName.Get(attrname), ""); attrcontainer.Add(attr); configchanged = true; } | ||
| 150 | + if (attr.Value != attrvalue) { attr.Value = attrvalue; configchanged = true; } | ||
| 151 | + return configchanged; | ||
| 152 | + } | ||
| 153 | + private static string NativeSystemPath | ||
| 154 | + { | ||
| 155 | + get | ||
| 156 | + { | ||
| 157 | + if (Environment.Is64BitOperatingSystem) | ||
| 158 | + { | ||
| 159 | + return System.IO.Path.Combine( | ||
| 160 | + Environment.GetFolderPath(Environment.SpecialFolder.Windows), | ||
| 161 | + "Sysnative"); | ||
| 162 | + } | ||
| 163 | + return Environment.GetFolderPath(Environment.SpecialFolder.System); | ||
| 164 | + } | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + private static string GetCurrentApplicationPoolName() | ||
| 168 | + { | ||
| 169 | + string appPoolName = string.Empty; | ||
| 170 | + foreach (Application app in (new ServerManager()).Sites[System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName()].Applications) | ||
| 171 | + { | ||
| 172 | + if (app.Path == HttpRuntime.AppDomainAppVirtualPath) { appPoolName = app.ApplicationPoolName; } | ||
| 173 | + } | ||
| 174 | + return appPoolName; | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + public static readonly HangfireBootstrapper Instance = new HangfireBootstrapper(); | ||
| 178 | + private BackgroundJobServer _backgroundJobServer { get; set; } | ||
| 179 | + private string SqlDBconnectionstring { get; set; } = null; | ||
| 180 | + private readonly object _lockObject = new object(); | ||
| 181 | + private bool _started; | ||
| 182 | + | ||
| 183 | + | ||
| 184 | + public HangfireBootstrapper() | ||
| 185 | + { | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + public void Start() | ||
| 189 | + { | ||
| 190 | + lock (_lockObject) | ||
| 191 | + { | ||
| 192 | + if (_started) return; | ||
| 193 | + _started = true; | ||
| 194 | + | ||
| 195 | + HostingEnvironment.RegisterObject(this); | ||
| 196 | + | ||
| 197 | + GlobalConfiguration.Configuration | ||
| 198 | + .UseSqlServerStorage(SqlDBconnectionstring) | ||
| 199 | + // Specify other options here | ||
| 200 | + ; | ||
| 201 | + | ||
| 202 | + _backgroundJobServer = new BackgroundJobServer(); | ||
| 203 | + } | ||
| 204 | + } | ||
| 205 | + | ||
| 206 | + public void Stop() | ||
| 207 | + { | ||
| 208 | + lock (_lockObject) | ||
| 209 | + { | ||
| 210 | + if (_backgroundJobServer != null) { _backgroundJobServer.Dispose(); } | ||
| 211 | + HostingEnvironment.UnregisterObject(this); | ||
| 212 | + } | ||
| 213 | + } | ||
| 214 | + | ||
| 215 | + void IRegisteredObject.Stop(bool immediate) { Stop(); } | ||
| 216 | + } | ||
| 217 | +} |
Vrh.Web.Reporting/Vrh.Web.Reporting.csproj
| @@ -329,6 +329,7 @@ | @@ -329,6 +329,7 @@ | ||
| 329 | <Compile Include="Global.asax.cs"> | 329 | <Compile Include="Global.asax.cs"> |
| 330 | <DependentUpon>Global.asax</DependentUpon> | 330 | <DependentUpon>Global.asax</DependentUpon> |
| 331 | </Compile> | 331 | </Compile> |
| 332 | + <Compile Include="HangfireBootstrapper.cs" /> | ||
| 332 | <Compile Include="Properties\AssemblyInfo.cs" /> | 333 | <Compile Include="Properties\AssemblyInfo.cs" /> |
| 333 | </ItemGroup> | 334 | </ItemGroup> |
| 334 | <ItemGroup> | 335 | <ItemGroup> |