如何加载自定义 DLL 所依赖的 Nuget 包?
How do I load Nuget packages that a custom DLL depends on?
我正在通过 Portal 开发一个功能应用程序(不是本地开发)。我有一个依赖于 1 个 nuget 包的自定义 DLL:Entity Framework 6.1.3
我已经将我的 DLL 上传到“../bin”,当我引用我的 DbContext
对象时,我的代码编译成功。到目前为止,还不错。
我还有一个 Project.json 文件,我看到它在我保存时获取了 nuget 包。到目前为止,还不错。
{
"frameworks": {
"net46":{
"dependencies": {
"EntityFramework": "6.1.3"
}
}
}
}
我的 Run.csx 代码编译成功,如下所示:
#r "../bin/Library.dll"
using System;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
log.Info("My code: Started");
log.Info(typeof(Library.MyContext).ToString());
}
但是,该代码实际上并没有 运行(我什至没有看到 "My code: Started" 日志项)。我收到的错误是:
2017-02-27T06:37:28.731 Exception while executing function:
Functions.TimerTriggerCSharp1. mscorlib: Exception has been thrown by
the target of an invocation. f-TimerTriggerCSharp1__-205940111: Could
not load file or assembly 'EntityFramework, Version=6.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its
dependencies. The system cannot find the file specified.
我的自定义 DLL 是我能制作的最简单的 EF 引用 DLL。您只需要重新创建它:
自定义 DLL packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net46" />
</packages>
自定义 DLL Class1.cs
using System.Data.Entity;
namespace Library
{
public class MyContext : DbContext
{
}
}
我做错了什么让我的自定义 DLL 无法使用下载的 EntityFramework nuget 包?
只是为了验证我的 nuget 引用是否确实有效,如果我注释掉我的大部分 Run.csx 代码并将其替换为这一行,所有代码都会正确执行并且记录您期望的内容:
log.Info($"My code: {typeof(System.Data.Entity.DbContext).ToString()}");
正如有人建议的那样,我已经尝试将我的 Run.csx 引用更改为如下所示,但它并没有改变我得到的 运行time 错误(它编译所以路径是正确的):
#r "../../../data/Functions/packages/nuget/entityframework/6.1.3/lib/net45/EntityFramework.dll"
#r "../bin/My.dll"
我也可以更改我的 Run.csx 文件以包含此代码并且它成功执行:
using System;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
log.Info(typeof(MyContext).ToString());
}
public class MyContext : System.Data.Entity.DbContext
{
}
貌似是配置问题,需要在工程文件中定义引用才能复制到输出。
在解决方案资源管理器中,展开项目、引用、右键单击 EntityFramework、属性、设置复制本地 = true。
有时候默认值是false,这样就不会出现在项目的输出文件夹中。
希望对您有所帮助。
为了使用来自引用包的程序集,您可以将自定义依赖项部署为 私有程序集:
- 将程序集部署到函数文件夹中的
bin
文件夹中(例如 wwwroot\myfunction\bin
)
- 仅按文件名引用没有相对路径的程序集(例如
Library.dll
)
如果您希望使用共享程序集,部署到公共位置并像上面那样引用,您需要部署程序集及其依赖项(基本上是项目构建的输出)。
如果您想利用共享模型,我推荐的另一个选项是将您的依赖项部署为 NuGet 包(您可以将其部署到托管在某处的自定义源或作为文件),该包然后会指定它的包依赖项,它们都会自动解析。
我正在通过 Portal 开发一个功能应用程序(不是本地开发)。我有一个依赖于 1 个 nuget 包的自定义 DLL:Entity Framework 6.1.3
我已经将我的 DLL 上传到“../bin”,当我引用我的 DbContext
对象时,我的代码编译成功。到目前为止,还不错。
我还有一个 Project.json 文件,我看到它在我保存时获取了 nuget 包。到目前为止,还不错。
{
"frameworks": {
"net46":{
"dependencies": {
"EntityFramework": "6.1.3"
}
}
}
}
我的 Run.csx 代码编译成功,如下所示:
#r "../bin/Library.dll"
using System;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
log.Info("My code: Started");
log.Info(typeof(Library.MyContext).ToString());
}
但是,该代码实际上并没有 运行(我什至没有看到 "My code: Started" 日志项)。我收到的错误是:
2017-02-27T06:37:28.731 Exception while executing function: Functions.TimerTriggerCSharp1. mscorlib: Exception has been thrown by the target of an invocation. f-TimerTriggerCSharp1__-205940111: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
我的自定义 DLL 是我能制作的最简单的 EF 引用 DLL。您只需要重新创建它:
自定义 DLL packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net46" />
</packages>
自定义 DLL Class1.cs
using System.Data.Entity;
namespace Library
{
public class MyContext : DbContext
{
}
}
我做错了什么让我的自定义 DLL 无法使用下载的 EntityFramework nuget 包?
只是为了验证我的 nuget 引用是否确实有效,如果我注释掉我的大部分 Run.csx 代码并将其替换为这一行,所有代码都会正确执行并且记录您期望的内容:
log.Info($"My code: {typeof(System.Data.Entity.DbContext).ToString()}");
正如有人建议的那样,我已经尝试将我的 Run.csx 引用更改为如下所示,但它并没有改变我得到的 运行time 错误(它编译所以路径是正确的):
#r "../../../data/Functions/packages/nuget/entityframework/6.1.3/lib/net45/EntityFramework.dll"
#r "../bin/My.dll"
我也可以更改我的 Run.csx 文件以包含此代码并且它成功执行:
using System;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
log.Info(typeof(MyContext).ToString());
}
public class MyContext : System.Data.Entity.DbContext
{
}
貌似是配置问题,需要在工程文件中定义引用才能复制到输出。
在解决方案资源管理器中,展开项目、引用、右键单击 EntityFramework、属性、设置复制本地 = true。
有时候默认值是false,这样就不会出现在项目的输出文件夹中。 希望对您有所帮助。
为了使用来自引用包的程序集,您可以将自定义依赖项部署为 私有程序集:
- 将程序集部署到函数文件夹中的
bin
文件夹中(例如wwwroot\myfunction\bin
) - 仅按文件名引用没有相对路径的程序集(例如
Library.dll
)
如果您希望使用共享程序集,部署到公共位置并像上面那样引用,您需要部署程序集及其依赖项(基本上是项目构建的输出)。
如果您想利用共享模型,我推荐的另一个选项是将您的依赖项部署为 NuGet 包(您可以将其部署到托管在某处的自定义源或作为文件),该包然后会指定它的包依赖项,它们都会自动解析。