3 回答

TA貢獻1966條經驗 獲得超4個贊
First Credit where credit is due, many issues I was pointed in the right direction by multiple SO users to name a few would be: Mikael bolinder , Abraham Qian , Lex Li , C# 聊天室里的好人和一個合作伙伴我的工作人員(在編寫此答案時沒有 SO 帳戶),在這個答案中,我計劃涵蓋使用 HTTPS 安全性在 IIS 服務器中托管 WCF 庫可能需要的所有內容。
我使用的工具:
Visual Studio 2017 專業版
IIS 10(Windows 10 自帶,但必須在 Windows 功能中激活)(見下文)
首先:確保您已安裝 visual studio 所需的所有組件。
Windows -> .Net 桌面開發
Windows -> Universal Windows platfor development
Web & Cloud -> ASP.NET 和 Web 開發
在這個列表和其他列表中,可能會包含一些額外的組件,原因是我安裝了它們,但無法驗證它們是否絕對必要。
現在,讓我們添加必要的 Windows 功能。控制面板 -> 程序 -> 打開或關閉 Windows 功能
確保進入 WCF 服務并檢查 HTTP 激活,不要被方塊愚弄(我的錯誤之一)
現在讓我們開始創建服務。打開 Visual Studio File -> New -> Project -> Visual C# -> Web -> WCF -> WCF Service Library這會生成您嘗試托管的 MCVE
現在您必須將它與網站鏈接以生成 Web.Config 文件和 SVC 文件,為此,在 Solutions Explorer 上,右鍵單擊您的解決方案,Add-> New Website。
現在在 web.config 文件中添加
<system.serviceModel>
<services>
<service name="WcfServiceLibrary4.Service1"> <!-- Change the library name as per your need -->
<endpoint address=""
binding="wsHttpBinding"
bindingConfiguration="secureHttpBinding"
contract="WcfServiceLibrary4.IService1"/> <!-- Change the library name as per your need -->
<endpoint address="mex"
binding="mexHttpsBinding"
contract="IMetadataExchange" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="secureHttpBinding">
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
接下來在網站上添加對服務的引用
發現服務并添加您創建的服務?,F在構建您的解決方案并發布它。**注意不要將解決方案發布在桌面或文檔等用戶目錄中,否則 ACL 權限會讓您頭疼,而是直接將其發布在 Drive 中的目錄中。
現在托管時間 首先讓我們打開 IIS(始終以管理員身份)并創建一個新證書。
在服務器上轉到 IIS 部分并選擇服務器證書,然后單擊右端的創建新證書。
現在從左側菜單創建一個新網站
確保切換到 https 并在此處選擇您的證書,現在要驗證您的服務是否已創建,您需要瀏覽您創建的網站 svc 文件,遺憾的是此時您會收到一條錯誤消息,說Server Error in '/' Application.
Could not find a base address that matches scheme http for the endpoint with binding BasicHttpBinding. Registered base address schemes are [https].
我無法找到問題的原因錯誤,但我能夠找到繞過它的方法,
繞過此錯誤的步驟 -> 從左側菜單中選擇您的網站,在右鍵單擊綁定的菜單中添加一個 HTTP 綁定,使用不同的端口。在此之后,您將能夠瀏覽您的 svc 文件的 HTTPS 版本。
現在,如果您瀏覽 HTTPS 鏈接,您會收到服務已創建的消息
現在您可以繼續創建一個使用該服務的應用程序。
什么樣的未來
我將嘗試找到一種方法來執行此操作而無需添加額外的綁定
第二個目標是在不添加額外網站的情況下實現這一目標。
如果我實現了這些,我會更新,但是這些不是我現在的優先事項,可能會在很長一段時間內不會添加,如果有任何不合理的地方,或者您有任何改進的想法,請在下面評論。

TA貢獻1802條經驗 獲得超4個贊
首先請不要將IIS物理路徑設置為桌面,這樣會造成權限問題。我們可以將IIS站點的物理路徑設置為C分區下的文件夾。
第二,請看下面的鏈接,主要說明WCF服務庫項目無法直接發布到IIS,因為其Appconfig無法被IIS識別,除非在網站根目錄手動添加額外的Webconfig。
https://learn.microsoft.com/en-us/dotnet/framework/wcf/deploying-a-wcf-library-project
一般而言,我們使用包含自動生成的 Webconfig 文件的 WCF 服務項目模板而不是 WCF服務庫項目模板,因為它通常用作類庫。
默認情況下,該服務由 BasicHttpBinding 托管,它取決于以下標記。
<protocolMapping>
<add binding="basicHttpBinding" scheme="http" />
</protocolMapping>
該服務也可以通過以下方式手動配置。這兩種方式配置文件是等價的。
<system.serviceModel>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="mybehavior">
<endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1" bindingConfiguration="mybinding"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="mybinding">
<security mode="None">
</security>
</binding>
</basicHttpBinding>
</bindings>
不需要在 webconfig 中分配服務端點地址。應該在IIS站點綁定模塊中完成。
WCF System.ServiceModel.EndpointNotFoundException
最后,我們可以通過添加額外的服務端點來通過 https 托管服務,請參考以下配置(簡化配置)。
<protocolMapping>
<add binding="basicHttpBinding" scheme="http"/>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
然后在IIS站點綁定模塊中添加https協議。
配置 Web.config 以發布 WCF 服務
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-an-iis-hosted-wcf-service-with- ssl
如果有什么我可以幫忙的,請隨時告訴我。

- 3 回答
- 0 關注
- 150 瀏覽
添加回答
舉報