2016-01-04 55 views
2

看來,在我的WebAPI項目,該項目採用OWIN,調用總是返回NULL:var appAssembly = Assembly.GetEntryAssembly(); &OWN - 不要混合?

var appAssembly = Assembly.GetEntryAssembly(); 

我也試過:

var entryAssembly = new StackTrace().GetFrames().Last().GetMethod().Module.Assembly; 

所有返回是 「System.Web程序」。

如何捕獲應用程序名稱,版本?

我想就在啓動時捕捉到這一信息的Web API項目:

/// <summary> 
/// The OWIN startup class. 
/// </summary> 
public class Startup 
{ 
    /// <summary> 
    /// The necessary OWIN configuration method. 
    /// </summary> 
    /// <param name="app">The app being started under OWIN hosting.</param> 
    public void Configuration(IAppBuilder app) 
    { 
     var appAssembly = Assembly.GetEntryAssembly(); 
     Aspect.Logging.LoggingHandler.Initialize(appAssembly, "Hard Coded App Name!"); 
     Log.Information("Starting App..."); 

     // Order is important here. The security wiring must happen first. 
     ConfigureAuthentication(app); 

     // Create web configuration and register with WebAPI. 
     HttpConfiguration config = new HttpConfiguration(); 
     WebApiConfig.Register(config); 

     // Configure documentation. 
     ConfigureDocumentation(config); 

     // Configure support for static files (e.g. index.html). 
     app.UseFileServer(new FileServerOptions 
     { 
      EnableDefaultFiles = true, 
      FileSystem = new PhysicalFileSystem(".") 
     }); 


     // Start the API. 
     app.UseWebApi(config); 
     Log.Information("App started."); 
    } 
+0

你參考,你想從大會信息大會,或者它是同一個程序集?你有沒有考慮過'var appAssembly = typeof(Startup).Assembly;' – vcsjones

+0

這很有效!不,我沒有考慮過使用它。甚至沒有找到我的搜索。謝謝! –

回答

2

用途:

var appAssembly = typeof(Startup).Assembly;