title: Get path of current middleware file in C#
body: <p>I am using NodeServices to run a JavaScript script within a middleware. However, the project that is using this, and where all of the JS files are stored, is separate from the startup project. Solution looks like this:</p>
  
  <pre><code>SolutionRoot/
    ├─Project/
    │ ├─NodeServices/
    | │ └─NodeService1/
    | │   └─adapter.js
    | ├─Middlewares/
    | | └─MyMiddleware.cs
    | └─Services/
    |   └─MyService/
    |    ├─MyService.cs
    |    └─IMyService.cs
    └─StartupProject/
      └─Startup.cs
  </code></pre>
  
  <p>For context, <code>Project</code> is an in-house CMS and <code>Startup</code> is a demo web project. <code>Project</code> is distributed through Nuget.</p>
  
  <p>The issue I'm having is that I am registering NodeServices in the middleware file using an extension method on <code>IServiceCollection</code>. But regardless of what method I use, the path always comes out to be relative to <code>Startup/Startup.cs</code>. How can I get the path to the <code>MyMiddleware</code> file itself and pass that as an option to <code>NodeServices</code>? Or, more specifically, what is the best way to get an absolute path to the <code>NodeServices</code> folder, regardless of how this project is referenced (ie even if it was through a Nuget Package)?</p>
  
  <p><strong>MyMiddleware.cs</strong></p>
  
  <pre><code>  public class MyMiddleware : IMiddleware {
      // ...
    }
  
    public static class MyMiddlewareExtensions
    {
        public static IApplicationBuilder UseMyMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware&lt;MyMiddleware&gt;();
        }
  
        public static IServiceCollection AddMyMiddleware(this IServiceCollection services)
        {
            var x = Directory.GetCurrentDirectory(); // This returns the path to the startup, where it was called
            var path = "../Project/NodeServices"; // this works but not viable
            services.AddNodeServices(options =&gt; {
                options.ProjectPath = path;
            });
  
            services.AddTransient&lt;IMyService, MyService&gt;();
            services.AddTransient&lt;MyMiddleware&gt;();
  
            return services;
        }
    }
  }
  </code></pre>
  
  <p><strong>Startup.cs</strong>:</p>
  
  <pre><code>namespace StartupProject
  {
      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
  
          public IConfiguration Configuration { get; }
  
          public void ConfigureServices(IServiceCollection services)
          {
              // Other Things
              services.AddMyMiddleware();
              // Other Things
          }
  
          public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              // Other Things
              app.UseMyMiddleware();
              // Other Things
          }
      }
  }
  </code></pre>