Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jack 10 posts 91 karma points notactivated
    Jan 20, 2020 @ 10:02
    Jack
    0

    Custom OWIN Startup file in Umbraco Cloud project

    I'm trying to setup a custom OWIN startup class for an Umbraco 8.4.0 project which was setup with Umbraco Cloud as-a-service. The issue is I can't seem to get the project to recognise my custom startup file exists. Here is the code:

      [assembly: OwinStartup(typeof(FischyMusic.Startup))]
    
    namespace FischyMusic
    {
        public class Startup : UmbracoDefaultOwinStartup
        {
            public override void Configuration(IAppBuilder app)
            {
                //ensure the default options are configured
                base.Configuration(app);
    
                // Configure hangfire
                var options = new SqlServerStorageOptions { PrepareSchemaIfNecessary = true };
                const string umbracoConnectionName = Umbraco.Core.Constants.System.UmbracoConnectionName;
                var connectionString = System.Configuration
                    .ConfigurationManager
                    .ConnectionStrings[umbracoConnectionName]
                    .ConnectionString;
    
                GlobalConfiguration.Configuration
                    .UseSqlServerStorage(connectionString, options);
    
                // Give hangfire a URL and start the server                
                app.UseHangfireDashboard("/hangfire");
                app.UseHangfireServer();
            }
        }
    }
    

    And in my web config file I have:

    <add key="owin:appStartup" value="Startup" /> 
    

    I have also tried removing the above key completely from the web config file, as my understanding is that it isn't necessary when I am specifying the OwinStartup with this line in my Startup class:

    [assembly: OwinStartup(typeof(FischyMusic.Startup))]
    

    Neither option has worked.

    Here is the error that I have been getting:

    enter image description here

    And here is my file structure for the project:

    enter image description here

    I have been able to get this working in a seperate project that does not sit within Umbraco Cloud. I am wondering if the issue is that Umbraco Cloud creates a ASP.NET Web Site Project rather than a ASP.NET Web Application Project?

    Any help would be greatly appreciated, I've been stuck on this for a few days now.

    Thanks

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jan 21, 2020 @ 19:33
    Nik
    0

    Hi Jack,

    Is the reason for the custom OWIN startup class only to configure Hangfire? If so, there is a way to do it without using a custom OWIN class :-)

    Nik

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jan 21, 2020 @ 19:36
    Nik
    0

    In the meantime, when I've done this in a v8 site my attribute on the owin class looks a bit different:

    [assembly: OwinStartup("UmbracoIdentityStartup", typeof(UmbracoIdentityOwinStartup))]
    

    Notice the string name as the first parameter, if you add that into yours and call it "Startup" see if that resolves the issue for you.

    Nik

  • Jack 10 posts 91 karma points notactivated
    Jan 22, 2020 @ 10:12
    Jack
    0

    Hi Nik,

    Thanks for your help. Yes, the only reason I want to do this is to configure hangtime. If there is another way to do this that you could point me in the direction of I would really appreciate it!

    As an aside, I have tried the method you used with the named string and that didn't work for me either.

    Jack

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jan 22, 2020 @ 10:17
    Nik
    0

    Hey Jack,

    So you can take advantage of the Composer features of v8, the following is also setting it up to a HangFire job can take advantage of the built in LightInject DI container.

    [RuntimeLevel(MinLevel = RuntimeLevel.Boot)]
    public class HangfireStartup : IComposer
    {
    
        public void Compose(Composition composition)
        {
    
            UmbracoDefaultOwinStartup.MiddlewareConfigured += UmbracoDefaultOwinStartup_MiddlewareConfigured;
        }
    
        private void UmbracoDefaultOwinStartup_MiddlewareConfigured(object sender, OwinMiddlewareConfiguredEventArgs e)
        {
            StartHanfgire(e.AppBuilder);
    
            OnHangfireStarted(new HangfireStartedArgs(e.AppBuilder));
        }
    
        private  void StartHanfgire(IAppBuilder app)
        {
    
            //Configure Hangfire
            var options = new SqlServerStorageOptions { PrepareSchemaIfNecessary = true, QueuePollInterval = TimeSpan.FromSeconds(30) };
            const string defaultUmbracoConnectionName = Umbraco.Core.Constants.System.UmbracoConnectionName;
            var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[defaultUmbracoConnectionName].ConnectionString;
    
            var serviceContainer = new LightInject.ServiceContainer();
            foreach (var t in (Current.Factory.Concrete as LightInject.ServiceContainer).AvailableServices)
                serviceContainer.Register(t);
            serviceContainer.ScopeManagerProvider = new PerLogicalCallContextScopeManagerProvider();
    
    
            GlobalConfiguration.Configuration.UseSqlServerStorage(connectionString, options)
                .UseLightInjectActivator(serviceContainer);
    
            //Give hangfire a URL and start the server
            app.UseHangfireDashboard("/umbraco/hangfire", new DashboardOptions { AppPath = null, Authorization = new[] { new UmbracoHangfireAuthorizationFilter() } });
            app.UseHangfireServer();
    
    
        }
    
        public static event EventHandler<HangfireStartedArgs> HangfireStarted;
    
        internal static void OnHangfireStarted(HangfireStartedArgs args)
        {
            HangfireStarted?.Invoke(null, args);
        }
    
    }
    
  • Jack 10 posts 91 karma points notactivated
    Jan 22, 2020 @ 14:49
    Jack
    0

    Hi Nik,

    Awesome, thanks for the example. So how would I get Umbraco to run the example HangfireStartup class that you provided?

    I have created a HangfireStartup.cs file at the root of my web project, with your code inside it, but if I add a breakpoint in that class it doesn't get hit.

    Apologies if I'm being stupid here, I don't know much about dependency injection I'm still learning about it.

    Thanks

    Edit: I should add, I had to remove this line from your example to get my project to build, so maybe that has something to do with Umbraco not picking it up?

    .UseLightInjectActivator(serviceContainer);
    
Please Sign in or register to post replies

Write your reply to:

Draft