C# 无法加载文件或程序集“CefSharp.dll”或其依赖项之一

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18203077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:32:04  来源:igfitidea点击:

Could not load file or assembly 'CefSharp.dll' or one of its dependencies

c#winformscefsharp

提问by Kien Dang Ngoc

I'm trying to use CefSharp to load my web app into winfoms. I've added 2 dll files: CefSharp.dll and CefSharp.WinForms into references and add 2 dll files icudt.dll and libcef.dll into my project through add existing items.
enter image description here

我正在尝试使用 CefSharp 将我的网络应用程序加载到 winfoms 中。我已将 2 个 dll 文件:CefSharp.dll 和 CefSharp.WinForms 添加到引用中,并通过添加现有项目将 2 个 dll 文件 icudt.dll 和 libcef.dll 添加到我的项目中。
在此处输入图片说明

and this is the code from the form

这是表单中的代码

public WebView web_view;

public Form1()
{
     InitializeComponent();
     web_view = new WebView("http://localhost:8084/wsmill",new CefSharp.BrowserSettings());
     web_view.Dock = DockStyle.Fill;
     toolStripContainer1.ContentPanel.Controls.Add(web_view);
     CefSharp.CEF.Initialize(new Settings());
}

When run the app, I got this error

运行应用程序时,我收到此错误

An unhandled exception of type 'System.IO.FileLoadException' occurred in WindowsFormsApplication1.exe Additional information: Could not load file or assembly 'CefSharp.dll' or one of its dependencies. A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT: 0x8007045A)

WindowsFormsApplication1.exe 中出现类型为“System.IO.FileLoadException”的未处理异常附加信息:无法加载文件或程序集“CefSharp.dll”或其依赖项之一。动态链接库 (DLL) 初始化例程失败。(来自 HRESULT 的异常:0x8007045A)

So anyone who know about this please help me, thanks

所以有知道这方面的朋友请帮帮我,谢谢

回答by Kurubaran

This is a common error which is caused by not all files being present in the output directory (bin\Debug or bin\Release, depending on which configuration you are running in Visual Studio). CefSharp.dllis a .NET-based DLL which is dependent on other .dll files, which in turn depends further on other .dll and other files...

这是一个常见错误,由并非所有文件都存在于输出目录(bin\Debug 或 bin\Release,具体取决于您在 Visual Studio 中运行的配置)中引起。CefSharp.dll是一个基于 .NET 的 DLL,它依赖于其他 .dll 文件,而后者又进一步依赖于其他 .dll 和其他文件......

Here is a listing of the minimum required files:

以下是所需的最少文件列表:

  • libcef.dll(The core Chromium DLL, which basically contains the web browser + the CEF embeddability interface which we depend on)
  • icudt.dll(Unicode DLL for Chromium, must also be present)
  • CefSharp.dll(managed .NET assembly which contains the core CefSharp functionality)
  • CefSharp.WinForms.dllOR CefSharp.WPF.dll(depending on your project type)
  • libcef.dll(核心 Chromium DLL,主要包含 Web 浏览器 + 我们依赖的 CEF 可嵌入性接口)
  • icudt.dll(用于 Chromium 的 Unicode DLL,也必须存在)
  • CefSharp.dll(包含核心 CefSharp 功能的托管 .NET 程序集)
  • CefSharp.WinForms.dllCefSharp.WPF.dll(取决于您的项目类型)

Check this link for more details

查看此链接了解更多详情

回答by c_wiz_kid

You have to set the 'copy to output' properties of the files to 'copy always' or 'copy if newer'. This copies all the files to the output directory, as stated by Coder.

您必须将文件的“复制到输出”属性设置为“始终复制”或“如果更新则复制”。如 Coder 所述,这会将所有文件复制到输出目录。

回答by Per Lundberg

This is a common problem and is therefore mentioned in the FAQ, question number 3.

这是一个常见问题,因此在FAQ 中提到,问题编号 3。

@AcccessDenied is right. The files need to be present in your output folder (bin\Debug or bin\Release). One way to make this is by using a Post-Buildaction, which can set under the project settings in Visual Studio.

@AccessDenied 是对的。这些文件需要存在于您的输出文件夹中(bin\Debug 或 bin\Release)。实现此目的的一种方法是使用Post-Build操作,该操作可以在 Visual Studio 中的项目设置下进行设置。

You can also set up the post-build in the .csprojfile, somewhat like this:

您还可以在.csproj文件中设置后期构建,有点像这样:

<Target Name="AfterBuild">
  <ItemGroup>
    <CefBinaries Include="$(SolutionDir)CEF$(UnmanagedPlatform)\*.*" />
    <LocaleFiles Include="$(SolutionDir)CEF\locales\*.*" />
    <SubProcessFiles Include="$(SolutionDir)$(UnmanagedPlatform)$(Configuration)\CefSharp.BrowserSubprocess.exe" />
  </ItemGroup>
  <Copy SourceFiles="@(CefBinaries)" DestinationFolder="$(TargetDir)" />
  <Copy SourceFiles="@(LocaleFiles)" DestinationFolder="$(TargetDir)locales" />
  <Copy SourceFiles="@(SubProcessFiles)" DestinationFolder="$(TargetDir)" />
</Target>

(This example is taken from the CefSharp.Wpf.Exampleproject in the CefSharp source code, CefSharp3 branch. The exact file locations may vary in your case, especially if using CefSharp1, so adapt it as needed to ensure the files get copied correctly.)

(此示例取自CefSharp 源代码 CefSharp3 分支中的CefSharp.Wpf.Example项目。确切的文件位置可能因您的情况而异,尤其是在使用 CefSharp1 时,因此请根据需要对其进行调整以确保正确复制文件。 )

I don't recommend putting stuff in bin\Debug or bin\Release and adding it to the solution using Copy Always. It feels like a kludge to me.

我不建议将东西放入 bin\Debug 或 bin\Release 并使用Copy Always. 对我来说,这感觉就像一团糟。

回答by Toan Nguyen

You need to put these files

你需要把这些文件

libcef.dll
icudtl.dat
CefSharp.dll
CefSharp.WinForms.dll

into your bin\Debug(or bin\Releasebase on your configuration)

进入您的bin\Debug(或bin\Release根据您的配置)

And please do not forget to install Visual C++ 2012 Redistribution( Visual C++ 2013Redistributable since version 43), if you don't Visual Studio will always display an exception tell not found CefSharp.dll although you already have it!

并且请不要忘记安装Visual C++ 2012 Redistribution(Visual C++ 2013Redistributable since version 43),如果你不安装,Visual Studio 将始终显示异常告诉未找到 CefSharp.dll 尽管你已经有了它!

Hope this help.

希望这有帮助。

回答by uTILLIty

I have just had problems with this as well and I did the following:

我刚刚也遇到了这个问题,我做了以下事情:

CefSharp.DependencyCheckerdid not report anything missing but as soon as I called new CefSharp.Wpf.ChromiumWebBrowser()the exception occured.

CefSharp.DependencyChecker没有报告任何遗漏,但是一旦我打电话给新CefSharp.Wpf.ChromiumWebBrowser()的异常发生。

I checked all ms.net CEF dlls using ILSpy and found that some of these DLLs were also located in the GAC. As soon as I removed them from GAC all worked ok!

我使用 ILSpy 检查了所有 ms.net CEF dll,发现其中一些 DLL 也位于 GAC 中。一旦我将它们从 GAC 中删除,一切正常!

回答by nikitaso

Setting "msvcp120.dll" and "msvcr120.dll" to "Copy Always" helped me.

将“msvcp120.dll”和“msvcr120.dll”设置为“始终复制”对我有帮助。

回答by ZerosAndOnes

In my case, I was following the steps outlined in CefSharp - Our Code World.

就我而言,我遵循CefSharp - Our Code World 中概述的步骤。

Instead of following step A and adding CefSharpAnyCpuSupportin csprojand probingin App.config, simply setting the Platform Targetto x86in step B did the trick.

而不是遵循步骤 A 并添加CefSharpAnyCpuSupportincsprojprobingin App.config,只需在步骤 B 中设置Platform Targettox86就可以了。

回答by CodingYourLife

The recommended way seems to be to use the NuGet package. Even then you need to make some non-intuitive changes but they are documented. When installing the package freshly a readme.txt file openswith most common issues.

推荐的方法似乎是使用 NuGet 包。即使这样,您也需要进行一些非直观的更改,但它们已记录在案。新安装软件包时,会打开一个readme.txt 文件,其中包含最常见的问题。

In my case I was missing (x64 also available)

就我而言,我失踪了(x64 也可用)

<probing privatePath="x86" />

My App.config looks like this (with CommonServiceLocator in it, ignore that part if you don't have it)

我的 App.config 看起来像这样(其中包含 CommonServiceLocator,如果没有,请忽略该部分)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="x86" />

      <dependentAssembly>
        <assemblyIdentity name="CommonServiceLocator" publicKeyToken="489b6accfaf20ef0" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.3.0" newVersion="2.0.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

The error occurs because there is really no Cefsharp DLL in the bin folder when you set the project to AnyCPU. But in the x86 and x64 folder bellow where you are probing when adding this line the DLL exists. A temporary fix would be to copy the contents from /bin/x86 to /bin.

出现这个错误是因为你把项目设置为AnyCPU的时候bin文件夹里确实没有Cefsharp DLL。但是在 x86 和 x64 文件夹中,您在添加此行时正在探测的位置存在 DLL。临时解决方法是将内容从 /bin/x86 复制到 /bin。

If AnyCpu is desired CefSharpAnyCpuSupport needs to be added to csproj file. The project Flag for Prefer 32bit needs to be set.

如果需要 AnyCpu,需要将 CefSharpAnyCpuSupport 添加到 csproj 文件中。需要设置 Prefer 32bit 的项目标志。

回答by Victor Thomas Wilcox Jr.

I had offline CEF working perfectly, then it suddenly stopped working after upgrading solution to core 3.1. Still not sure why, but copying in resources, VC++ runtimes, etc. and other usual solutions didn't work. I added the following code for diagnostic purposes in my startup code and suddenly, CEF works again. Assemblies were in same path, but it seems loading them clued in the assembly binder. If anyone is similarly frustrated, might try similar approach with whatever variant of CEF assemblies you are working with. Maybe some .net gurus can explain exactly why this helps.

我的离线 CEF 工作正常,然后在将解决方案升级到核心 3.1 后突然停止工作。仍然不知道为什么,但是复制资源、VC++ 运行时等和其他常用的解决方案不起作用。我在启动代码中添加了以下用于诊断的代码,突然间,CEF 再次运行。程序集在相同的路径中,但似乎在程序集活页夹中加载它们。如果有人同样感到沮丧,可以尝试使用您正在使用的任何 CEF 程序集变体的类似方法。也许一些 .net 专家可以准确解释为什么这会有所帮助。

  try
  {
    Assembly asm;
    string path;

    path = Path.Combine(
      Directory.GetCurrentDirectory(),
      "CefSharp.Core.dll");
    asm = Assembly.LoadFrom(path);
    path = Path.Combine(
      Directory.GetCurrentDirectory(),
      "CefSharp.OffScreen.dll");
    asm = Assembly.LoadFrom(path);
  }
  catch (Exception exception)
  {
    System.Diagnostics.Trace.WriteLine(exception.Message + " @ " + exception.StackTrace);
  }

*** Updates below after @amaitland offered the pertinent information, thanks! As per https://github.com/cefsharp/CefSharp.MinimalExample#net-core-supportI updated my project to include:

***@amaitland 提供相关信息后更新如下,谢谢!根据https://github.com/cefsharp/CefSharp.MinimalExample#net-core-support我更新了我的项目以包括:

<ItemGroup>
  <Reference Update="CefSharp">
    <Private>true</Private>
  </Reference>
  <Reference Update="CefSharp.Core">
    <Private>true</Private>
  </Reference>
  <Reference Update="CefSharp.OffScreen">
    <Private>true</Private>
  </Reference>
</ItemGroup>

and it no longer needs the previous code block to work out of the box. If anyone runs into confusion like I did, adding privateassets all to the PACKAGE reference is not the same thing. Rather than typing in those lines, you can also expand 'Assemblies' in the solution explorer for your project, and right click on the CefSharp assemblies there and change CopyLocal to Yes. Thanks again, @amaitland

它不再需要之前的代码块即可开箱即用。如果有人像我一样遇到困惑,将私有资产全部添加到 PACKAGE 引用并不是一回事。除了输入这些行,您还可以在项目的解决方案资源管理器中展开“程序集”,然后右键单击那里的 CefSharp 程序集并将 CopyLocal 更改为是。再次感谢@amaitland