如何加载自定义DLL依赖的Nuget程序包? [英] How do I load Nuget packages that a custom DLL depends on?

查看:222
本文介绍了如何加载自定义DLL依赖的Nuget程序包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过门户网站开发功能应用程序(而不是本地开发).我有一个依赖于1个nuget包的自定义DLL:Entity Framework 6.1.3

I'm developing a Function App via the Portal (not local development). I have a custom DLL that depends on 1 nuget package: Entity Framework 6.1.3

我已将我的DLL上传到"../bin",并且当我引用我的DbContext对象时,我的代码已成功编译.到目前为止,一切都很好.

I have uploaded my DLL to "../bin" and my code compiles successfully when I reference my DbContext object. So far, so good.

我还有一个 Project.json 文件,保存时会看到它获取nuget包.到目前为止,一切都很好.

I also have a Project.json file and I see it acquiring the nuget packages when I save. So far, so good.

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "EntityFramework": "6.1.3"
      }
    }
  }
}

我的 Run.csx 代码成功编译,看起来像这样:

My Run.csx code compiles successfully and looks like this:

#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());    
}

但是,该代码实际上并未运行(我什至没有看到我的代码:已启动"日志项).我收到的错误是:

However, the code doesn't actually run (I don't even see the "My code: Started" log item). The error I receive is:

2017-02-27T06:37:28.731执行函数时发生异常: Functions.TimerTriggerCSharp1. mscorlib:已引发异常 调用的目标. f-TimerTriggerCSharp1 __- 205940111:可以 无法加载文件或程序集'EntityFramework,Version = 6.0.0.0, 文化=中性,PublicKeyToken = b77a5c561934e089'或其中之一 依赖关系.系统找不到指定的文件.

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.您只需重新创建它:

My custom DLL is the simplest possible EF-referencing DLL I can possibly make. All you need to recreate it is this:

自定义DLLpackages.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包?

What am I doing wrong that is keeping my custom DLL from being able to utilize the downloaded EntityFramework nuget package?

只是为了验证我的nuget引用是否在正常工作,如果我注释掉大部分 Run.csx 代码并将其替换为此行,则所有代码都会正确执行并记录您的期望:/p>

Just to verify that my nuget references are actually working, if I comment out most of my Run.csx code and replace it with this line, all executes correctly and logs what you would expect:

log.Info($"My code: {typeof(System.Data.Entity.DbContext).ToString()}");


正如有人建议的那样,我尝试将我的 Run.csx 引用更改为这样,并且它不会更改我得到的运行时错误(它会编译,因此路径是正确的):


As somebody suggested, I have tried changing my Run.csx references to look like this and it doesn't change the runtime error I get (it does compile so the path is correct):

#r "../../../data/Functions/packages/nuget/entityframework/6.1.3/lib/net45/EntityFramework.dll"
#r "../bin/My.dll"


我也可以将我的 Run.csx 文件更改为包含以下代码,并且该文件可以成功执行:


I can also change my Run.csx file to contain this code and it does successfully execute:

using System;

public static void Run(TimerInfo myTimer, TraceWriter log)
{
    log.Info(typeof(MyContext).ToString());    
}

public class MyContext : System.Data.Entity.DbContext
{
}

推荐答案

为了使用引用的程序包中的程序集,可以将自定义依赖项部署为私有程序集:

In order to consume the assemblies coming from your referenced packages, you can deploy your custom dependency as a private assembly:

  • 将程序集部署到功能文件夹(例如wwwroot\myfunction\bin)中的bin文件夹中
  • 仅通过文件名(例如Library.dll)引用不带相对路径的程序集
  • Deploy the assembly into a bin folder, in your function folder (e.g. wwwroot\myfunction\bin)
  • Reference the assembly without the relative path, by file name only (e.g. Library.dll)

如果您希望使用共享程序集,将其部署到一个公共位置并按上面的引用进行引用,则需要部署具有其依赖项的程序集(本质上是项目构建的输出).

If you wish to use shared assemblies, deployed to a common location and referenced as you have above, you'd need to deploy the assembly with its dependencies (essentially the output from your project build).

如果您想利用共享模型,我建议的另一个选择是将依赖项部署为NuGet程序包(您可以将其部署到托管在某个位置的自定义源或作为文件),然后该程序包将指定其程序包依赖性,它们将全部自动解决.

Another option that I would recommend if you want to take advantage of the shared model is to deploy your dependency as a NuGet package (which you can deploy to either a custom source hosted somewhere or as a file), that package would then specify its package dependencies and they would all be resolved automatically.

这篇关于如何加载自定义DLL依赖的Nuget程序包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆