CS0234错误:在Microsoft命名空间中不存在Owin类型或命名空间名称

3
我正在使用VS 2015 Community并尝试创建一个WebServer Console,使用这些“简单”的教程:
OWIN和Katana part 1: the basics
ASP.NET Fundamentals
这两个教程看起来很容易,但是当我尝试编译它们时,我几乎在同一个地方卡住了。这两个教程都引用了DNX 4.5.1(Microsoft.Owin.Host.HttpListener(3.0.1)和Microsoft.Owin.Hosting(3.0.1))和DNX Core 5.0。以下是第一个链接的代码(与第二个链接几乎相同(包括错误),因此我不会包含该代码):
using Microsoft.Owin.Hosting;
using Owin;
using System;

namespace KatanaBasics
{
  public class Program
    {
        public static void Main(string[] args)
        {
          string uri = "http://localhost:7990";

          using (WebApp.Start<startup>(uri))
          {
            Console.WriteLine("Web server on {0} starting.", uri);
            Console.ReadKey();
            Console.WriteLine("Web server on {0} stopping.", uri);
          }
        }
    }

    public class startup
  {
      public void Configuration(IAppBuilder appBuilder)
      {
        appBuilder.Run(owinContext =>
        {
          owinContext.Response.WriteAsync("Hello from OWIN web server");
        });
      }
  }
}

我在“owinContext =>”处开始看到红色的波浪线,一直延续到该方法的结尾。 当我尝试编译时,这里是我收到的错误信息:
Severity    Code    Description Project File    Line    Suppression State
Error   CS0234  The type or namespace name 'Owin' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)  KatanaBasics.DNX Core 5.0

Severity    Code    Description Project File    Line    Suppression State
Error   CS0246  The type or namespace name 'Owin' could not be found (are you missing a using directive or an assembly reference?)  KatanaBasics.DNX Core 5.0

Severity    Code    Description Project File    Line    Suppression State
Error   CS0246  The type or namespace name 'IAppBuilder' could not be found (are you missing a using directive or an assembly reference?)   KatanaBasics.DNX Core 5.0

Severity    Code    Description Project File    Line    Suppression State
Error   CS0103  The name 'WebApp' does not exist in the current context KatanaBasics.DNX Core 5.0   

Severity    Code    Description Project File    Line    Suppression State
Error   CS1643  Not all code paths return a value in lambda expression of type 'Func<IOwinContext, Task>'   KatanaBasics.DNX 4.5.1

Owin 不支持 DNX Core 5.0,但支持 DNX 4.5.1。我原以为可以将其移动或复制到 DNX 4.5.1,但似乎 DNX Core 5.0 是默认的选择。
我找到了类似问题的解决方法,但是并不理解所有的解释。例如,我看到一篇帖子说要更改目标,但我甚至找不到他们所描述的“修复”内容在哪里或是什么。所以,似乎我需要一个早期版本的 DNX Core 5.0?如果是这样,我该如何引用早期版本呢?非常感谢!

你是否也手动在项目->引用下添加了对程序集的引用? - MethodMan
我从项目-->管理NuGet包中添加了Microsoft.Owin.Host.HttpListener和Microsoft.Owin.Hosting。在项目-->引用中,我应该添加哪个引用? - Tilting Code
你需要确保将该包添加到.config文件中,同时还有一种解决方法...如果你可以在项目中创建一个Dependencies文件夹...手动将dll复制到该文件夹中,然后右键单击项目中的文件夹并选择AddExisting Item..然后一旦你添加了它..右键单击引用,添加引用..然后导航到dependencies文件夹..选择它..然后转到引用中的dll的CopyLocal,并将该值设置为true - MethodMan
谢谢MethodMan。不太确定我是否理解你的意思,但这是我尝试过的方法:首先,没有.config文件(这是因为它是一个WebServer控制台项目吗?)。有一个AssemblyInfo.cs文件,但里面没有太多内容。我在项目文件夹下创建了Dependencies文件夹,找到了以下DLL文件并将它们复制到Dependencies文件夹中:\1.0.0\lib\net40\Owin.dll、\3.0.1\lib\net45\Microsoft.Owin.Hosting.dll、\3.0.1\lib\net45\Microsoft.Owin.Host.HttpListener.dll、\3.0.1\lib\net45\Microsoft.Owin.dll........ 我需要添加另一个评论来完成这个,因为我用了太多字符... - Tilting Code
在VS中,它们已经被添加到依赖文件夹中,所以我不需要手动添加它们。然后我去了项目-->引用,将每个引用都添加了进去,它们替换了当前的引用。每个引用的新版本号现在是1.0.0,并且都位于DNX 4.5.1文件夹中。当我尝试重新构建时,没有任何变化,我得到了相同的错误信息。 - Tilting Code
2个回答

2
我遇到了相同的问题。为了测试,我按下ctrl-F5键,并在它询问我是否要忽略错误后选择“是”……它奇迹般地正常运行了。
在我的工作电脑上,我将2013升级到VS2015 Update 3后出现了这些错误。
我在家里用全新安装的VS2015 Update 3执行了相同的步骤,但没有出现这些(可能是虚假的)错误。
这不是一个理想的答案,但我希望能帮到你。

2
在 Visual Studio 中,转到“工具”->“Nuget 包管理器”->“程序包管理器控制台”,然后输入:Update-Package。

在从另一台机器克隆了一个旧仓库之后,这对我起作用了。 - ThienLD

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接