Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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)

So anyone who know about this please help me, thanks

like image 836
Kien Dang Ngoc Avatar asked Aug 13 '13 07:08

Kien Dang Ngoc


5 Answers

You need to put these files

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

into your bin\Debug (or bin\Release, based on your configuration).

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

like image 177
Toan Nguyen Avatar answered Oct 29 '22 15:10

Toan Nguyen


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

  • check that all files are in the apps root directory (https://github.com/cefsharp/CefSharp/wiki/Output-files-description-table-%28Redistribution%29 and https://bitbucket.org/chromiumembedded/cef/src/aefb5ccce879f308f0bcc3ac719c86defe2f9715/tools/distrib/win/README.redistrib.txt?at=master&fileviewer=file-view-default)
  • made sure the C++ Redist was installed (in fact I have 2010, 2013, 2015 all x86 and x64 installed)
  • added the following code during startup to ensure all dependencies are there

        string dir = AppDomain.CurrentDomain.BaseDirectory;
        var missingDeps = CefSharp.DependencyChecker.CheckDependencies(true, false, dir, string.Empty,
            Path.Combine(dir, "CefSharp.BrowserSubprocess.exe"));
        if (missingDeps?.Count > 0)
            throw new InvalidOperationException("Missing components:\r\n  " + string.Join("\r\n  ", missingDeps));
        // ReSharper disable once UnusedVariable
        var browser = new CefSharp.Wpf.ChromiumWebBrowser(); //test, if browser can be instantiated
    

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

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!

like image 39
uTILLIty Avatar answered Oct 29 '22 15:10

uTILLIty


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

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.dll or CefSharp.WPF.dll (depending on your project type)

See CefSharp - Frequently asked questions.

like image 7
Kurubaran Avatar answered Oct 29 '22 16:10

Kurubaran


This is a common problem and is therefore mentioned in the FAQ, question number 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-Build action, which can set under the project settings in Visual Studio.

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

<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.Example project 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.)

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.

like image 6
Per Lundberg Avatar answered Oct 29 '22 17:10

Per Lundberg


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.

  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-support I updated my project to include:

<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

like image 3
Victor Thomas Wilcox Jr. Avatar answered Oct 29 '22 16:10

Victor Thomas Wilcox Jr.