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

11

21

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

Unprecedented answered 13/8, 2013 at 7:28 Comment(3)
install Visual C++ Redistributable 2012. It should work.Luciennelucier
FYI, Norton Antivirus will try to quarantine cefsharp.dll .Hypnotist
As ARUNRAJ said Install this: microsoft.com/en-au/download/details.aspx?id=30679Vendetta
Q
26

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.

Quinlan answered 29/10, 2013 at 15:5 Comment(2)
At least with my install the 2nd file should be icudtl.dat, not icudt.dll. Note the "l" after the "t" before ".dat".Coaming
icudtl.dat is the Unicode support file not icudt.dll See github.com/cefsharp/CefSharp/wiki/…Ideomotor
A
8

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.

Adkins answered 13/8, 2013 at 8:1 Comment(1)
So how can I add libcef and icudt, I know that it's win32 dll not a .net dll so I can't add it like a reference, follow the instruction from microsoft, I did like above, but it didn't work.Unprecedented
H
7

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.

Harlot answered 26/10, 2013 at 18:50 Comment(1)
This solution is cleaner than the top upvoted answer, it doesn't match the NuGet CefSharp dir structure but it gives you the general idea.Lumbard
B
7

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

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!

Burgee answered 25/2, 2016 at 15:58 Comment(1)
THANK YOU. I've been reading all the answers online, including the FAQ, and they all left out a couple of components that the DependencyChecker found.Handwriting
E
3

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

Expertise answered 20/1, 2020 at 0:7 Comment(2)
See github.com/cefsharp/CefSharp.MinimalExample#net-core-support specifically you need to add entries to your project github.com/cefsharp/CefSharp.MinimalExample/blob/master/…Unemployable
@Unemployable Thank you!!! That explains it. I was having trouble at first because via nuget the are project references and setting privateassets all there was having no effect, but adding separate itemgroup with private true works!Expertise
G
1

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.

Grozny answered 13/8, 2013 at 8:16 Comment(3)
it still throws the error no matter I changed it to copy always or copy if newerUnprecedented
@KienDangNgoc did you resolve this as i m facing the same issueMukul
actually adding those two directly into my bin release and setting 'copy to output' worked for me thanks c_wiz_kidMukul
C
0

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

Instead of following step A and adding CefSharpAnyCpuSupport in csproj and probing in App.config, simply setting the Platform Target to x86 in step B did the trick.

Cyndi answered 25/6, 2018 at 12:36 Comment(0)
B
0

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 opens with most common issues.

In my case I was missing (x64 also available)

<probing privatePath="x86" />

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

<?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.

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

Beekman answered 14/8, 2018 at 23:2 Comment(3)
The files are only copied into x86 and x64 sub folders when you choose AnyCpu, if you pick x86 or x64 as your solution target then the files will be copied directly into the bin folder. Setting probing privatePath as you suggest on an x64 will crash unless your executable is set to prefer 32bit.Unemployable
Note that you must set x86/x64 at the solution level to use that approach, just changing for your individual project won't work. See github.com/cefsharp/CefSharp.MinimalExample for a working exampleUnemployable
Thanks for your input. I updated my question to mention x64 and the reason of selecting AnyCPUBeekman
P
0
  1. Make sure all dependent libraries are copied to debug/release folder
  2. If you build CEFSharp libs by your own, make sure the build configuration matches. That is, if you build your application in Release mode , x86 then you must build CEFSharp libs also in Release, x86. Other wise it will throw dll not found error
Pitman answered 10/9, 2020 at 19:5 Comment(0)
A
0

enter image description here

I found that one of the project platform is mentioned as x64. I changed it to "Any CPU" it works fine.

Alvinaalvine answered 18/12, 2020 at 22:11 Comment(0)
C
-3

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

Chiron answered 11/8, 2016 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.