Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can only serve one file with OWIN UseStaticFiles

Tags:

.net

owin

I'm using OWIN to be able to serve static content over http from within a Windows Service. (To embed a Web Administration Tool for the Windows Service).

I experience some strange behaviour:

  • When I run the service, I can only access one file that is in the "web" folder, every consecutive call results in the browser telling that the page is not available (ERR_CONNECTION_RESET).
  • The embedded WebApi stays accessible.
  • When I restart the service with the same address and port, the files stay invisible.
  • When I restart the service on another port, I can access a file once...

By the way, this files in this "web" folder are set to "Copy always" for the "Copy to Output Directory" property.

Anyone knows what is going wrong?

See here my StartUp configuration class

public class WebStartUp
{
    public void Configuration(IAppBuilder app)
    {
        string staticFilesDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "web");
        app.UseStaticFiles(staticFilesDir);

        HttpConfiguration config = new HttpConfiguration();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        app.UseWebApi(config);
    }
}

See here my Windows Service that hosts it...

    public partial class MyService : ServiceBase
    {
        private IDisposable webApp;
        private const string ServiceAddress = "http://localhost:2345";

        public MyService()
        {

        }

        protected override void OnStart(string[] args)
        {
            InternalStart();
        }

        internal void InternalStart()
        {
            webApp = WebApp.Start<WebStartUp>(url: ServiceAddress);
        }

        protected override void OnStop()
        {
        }

        public static void Main()
        {
#if DEBUG
            var service = new MyService();
            Console.WriteLine("starting");
            service.InternalStart();
            Console.ReadLine();
#else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] {
                new RaceManagerService();
            }
            ServiceBase.Run(ServicesToRun);
#endif

        }
    }
like image 457
Stivni Avatar asked Oct 22 '22 03:10

Stivni


2 Answers

I think I know what is going wrong: The 3rd party frameworks that you are using are either in alpha or beta. You should not rely on them based on you experience with them and their general state.

I created an almost identical setup (link to project files) and saw the exact same results. The used libraries are just not up to the task yet.

Edit:

I could get it to run much more relaibly with version 0.23.20815.0 of the Microsoft.Owin.StaticFiles library. I built it myself from the latest Katana sources. You can find my latest code at my GitHub page.

like image 180
meilke Avatar answered Dec 09 '22 09:12

meilke


I use a similar configuration: Windows service as OWIN host with WebApi and StaticFiles. With this I did never see your problem. It's working fine.

I am using StaticFiles version 0.24.0-pre-20624-416 from the Katana Nightly Builds. Maybe this will solve also your problem. Another difference is, that I configure StaticFiles only with a relative path, not an absolute one as you do. It shouldn't make any difference, but who knows?

BTW: I blogged about StaticFiles 2 months ago: http://ritzlgrmft.blogspot.de/2013/06/owin-with-static-files-exception.html

like image 24
Markus Wagner Avatar answered Dec 09 '22 10:12

Markus Wagner