1.配置支持AnyCpu编译模式
CefSharp从51版本以后开始支持AnyCpu编译模式,首先需要在当前项目的csproj文件的PropertyGroup节点下第一行增加一个配置项
true
然后在程序的启动入口配置动态加载目标平台x86/x64的程序集:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AppDomain.CurrentDomain.AssemblyResolve += Resolver;
Application.Run(new Form1());
}
private static Assembly Resolver(object sender, ResolveEventArgs args)
{
if (args.Name.StartsWith("CefSharp"))
{
string assemblyName = args.Name.Split(new[] { ',' }, 2)[0] + ".dll";
string archSpecificPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
Environment.Is64BitProcess ? "x64" : "x86",
assemblyName);
return File.Exists(archSpecificPath)
? Assembly.LoadFile(archSpecificPath)
: null;
}
return null;
}
这种方法是根据运行的目标平台动态去加载对应的程序集,如果我们能明确运行平台则可以不用加上面的代码逻辑,在当前项目App.config文件的根节点下加入以下配置即可:
2.使用Http代理服务
网上一些文章介绍的通过添加命令行参数CefCommandLineArgs的方式,我试了一下不管用,通过 CefSharpSettings.Proxy = new ProxyOptions("ipadress", "prot", "username", "password"); 这句是可以配置成功的,但这个是全局配置,不能满足独立我要控制每个browser实例各自使用自己的代理服务器。通