共用方式為


逐步解說:建立託管的 Web Core 應用程式

本逐步解說示範如何建立 Windows 主控台應用程式,以使用 IIS 7 中提供的託管 Web 核心功能。

IIS 7 中的託管 Web Core 功能可讓您建立載入 IIS 核心實例的應用程式,這是 IIS 所提供的基底 Web 應用程式和內容服務功能。 如需託管 Web 核心的詳細資訊,請參閱 託管的 Web Core API 參考

您必須為遵循ApplicationHost.config檔案格式的應用程式提供組態檔。 不過,您不應該使用實際的ApplicationHost.config檔案,因為它可能包含與您網站設定衝突的設定。 如需如何為應用程式建立組態檔的詳細資訊,請參閱逐步解說 :建立託管 Web Core 的組態檔

本逐步解說所述的工作包括下列各項:

  • 為您的應用程式建立 C++ 專案。

  • 為您的應用程式新增 C++ 程式碼。

  • 編譯及測試您的應用程式。

  • 針對使用應用程式時的錯誤進行疑難排解。

必要條件

需要下列軟體才能完成範例中的步驟:

  • IIS 7.

注意

雖然您必須在已安裝 IIS 7 的電腦上執行裝載的 Web Core 應用程式,但您不需要在已安裝 IIS 7 的電腦上編譯範例應用程式。 您可以在不同版本的 Windows 上編譯應用程式,然後將您的應用程式複製到已安裝 IIS 7 的電腦。

  • Visual Studio 2005。

注意

您也可以使用 Visual Studio .NET 2003 或更早版本,雖然逐步解說步驟可能不相同。

建立託管的 Web Core 應用程式

在本逐步解說的這個部分中,您將為您的應用程式建立 C++ 主控台應用程式專案。

為您的應用程式建立 C++ 專案

  1. 啟動 Visual Studio 2005。

  2. 確認全域選項具有 SDK 包含檔案的所有正確路徑:

    1. [工具] 功能表上,按一下 [選項]

      [ 選項] 對話方塊隨即開啟。

    2. 展開樹狀檢視中的 [ 專案和方案 ] 節點,然後按一下 [VC++ 目錄]。

    3. 在 [ 顯示目錄] 方塊中,選取 [包含檔案]。

    4. 確認您安裝 SDK 包含檔案的路徑已列出。 如果未列出路徑,請按一下 [ 新增行 ] 圖示,然後新增您安裝 SDK 包含檔案的路徑。

    5. 按一下 [確定]。

  3. 建立新的 C++ 專案:

    1. [檔案] 功能表上,指向 [開新檔案] ,然後按一下 [專案]

      此時會開啟 [新增專案] 對話方塊。

    2. 在 [ 專案類型] 窗格中,展開 [Visual C++ ] 節點,然後按一下 [ Win32]。

    3. 在 [ 範本] 窗格中,按一下 [Win32 專案]。

    4. 在 [ 名稱] 方塊 中,輸入 HostedWebTest

    5. 在 [ 位置] 方塊中,輸入專案的路徑。

    6. 按一下 [確定]。

      Win32 應用程式精靈隨即開啟。

    7. 按一下 [應用程式設定]。

    8. [應用程式類型] 底下,按一下 [主控台應用程式]。

    9. 按一下 [完成] 。

      Visual Studio 會開啟 HostWebTest.cpp 專案。

  4. 使用 __stdcall (/Gz) 呼叫慣例設定要編譯的專案:

    1. 在 [ 專案] 功能表上,按一下 [HostedWebTest 屬性]。

    2. 展開樹狀檢視中的 [ 組態屬性] 節點,展開 C/C++ 節點,然後按一下 [ 進階]。

    3. 在 [ 組態 ] 方塊中,選取 [所有設定]

    4. 在 [ 呼叫慣例] 方塊中,選取 [__stdcall (/Gz)

    5. 按一下 [確定]。

新增應用程式的程式碼

本節說明如何將自動新增至 C++ 檔案的程式碼取代為將執行託管 Web Core 的程式碼。

注意

此範例會在 Inetsrv 資料夾中尋找名為 HostedWebTest.config 的檔案。 您可以修改路徑和檔案名,但您必須提供有效的組態檔。 如需如何為應用程式建立組態檔的詳細資訊,請參閱逐步解說 :建立託管 Web Core 的組態檔

為您的應用程式新增 C++ 程式碼

  1. 如果尚未開啟,請開啟 HostedWebTest.cpp 檔案,然後移除所有現有的 C++ 程式碼。

  2. 將下列 C++ 程式碼複製到 檔案中:

    #include "stdafx.h"
    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>
    #include <hwebcore.h>
    
    // NOTE: Set the project's calling convention to "__stdcall (/Gz)".
    
    HRESULT _cdecl _tmain(int argc, _TCHAR* argv[])
    {
        // Create a handle for the Web core DLL.
        HINSTANCE hDLL;
    
        // Specify the HRESULT for returning errors.
        HRESULT hr = S_OK;
    
        // Create arrays to hold paths.
        WCHAR wszInetPath[MAX_PATH];
        WCHAR wszDllPath[MAX_PATH];
        WCHAR wszCfgPath[MAX_PATH];
    
        // Retrieve the path of the Inetsrv folder.
        DWORD nSize = ::ExpandEnvironmentStringsW(
            L"%windir%\\system32\\inetsrv",wszInetPath,MAX_PATH);
    
        // Exit if the path of the Inetsrv folder cannot be determined.
        if (nSize == 0)
        {
            // Retrieve the last error.
            hr = HRESULT_FROM_WIN32(GetLastError());
            // Return an error status to the console.
            printf("Could not determine the path to the Inetsrv folder.\n");
            printf("Error: 0x%x\n",hr);
            // Return an error from the application and exit.
            return hr;
        }
    
        // Append the Web core DLL name to the Inetsrv path.
        wcscpy_s(wszDllPath,MAX_PATH-1,wszInetPath);
        wcscat_s(wszDllPath,MAX_PATH-1,L"\\");
        wcscat_s(wszDllPath,MAX_PATH-1,WEB_CORE_DLL_NAME);
    
        // Append the config file name to the Inetsrv path.
        wcscpy_s(wszCfgPath,MAX_PATH-1,wszInetPath);
        wcscat_s(wszCfgPath,MAX_PATH-1,L"\\HostedWebTest.config");
    
        // Create a pointer to WebCoreActivate.
        PFN_WEB_CORE_ACTIVATE pfnWebCoreActivate = NULL;
    
        // Create a pointer to WebCoreShutdown.
        PFN_WEB_CORE_SHUTDOWN pfnWebCoreShutdown = NULL;
    
        // Load the Web core DLL.
        hDLL = ::LoadLibraryW(wszDllPath);
    
        // Test whether the Web core DLL was loaded successfully.
        if (hDLL == NULL)
        {
            // Retrieve the last error.
            hr = HRESULT_FROM_WIN32(GetLastError());
            // Return an error status to the console.
            printf("Could not load DLL.\n");
            printf("Error: 0x%x\n",hr);
        }
        else
        {
            // Return a success status to the console.
            printf("DLL loaded successfully.\n");
            // Retrieve the address for "WebCoreActivate".
            pfnWebCoreActivate = (PFN_WEB_CORE_ACTIVATE)GetProcAddress(
                hDLL,"WebCoreActivate");
            // Test for an error.
            if (pfnWebCoreActivate==NULL)
            {
                // Retrieve the last error.
                hr = HRESULT_FROM_WIN32(GetLastError());
                // Return an error status to the console.
                printf("Could not resolve WebCoreActivate.\n");
                printf("Error: 0x%x\n",hr);
            }
            else
            {
                // Return a success status to the console.
                printf("WebCoreActivate successfully resolved.\n");
                // Retrieve the address for "WebCoreShutdown".
                pfnWebCoreShutdown = (PFN_WEB_CORE_SHUTDOWN)GetProcAddress(
                    hDLL,"WebCoreShutdown");
                // Test for an error.
                if (pfnWebCoreShutdown==NULL)
                {
                    // Retrieve the last error.
                    hr = HRESULT_FROM_WIN32(GetLastError());
                    // Return an error status to the console.
                    printf("Could not resolve WebCoreShutdown.\n");
                    printf("Error: 0x%x\n",hr);
                }
                else
                {
                    // Return a success status to the console.
                    printf("WebCoreShutdown successfully resolved.\n");
                    // Return an activation status to the console.
                    printf("Activating the Web core...\n");
                    // Activate the Web core.
                    hr = pfnWebCoreActivate(wszCfgPath,L"",L"TestWebCore");
                    // Test for an error.
                    if (FAILED(hr))
                    {
                        // Return an error status to the console.
                        printf("WebCoreActivate failed.\n");
                        printf("Error: 0x%x\n",hr);
                    }
                    else
                    {
                        // Return a success status to the console.
                        printf("WebCoreActivate was successful.\n");
                        // Prompt the user to continue.
                        printf("Press any key to continue...\n");
                        // Wait for a key press.
                        int iKeyPress = _getch();
                        // Return a shutdown status to the console.
                        printf("Shutting down the Web core...\n");
                        // Shut down the Web core.
                        hr = pfnWebCoreShutdown(0L);
                        // Test for an error.
                        if (FAILED(hr))
                        {
                            // Return an error status to the console.
                            printf("WebCoreShutdown failed.\n");
                            printf("Error: 0x%x\n",hr);
                        }
                        else
                        {
                            // Return a success status to the console.
                            printf("WebCoreShutdown was successful.\n");
                        }
                    }
                }
            }
            // Release the DLL.
            FreeLibrary(hDLL);
        }    
        // Return the application status.
        return hr;
    }
    
  3. 儲存 HostedWebTest.cpp 檔案。

編譯及測試應用程式

建立並儲存 C++ 檔案之後,下一個步驟是編譯及測試您的應用程式。

注意

如果您未在已安裝 IIS 7 的電腦上編譯應用程式,您必須先將HostedWebTest.exe檔案複製到已安裝 IIS 7 的電腦,再測試應用程式。

編譯及測試您的應用程式

  1. 編譯應用程式:

    1. 在 [建置] 功能表上,按一下 [建置方案]。

    2. 確認 Visual Studio 未傳回任何錯誤或警告。 如果您看到任何錯誤或警告,則必須在測試專案之前解決這些問題。

  2. 開啟 Windows 檔案總管,並找出您在建立 C++ 專案時指定的資料夾。

    根據您的組建選項,您應該會在專案的預設資料夾中看到名為 DebugRelease 的資料夾。

  3. 在 [ 偵錯 ] 或 [ 發行] 資料夾中,找出名為 HostedWebTest.exe 的檔案。

  4. 確認您的組態檔位於應用程式的適當資料夾中。 例如,本逐步解說稍早所列的程式碼範例是設計成使用位於 Inetsrv 資料夾中HostedWebTest.config名為 的檔案。

  5. 開啟 [命令提示字元] 視窗,並將目錄變更為HostedWebTest.exe檔案所在的路徑。

  6. 輸入 HostedWebTest ,然後按 ENTER 以啟動應用程式。

  7. 您應該會看到與下列類似的應用程式輸出:

    DLL loaded successfully.
    WebCoreActivate successfully resolved.
    WebCoreShutdown successfully resolved.
    Activating the Web core...
    WebCoreActivate was successful.
    Press any key to continue...
    
  8. 將 [命令提示字元] 視窗最小化,而不按下鍵盤上的任何按鍵。

  9. 根據您的組態設定,您可以開啟網頁瀏覽器,並流覽至組態檔中定義的網站。

  10. 當您完成流覽至您的網站時,請返回 [命令提示字元] 視窗,然後按空格鍵。

  11. 您應該會看到與下列類似的應用程式輸出:

    Shutting down the Web core...
    WebCoreShutdown was successful.
    

錯誤疑難排解

如果您的應用程式未載入,或在執行時傳回錯誤,下列步驟將協助您診斷您可能遇到的一些錯誤。

針對應用程式中的錯誤進行疑難排解

  • 如果您的應用程式傳回下列錯誤:

    Could not load DLL.
    Error: 0x8007007e
    

    這是ERROR_MOD_NOT_FOUND狀態。 此錯誤表示無法找到Hwebcore.dll檔案。 如果未安裝 IIS,就會發生此錯誤。

  • 如果您的應用程式傳回下列錯誤:

    DLL loaded successfully.
    WebCoreActivate successfully resolved.
    WebCoreShutdown successfully resolved.
    Activating the Web core...
    WebCoreActivate failed.
    Error: 0x8007000d
    

    這是ERROR_INVALID_DATA狀態。 此錯誤表示您的組態檔無法找到或包含錯誤。 您應該在 Windows 事件檢視器中查看其他錯誤描述。

  • 如果您的應用程式傳回下列錯誤:

    DLL loaded successfully.
    WebCoreActivate successfully resolved.
    WebCoreShutdown successfully resolved.
    Activating the Web core...
    WebCoreActivate failed.
    Error: 0x800700b7
    

    這是ERROR_ALREADY_EXISTS狀態。 此錯誤表示已載入您的組態檔,但它包含重複的資訊。 例如,您可能已定義多個應用程式集區,或者您可能已建立重複的網站系結。 您應該檢查 Windows 事件檢視器是否有其他錯誤描述。

  • 如果您的應用程式成功載入,但當您流覽應用程式所裝載的網站時收到 HTTP 404 錯誤,您應該檢查應用程式針對 404 子狀態碼所建立的 IIS 記錄。 以下是您可能會看到的一些子狀態碼:

    • 404.3 「Mime Map 拒絕」— 此子狀態碼表示未正確設定所要求資源的 MIME 類型。 例如,如果您流覽至副檔名為 .txt 的檔案,而且您尚未將該副檔名新增至組態檔中的 MIME 對應,您將會遇到此程式碼。

    • 404.4 「No Handler」— 此子狀態碼表示尚未為要求的資源設定處理常式。 例如,如果您流覽至副檔名為 .htm 的檔案,而且您尚未將靜態檔案處理常式新增至組態檔,您就會遇到此程式碼。

    • 404.7 「拒絕副檔名」— 此子狀態碼表示已由要求篩選封鎖副檔名。 例如,如果您流覽至副檔名為.gif的檔案,且要求篩選已設定為拒絕存取該副檔名的檔案,您就會遇到此程式碼。

      這些錯誤都表示應用程式組態檔中的設定有問題。 如需詳細資訊,請參閱逐步解說 :建立託管 Web Core 的組態檔

另請參閱