2016-04-13 42 views
0

我陷入了非常简单的问题。使用本地类库作为Web API的资源(ASP.NET Core 1.0)

我想加载本地.NET 4类库(DLL)项目在我的新的ASP.NET Web API项目用作资源(第一次尝试asp.net)

我熟悉的WinForms应用程序,现在正在尝试创建一个ASP.NET Web API,以便能够从PHP/linux服务器访问我的一个助手DLL。我不确定这是否是一种好方法,但我试图阻止在PHP代码中重新实现DLL的逻辑。

现在的问题是我无法将DLL或库类项目添加到Web API项目的资源。

在试图解决我已经尝试了问题:

  1. 的Visual Studio 2015年新建项目>的ASP.NET Web应用程序 - > ASP.NET 1.0的核心 模板 - >网络API
  2. 创建新类库项目到Web API解决方案(或导入现有项目)
  3. 添加项目到Web API项目引用
  4. 添加使用类库命名空间的指令控制器
  5. 尝试建立连带错误:

Error CS0246 The type or namespace name 'ClassLibrary1' could not be found (are you missing a using directive or an assembly reference?) WebApplication1.DNX Core 5.0

对于我来说,似乎它试图获取从的NuGet什么的资源。

我希望有人能够轻易地指出我的错误,所以我可以继续与实际工作一起工作。 (还是我只是错过使用Web API概念?)

project.json

{ 
    "version": "1.0.0-*", 
    "compilationOptions": { 
    "emitEntryPoint": true 
    }, 

    "dependencies": { 
    "Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1", 
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", 
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final", 
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", 
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Configuration.FileProviderExtensions" : "1.0.0-rc1-final", 
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final" 
    }, 

    "commands": { 
    "web": "Microsoft.AspNet.Server.Kestrel", 
    }, 

    "frameworks": { 
    "dnx451": { 
     "dependencies": { 
     "ClassLibraryTest2": "1.0.0-*" 
     } 
    }, 
    "dnxcore50": { } 
    }, 

    "exclude": [ 
    "wwwroot", 
    "node_modules" 
    ], 
    "publishExclude": [ 
    "**.user", 
    "**.vspscc" 
    ] 
} 
+0

你尝试到库添加到文件与您的解决方案>现有的项目?之后,在您的webapi项目中添加对它的引用? – koelkastfilosoof

+0

我也尝试过这种方式,并以同样的方式失败。所以我试图翻译所有其他的错误来源,所以我给web api创建了一个新的类库项目,同样的原因也用它作为问题的一个例子。 –

+0

也许你可以设置一个本地的NuGet feed,并通过nuget feed安装参考。教程在这里:https://docs.nuget.org/create/hosting-your-own-nuget-feeds – koelkastfilosoof

回答

1

无需去除dnxcore50部分。

只需从dnx451中删除"ClassLibraryTest2": "1.0.0-*",然后在全局依赖项列表中添加依赖项。它将应用于dnx451和dnxcore50。

的project.json应该是这样的:

{ 
    "version": "1.0.0-*", 
    "compilationOptions": { 
    "emitEntryPoint": true 
    }, 

    "dependencies": { 
    "Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1", 
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", 
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final", 
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", 
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Configuration.FileProviderExtensions" : "1.0.0-rc1-final", 
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final", 
    "ClassLibraryTest2": "1.0.0-*" 
    }, 

    "commands": { 
    "web": "Microsoft.AspNet.Server.Kestrel", 
    }, 

    "frameworks": { 
    "dnx451": { 
     "dependencies": {    
     } 
    }, 
    "dnxcore50": { } 
    }, 

    "exclude": [ 
    "wwwroot", 
    "node_modules" 
    ], 
    "publishExclude": [ 
    "**.user", 
    "**.vspscc" 
    ] 
} 
+0

感谢您的建议。移动依赖关系给了我其他错误:“错误NU1002 \t项目WebApplicationTest2中的依赖项ClassLibraryTest2 1.0.0不支持框架DNXCore,Version = v5.0。” (类库现在是.NET 4,但我也试过更高的版本,同样的错误) –

+0

我也尝试从框架中删除“dnxcore50”:{}然后构建,但仍然尝试在控制器中使用ClassLibrary时得到错误“错误\t CS0246 \t无法找到类型或命名空间名'ClassLibraryTest2'(您是否缺少使用指令或程序集引用?”) –

+0

现在,如果我删除dnxcore50并将ClassLibrary目标更改为.Net 4.5.1它的工作原理并不重要,如果依赖关系在dnx451或全局依赖关系中。 (但那样我就不能使用.Net 4 ClassLibrary,并且我失去了dnxcore50依赖关系) –