【原创】WCF开发框架 - 采用wsHttpBinding及basicHttpBinding连接https协议的WCF服务


WCF开发框架 - 采用wsHttpBinding及basicHttpBinding两种绑定方式连接https协议的WCF服务
本文将详细介绍基于wsHttpBinding绑定,采用https协议的WCF服务,主要内容有:
一、服务端配置
二、发布部署IIS承载的WCF服务
三、IIS配置
四、测试WCF服务
五、CSFramework基础架构客户端程序连接WCF服务
六、basicHttpBinding 绑定https协议
一、服务端配置
开发环境vs2017,打开WCF_IISServer网站的web.config文件,参考以下配置:
1.1 web.config文件配置 - bingings节点
配置wsHttpBinding协议绑定,<security mode="Transport">
XML Code:
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None"/>
<message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
<bindings>
<!--wsHttpBindings配置-->
<wsHttpBinding>
<binding name="WSHttpBindings" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<!--wsHttpBindings配置-->
<wsHttpBinding>
<binding name="WSHttpBindings" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None"/>
<message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
此功能仅适用于在 IIS 下承载的 WCF 服务。 默认情况下不启用此功能。 若要启用它,必须将 multipleSiteBindingsEnabled 特性添加到 serviceHostingEnvironment,并将其设置为 true。
XML Code:
<system.serviceModel>
<!--支持多个IIS站点绑定,同时支持http/https-->
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
<!--支持多个IIS站点绑定,同时支持http/https-->
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
1.2 web.config文件配置 - services节点
binding="wsHttpBinding"
bindingConfiguration="WSHttpBindings"
注意:endpoint配置:binding="mexHttpsBinding"
XML Code:
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="WSHttpBindings" contract="CSFrameworkV5.WCFContract.ICommonService">
<identity>
<dns value="localhost"/>
</identity>
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
</service>
</services>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
<!--WCF服务配置,配置服务对应的接口-->
<services>
<service behaviorConfiguration="myBehavior" name="CSFrameworkV5.WCFContract.WCF_Class.CommonService">
<services>
<service behaviorConfiguration="myBehavior" name="CSFrameworkV5.WCFContract.WCF_Class.CommonService">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="WSHttpBindings" contract="CSFrameworkV5.WCFContract.ICommonService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
</service>
</services>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
注意下面的:mexHttpsBinding

1.3 web.config文件配置 - behaviors节点 (行为配置)
httpsGetEnabled="true"
XML Code:
<!--WCF服务配置 通用行为配置-->
<behaviors>
<serviceBehaviors>
<behavior name="myBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="200" maxConcurrentInstances="200"/>
</behavior>
</serviceBehaviors>
</behaviors>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
<behaviors>
<serviceBehaviors>
<behavior name="myBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="200" maxConcurrentInstances="200"/>
</behavior>
</serviceBehaviors>
</behaviors>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
二、发布IIS承载的WCF服务(IIS宿主),采用https协议
*******************************************************************
注意:在VS开发环境运行IIS默认是启动IISExpress服务(http协议),IISExpress是个轻量级IIS,作者未验证是否能支持https协议及配置SSL证书,可能非常的复杂!因此用我们的阿里云ECS服务器(测试服务器)部署IIS。
*******************************************************************
2.1 发布部署IIS
参考:WCF开发框架 - vs发布IIS承载(IIS Hosting)的WCF应用服务详解
2.2 绑定https协议,安装ssl证书
选择网站,在右侧菜单点击【绑定...】,打开编辑网站绑定界面:

查看ssl证书颁发的域名:
选择SSL证书要注意,若服务器安装多个SSL证书,最后一个是最新安装的ssl证书,必须要核对域名是否一致。

三、IIS配置
3.1 启用32位应用程序,设置为true。
若IIS服务器所部署的电脑是64位操作系统,必须设置!

若不配置,会出现以下错误:

四、测试WCF服务
4.1 在iis内浏览网站,默认打开IE浏览器,https协议正常

4.2 输入:https://cs5.manonwo.com/CommonService.svc,单个WCF服务https协议正常

五、C/S框架旗舰版v5.1客户端配置:
5.1 配置app.config 文件
打开vs, 修改CSFrameworkV5.Main下面的App.config文件,添加如下配置:
(https默认端口:443,若指定端口号,必须与IIS服务器的端口一致)
XML Code:
<appSettings>
<!--IIS承载的WCF服务地址(HTTPS)协议,阿里云服务器测试-->
<add key="ICommonService" value="https://cs5.manonwo.com/CommonService.svc"/>
<add key="IDataDictService" value="https://cs5.manonwo.com/DataDictService.svc"/>
<add key="IMessageCenter" value="https://cs5.manonwo.com/MessageCenter.svc"/>
<add key="ISystemSecurityService" value="https://cs5.manonwo.com/SystemSecurityService.svc"/>
</appSettings>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
<!--IIS承载的WCF服务地址(HTTPS)协议,阿里云服务器测试-->
<add key="ICommonService" value="https://cs5.manonwo.com/CommonService.svc"/>
<add key="IDataDictService" value="https://cs5.manonwo.com/DataDictService.svc"/>
<add key="IMessageCenter" value="https://cs5.manonwo.com/MessageCenter.svc"/>
<add key="ISystemSecurityService" value="https://cs5.manonwo.com/SystemSecurityService.svc"/>
</appSettings>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
支持HTTPS(ssl证书)配置:<security mode="Transport">
XML Code:
<!--WCF服务-客户端配置-->
<system.serviceModel>
<bindings>
<!--WSHttpBinding 绑定配置-->
<wsHttpBinding>
<binding name="WSHttpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
<!--HTTP协议
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" />
<message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false" />
</security>
-->
<!--HTTPS协议 -->
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None" />
<message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false" />
</security>
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
<system.serviceModel>
<bindings>
<!--WSHttpBinding 绑定配置-->
<wsHttpBinding>
<binding name="WSHttpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
<!--HTTP协议
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" />
<message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false" />
</security>
-->
<!--HTTPS协议 -->
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None" />
<message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false" />
</security>
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
5.2 修改WCFFactory.cs 文件的Create方法(在这里创建三种绑定的对象实例)

六、运行C/S框架基础架构(ClientFoundation)程序,https协议连接正常:

查询/修改数据正常:

七、basicHttpBinding 绑定https协议
参考上面的步骤,与wsHttpBinding配置相同。
把wsHttpBinding相关参数配置改为basicHttpBinging配置。
WCF快速开发框架 - IIS承载WCF http协议BasicTcpBinding配置详解

<本文完>
C/S架构软件快速开发平台-旗舰版V5.1 (Ultimate Edition 2021)






C/S架构软件快速开发平台助力开发团队快速搭建自己的软件项目,旗舰版提供强大的底层开发架构及快速开发工具-Winform三层架构代码生成器v5.1,旗舰版集成大量应用于大型系统的通用功能模块、数据界面及通用权限管理系统,提供丰富的实例开发模板、开发文档、线上技术指导服务,助力您快速搭建软件项目。
C/S架构开发框架系列产品已成功应用500多家企业、4000多位软件用户,其中包括国内知名软件公司、国有企业、研发机构及上市公司(优秀企业选择了我们的产品-成功案例)。经过十年迭代升级,最新旗舰版V5.1,基础架构更成熟、应用更广泛、性能更稳定、开发效率更高!
开发框架配套的核心工具 - Winform三层架构代码生成器能快速生成界面(FORM)、业务层(BLL)、数据层(DAL)、模型(Model)、报表(Report)以及VS工程项目(Project)的源代码,快速提升开发效率,节约开发时间,降低项目成本,根据多个项目统计,开发框架能为您减少60%以上的工作量。
开发框架经过多个技术层面性能测试,能满足企业级大型软件项目开发技术指标,稳定、开源、快速是开发框架的核心竞争力。我们积极收集用户反馈的意见,不断完善和改进产品。我们秉着分享成功经验坚持产品创新原则,专业、专心、专注的工匠精神,致力于服务IT同行,为您创造价值。
--- 唯快不破,以势赢,以力胜!

扫一扫加作者微信


版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网