Kiba518

Kiba518

三线城市架构师一枚。

Fork me on GitHub

【Net】CEF浏览IISExpress运行Web项目

前言

本文介绍在Winform桌面应用中,使用IISExpress做Host主机,启动.Net平台的Web项目。

浏览Web网页使用CEF开源组件。

准备

首先创建Winform项目WinFormIISExpressHost。

然后把IISExpress文件夹放到项目的Bin\Debug下。

寻找IISExpress

寻找IISExpress很简单,如果本机安装了VS,那么可以直接在C:\Program Files\IIS Express下找到。

当然,也可以上官网下载安装,然后到安装目录找IISExpress,官方下载地址:https://www.microsoft.com/zh-CN/download/details.aspx?id=34679

注意:IISExpress默认的文件夹名是【IIS Express】,这中间有个空格,我们需要去掉它,因为后面需要用命令行启动IISExpress,空格会给我们带来很多麻烦。

启动IISExpress

IISExpress文件夹下有个iisexpress.exe文件,我们只要启动它IIS就会运行。

但IISExpress有个问题,它默认不读取当前目录的下配置文件【AppServer\applicationhost.config】;所以我们在启动IISExpress时,必须指定它的启动文件。

指定IISExpress的命令行如下,可以在CMD下运行。

start C:\Users\Administrator\Desktop\IISExpress\iisexpress /config:C:\Users\Administrator\Desktop\IISExpress\AppServer\applicationhost.config

现在我们把该命令行转换成代码启动,如下:

public string DirMain = Environment.CurrentDirectory + @"\";
private void Form1_Load(object sender, EventArgs e)
{
    var plist = System.Diagnostics.Process.GetProcessesByName("iisexpress");
    if (plist.Count() <= 0)
    {
        string para = $@"/config:{DirMain}IISExpress\AppServer\applicationhost.config";
        Start($@"{DirMain}IISExpress\iisexpress", para);
​
    }
}
public static bool Start(string programPath, string para)
{
    try
    {
        Process myProcess = new Process();
        myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        myProcess.StartInfo.UseShellExecute = false;
        myProcess.StartInfo.FileName = programPath;
        myProcess.StartInfo.CreateNoWindow = true;
        myProcess.StartInfo.Arguments = para;
        myProcess.EnableRaisingEvents = false;
        bool boo = myProcess.Start();
        return boo;
    }
    catch (Exception ex)
    {
        return false;
    }
} 

修改IISExpress配置文件

IISExpress的配置文件是AppServer\applicationhost.config,现在我们修改它,让IISExpress指定当前项目的路径下的Bin\WebSite文件夹为网站应用的根目录。

用记事本打开applicationhost.config,然后找到sites(网站配置节点)。

修改网站信息中的physicalPath(物理路径)属性的值。

ps:我们还可以修改网站运行端口,和在复制一个Site节点,增加另一个网站。

<sites>
            <site name="Development Web Site" id="1" serverAutoStart="true">
                <application path="/">
                    <virtualDirectory path="/" physicalPath="D:\WinFormIISExpressHost\bin\Debug\WebSite" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation=":5180:localhost" />
                </bindings>
            </site>
            <siteDefaults>
                <!-- To enable logging, please change the below attribute "enabled" to "true" -->
                <logFile logFormat="W3C" directory="%AppData%\Microsoft\IISExpressLogs" enabled="false" />
                <traceFailedRequestsLogging directory="%AppData%\Microsoft" enabled="false" maxLogFileSizeKB="1024" />
            </siteDefaults>
            <applicationDefaults applicationPool="IISExpressAppPool" />
            <virtualDirectoryDefaults allowSubDirConfig="true" />
        </sites> 

测试

现在我们新建一个WebTEST的MVC网站项目,然后将其发布;将发布的文件放到刚刚的Winform项目的Bin/Website文件夹下(也可以直接发布到该文件夹下)。

然后运行项目。

项目运行后,电脑右下角会出现IISExpress的图标。

然后我们访问http://localhost:5180/。访问成功;如下图:

CEF应用

IISExpress已经成功运行了,现在我们使用CEF来浏览网页。(CEF是一个使用Chrome内核的Browser)

首先引用CEF(有时候引用了CEF后,项目会出现未刷新的情况,关闭重启即可在引用中看到引用的DLL了),如下图:

引用了CEF后,我们会发现,项目编译会报错;这是因为CEF不支持AnyCPU,所以我们需要将平台目标改成X64。(项目属性和解决方案配置都要修改)

不过很多时候,我们的解决方案必须使用AnyCPU,那么,我们就需要修改下工程文件了。(项目属性必须是X64)

用记事本打开WinFormIISExpressHost.csproj;在第一个PropertyGroup下增加节点<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>;如下图:

修改完工程文件VS会提示重新加载项目;点击确定重新加载,然后项目已经可以编译过去了。

现在我们将CEF应用到项目中,代码如下:

var chromeBrowser = new ChromiumWebBrowser("http://localhost:5180/");
            panelParent.Controls.Add(chromeBrowser);

然后运行项目。如下图,项目在Anycpu下运行成功。

注:如果需要访问的网站是Https,且该网站没有合法的证书,则需要跳过证书限制,跳过证书代码如下:

 var setting = new CefSettings();
            //设置语言 
            setting.Locale = "zh-CN";
            //cef设置userAgent
            setting.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
            //配置浏览器路径
            
            setting.CefCommandLineArgs.Add("--ignore-urlfetcher-cert-requests", "1");//解决证书问题
            setting.CefCommandLineArgs.Add("--ignore-certificate-errors", "1");//解决证书问题
            CefSharp.Cef.Initialize(setting, performDependencyCheck: true, browserProcessHandler: null);

  

----------------------------------------------------------------------------------------------------

代码已经传到Github上了,欢迎大家下载。

Github地址:https://github.com/kiba518/WinFormIISExpressHost

----------------------------------------------------------------------------------------------------

注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!
若您觉得这篇文章还不错,请点击下方的推荐】,非常感谢!

https://www.cnblogs.com/kiba/p/12719481.html

 

 

posted @ 2020-05-18 09:44  kiba518  阅读(1023)  评论(4编辑  收藏  举报