前言
本文介紹在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

