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>
接下来在网站上添加对服务的引用
发现服务并添加您创建的服务。现在构建您的解决方案并发布它。**注意不要将解决方案发布在桌面或文档等用户目录中,否则 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 关注
- 94 浏览
添加回答
举报