0

I have a file that needs to be read from. The problem is that if I have the FileStream open and read it everytime a request is made, the application becomes very very slow due to the size of the file (A few MB), and the fact that it runs on every request. Is there a way to read it from the beginning, then inside my Controller just refer to it?

This is my initialization code:

namespace WebsiteServer
{
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{*url}");
        });
    }
}
}

Thank you!

  • 2
    Startup code should go in the [`Application_Start`](http://www.c-sharpcorner.com/UploadFile/1f3f2a/understating-mvc-application-start-and-different-components/) handler in `global.asax.cs`, where it will run only once. You can cache the file in a static variable or in [Application State](https://msdn.microsoft.com/en-us/library/94xkskdf.aspx). – John Wu Aug 23 '17 at 16:29
  • Read this, look useful to me https://stackoverflow.com/a/36659356/713789 – Anirudha Gupta Aug 23 '17 at 16:39

0 Answers0