新 (SDK) csproj 格式的 OWIN 应用程序

OWIN app on new (SDK) csproj format

在将 OWIN 应用程序转换为新的 csproj 格式时,我遇到了一些问题。第一个问题与更改后的输出目录有关(因为它现在在路径中包含了运行时)。这引发了以下错误:

Could not load file or assembly 'Microsoft.Owin.Host.SystemWeb' or one of its dependencies. The system cannot find the file specified.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Owin.Host.SystemWeb' or one of its dependencies. The system cannot find the file specified.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

我可以通过在 Web.config:

中设置路径来解决这个问题
<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <probing privatePath="bin/net461" />
  </assemblyBinding>
</runtime>

之后,我现在面临这个问题:

The following errors occurred while attempting to load the app.
- No assembly found containing an OwinStartupAttribute.
- The given type or method 'ClientWebAPI.Startup' was not found. Try specifying the Assembly.
To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config.
To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.EntryPointNotFoundException: The following errors occurred while attempting to load the app.
- No assembly found containing an OwinStartupAttribute.
- The given type or method 'ClientWebAPI.Startup' was not found. Try specifying the Assembly.
To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config.
To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

我已经使用程序集属性声明了入口点:

[assembly: OwinStartup(typeof(API.Startup))]
namespace API
{
    public class Startup
    { /*...*/ }
}

"Sdk" 风格的 csproj,有时称为 VS2017 项目格式,尚不支持 ASP.NET 4 个项目。请参阅以下问题:

这里有两层问题:(1) MSBuild 和 (2) Visual Studio。如果您充分了解目标和导入的工作方式,则可以解决方法 (1) 并使 Sdk 样式的 csproj 适用于 Owin 项目。

比较难的部分是(2) - Visual Studio。当切换到 Sdk 风格的项目时,在幕后,VS 正在切换到项目系统的全新实现,即在 https://github.com/dotnet/project-system 中开源的项目系统。该项目系统还没有完全支持ASP.NET 4个项目。

您可以使用 Microsoft.Owin.SelfHost 而不是 Microsoft.Owin.Host.SystemWeb 解决方法 (2)。要使用 Microsoft.Owin.Host.SystemWeb 工作,VS 需要知道如何为 IIS Express 生成 web.config 和 applicationhost.config 文件,如何启动 IIS Express 站点,以及在启动后附加到进程。其中大部分没有移植到新的项目系统,我不确定是否会移植。新的项目系统旨在与 ASP.NET Core 一起使用,后者使用非常不同的方法来启动和配置 IIS Express。