var appAssembly = Assembly.GetEntryAssembly(); &安培;自己 - 不要混?

时间:2016-01-04 21:11:54

标签: c#-4.0 asp.net-web-api2 owin

在我的WebAPI项目中,似乎使用OWIN,调用始终返回NULL:

var appAssembly = Assembly.GetEntryAssembly();

我也尝试过:

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

所有返回的是" System.Web"。

如何捕获应用程序名称,版本???

我正试图在Startup上为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.");
    }

1 个答案:

答案 0 :(得分:3)

使用:

var appAssembly = typeof(Startup).Assembly;