【问题标题】:Telling OwinHost on command line how to load a OwinSetup class with cwd outside project在命令行上告诉 OwinHost 如何在项目外部使用 cwd 加载 OwinSetup 类
【发布时间】:2017-07-27 12:00:23
【问题描述】:

如果您的 cwd 在您的项目根目录中,OwinHost 非常棒,即

c:/code/myprojectroot> ../location/of/owinhost.exe

当您从项目根目录工作时,只要您的项目中某处有 [assembly: OwinStartup(typeof(MyOwinStartupClass))],OwinHost 似乎非常乐意自动神奇地找到您的类。

但是,如果您尝试将其作为构建脚本的一部分或 cwd 不是项目根目录的其他内容来执行,那么 OwinHost 对您来说就不那么好了,命令行参数意味着您可以告诉它您的项目所在的位置:

c:/not/project/root> location/of/owinhost.exe -d "path/to/project/root"

但是这样做,它总是会告诉你一些讨厌的谎言,比如:

Starting with the default port: 5000 (http://localhost:5000/) Error: System.EntryPointNotFoundException The following errors occurred while attempting to load the app. - No assembly found containing an OwinStartupAttribute.

所以我尝试了以下方法:

  • c:/not/project/root> location/of/owinhost.exe -d "path/to/project/root" MyOwinStartupClass
  • c:/not/project/root> location/of/owinhost.exe -d "path/to/project/root" MyNamespace.MyOwinStartupClass
  • c:/not/project/root> location/of/owinhost.exe -d "path/to/project/root" MyNamespace.MyOwinStartupClass,MyAssemblyName
  • c:/not/project/root> location/of/owinhost.exe -d "path/to/project/root/bin/Debug" MyOwinStartupClass
  • c:/not/project/root> location/of/owinhost.exe -d "path/to/project/root/bin/Debug" Namespace.MyOwinStartupClass
  • c:/not/project/root> location/of/owinhost.exe "path/to/project/root"
  • c:/not/project/root> location/of/owinhost.exe "path/to/project/root/bin/Debug/my-assembly-with-owin-startup-in.dll"

没有任何作用,就像 OwinHost 工作的唯一方式是,如果您以 CWD 作为项目根目录运行它,那么任何人都可以阐明我哪里出错了吗?正如我给它的目录 (-d specifies the target directory of the application) 一样,我已经尝试手动给它 DLL,给它上面的任何排列。

所以我想不出任何 Owin-Whisperers 可以告诉我正确的咒语来让它做我想做的事?

【问题讨论】:

  • 你试过c:/not/project/root> location/of/owinhost.exe -d "path/to/project/root/bin/Debug" (即不指定启动类)吗?
  • 是的,我也试过了,我已经尝试了上面的所有合理排列,有和没有启动类名等。

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


【解决方案1】:

查看OwinHostsource,有如下一行:

ResolveAssembliesFromDirectory(
            Path.Combine(Directory.GetCurrentDirectory(), "bin"));

请注意它使用 Directory.GetCurrentDirectory()

这会调用具有AssemblyResolve 事件的方法

    public static void ResolveAssembliesFromDirectory(string directory)
    {
        var cache = new Dictionary<string, Assembly>();
        AppDomain.CurrentDomain.AssemblyResolve +=
            (a, b) =>
            {

所以,它似乎总是从当前目录开始搜索。这可能是您的问题的原因。

修复:提交补丁修复 OwinHost ! (或)写你自己的Console app to Host

【讨论】:

猜你喜欢
  • 2011-06-02
  • 2019-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-01
  • 2013-06-04
  • 1970-01-01
相关资源
最近更新 更多