通通WPF隨筆(2)——自己制作輕量級asp.net網站服務


 


       大學玩asp.net時就發現VS在Debug時會起一個web服務,這東西也太神奇了服務起得這么快,而相對於IIS又這么渺小。

       前幾個月在用phonegap+jqmobi(被inter收購后叫App Framework)做手機應用開發。用dreamweaver CS6的雲編譯確實挺方便的,但是寫代碼的話還是比較喜歡VS的代碼聯想。本地調試時,點擊又啟動了這個web服務。

       再后來要開發一個在WPF嵌入網頁的控件,果斷用WebBrowser控件簡單地封裝了一下,這樣發現當只是打開本地的html頁面時就會彈出安全阻止信息,需要手動點一下允許,度娘的各種改IE設置就是沒用,最后發現通過訪問web服務返回的頁面就不會彈出該提示了。第一反映就是用VS自帶的這個來實現,但是托盤會彈氣泡等,所以不得不改造下了。

 用360查看該端口定位文件

 

 

151KB,還綠色版,這也太牛了。但是但我用局域網地址進行訪問時就發現不可訪問,這是為什么呢?有點太可惜了所以就上網找了這個exe的相關資料,有人已經修改了可以局域網訪問了,但是他改的東西下載下來報錯,於是就決定自己動手進行修改。

仔細閱讀之前各位大神寫的文章后,綜合優化了一下,終於出現了,真正綠色版(.NET 4.0),win8下完美運行:

 純正綠色版,無任何微軟信息

 

 

1.修改過程


  把WebDev.WebServer40.EXE拖到ILSpy.exe里進行反編譯成項目(本人比較支持免費軟件)

  同樣也反編譯應用的WebDev.WebHost40成項目

server里對host的引用刪了,重新引用剛反編譯的項目。

修改Microsoft.VisualStudio.WebHost.Server里的方法Start(),修改的代碼如下:

 

if (Socket.OSSupportsIPv6)
            {
                try
                {
                    this._socketIpv6 = this.CreateSocketBindAndListen(AddressFamily.InterNetworkV6, IPAddress.IPv6Any, this._port);
                }
                catch (SocketException ex)
                {
                    if (ex.SocketErrorCode == SocketError.AddressAlreadyInUse || !flag)
                    {
                        throw;
                    }
                }
            }
            if (flag)
            {
                try
                {
                    this._socketIpv4 = this.CreateSocketBindAndListen(AddressFamily.InterNetwork, IPAddress.Any, this._port);
                }
                catch (SocketException)
                {
                    if (this._socketIpv6 == null)
                    {
                        throw;
                    }
                }
            }

 

 Microsoft.VisualStudio.WebHost.RequestTryParseRequest()方法一個判斷注釋掉

private bool TryParseRequest()
        {
            this.Reset();
            this.ReadAllHeaders();
            //if (!this._connection.IsLocal)
            //{
            //    this._connection.WriteErrorAndClose(403);
            //    return false;
            //}
            if (this._headerBytes == null || this._endHeadersOffset < 0 || this._headerByteStrings == null || this._headerByteStrings.Count == 0)
            {
                this._connection.WriteErrorAndClose(400);
                return false;
            }

這時候F6編譯一下發現 Microsoft.VisualStudio.WebHost.NtlmAuth下的Authenticate()方法報一大堆Fixed關鍵字錯誤,做如下修改騙過編譯器

fixed (SecHandle* lptr = (&this._securityContext))
            {
                IntPtr* ptr = (IntPtr*)lptr;
                fixed (SecBuffer* lptr2 = (&this._inputBuffer))
                {
                    IntPtr* ptr2 = (IntPtr*)lptr2;
                    fixed (SecBuffer* lptr3 = (&this._outputBuffer))
                    {
                        IntPtr* ptr3 = (IntPtr*)lptr3;
                        fixed (byte* lptr4 = (&array[0]))
                        {
                            IntPtr* ptr4 = (IntPtr*)lptr4;
                            fixed (byte* lptr5 = (&array2[0]))
                            {
                                IntPtr* ptr5 = (IntPtr*)lptr5;
                                IntPtr phContext = IntPtr.Zero;
                                if (this._securityContextAcquired)
                                {
                                    phContext = (IntPtr)((void*)ptr);
                                }

Microsoft.VisualStudio.WebHost.ConnectionGetHost()方法里加入如下代碼(要把WebDev.WebHost40.dll復制到站點目錄的bin目錄下):

lock (this._lockObject)
                {
                    host = this._host;
                    if (host == null)
                    {
                        //復制當前dll到站點目錄
                        Assembly myAss = Assembly.GetExecutingAssembly();
                        string assUrl = myAss.Location;
                        if (!File.Exists(this._physicalPath + "\\bin\\" + myAss.FullName.Split(',')[0] + ".dll"))
                        {
                            if (!Directory.Exists(this._physicalPath + "\\bin"))
                            {
                                Directory.CreateDirectory(this._physicalPath + "\\bin");
                            }
                            File.Copy(assUrl, this._physicalPath + "bin\\" + myAss.FullName.Split(',')[0]+".dll");
                        }
                       
                        string text = (this._virtualPath + this._physicalPath).ToLowerInvariant();
                        string appId = text.GetHashCode().ToString("x", CultureInfo.InvariantCulture);
                        this._host = (Host)this._appManager.CreateObject(appId, typeof(Host), this._virtualPath, this._physicalPath, false);
                        this._host.Configure(this, this._port, this._virtualPath, this._physicalPath, this._requireAuthentication, this._disableDirectoryListing);
                        host = this._host;
                    }
                }

最后一步,為WebDev.WebHost40.dll添加簽名,不然運行時還是會去加載自帶的WebDev.WebHost40.dll

編譯一下,這樣運行WebDev.WebServer40.EXE加載本地路徑就可以局域網訪問了。

為了簡單易用,我用WPF做了窗口來方便WebServer40.EXE啟動參數的傳遞,在傳遞參數時多加入一個“/silent:true”參數就可以靜默運行了。具體怎么啟動怎么傳參具體我就不說了,參看Process這個類。

 運行如下:

 

 

下載地址:

http://files.cnblogs.com/tong-tong/TTWebServer.zip

 

 

 參考文獻:

 http://www.cnblogs.com/huigll/archive/2011/02/25/1851112.html

  

后記


  近來在各種房貸、老婆貸的壓力下接了各種私活,各種加班,各種被坑,不過數錢時還是比較爽的~~~當今物價都在上漲,唯獨工資不漲,那錢都去了哪里了呢???

 

 

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM