vs使用添加服务引用方式连接HTTPS协议的WCF服务
![](http://www.csframework.com/images/article-type5.gif)
![vs使用添加服务引用方式连接HTTPS协议的WCF服务 vs使用添加服务引用方式连接HTTPS协议的WCF服务](http://www.csframework.com/images/seo/SEO-WCF%E5%BC%80%E5%8F%91%E6%A1%86%E6%9E%B6.jpg)
客户端连接WCF服务,常用的两种连接方式:
1. 使用透明代理
2. 添加服务引用
下面将讲解添加服务引用方式连接WCF服务, 操作步骤:
1. VS解决方案,添加服务引用:
![贴图图片-WCF添加服务引用连接HTTPS](http://www.csframework.com/upload/image/WCF添加服务引用连接HTTPS.png)
2. 输入https协议地址,点【转到】,若地址正确会显示WCF服务接口名称,
最后修改命名空间,如:MyCommonService,点【确定】按钮。
![贴图图片-WCF添加服务引用连接HTTPS1](http://www.csframework.com/upload/image/WCF添加服务引用连接HTTPS1.png)
添加WCF服务引用后,VS会自动生成WCF接口的客户端代理类,
名字空间:MyCommonService
WCF代理类:MyCommonService.CommonServiceClient
WCF接口:MyCommonService.ICommonService
![贴图图片-WCF添加服务引用连接HTTPS2](http://www.csframework.com/upload/image/WCF添加服务引用连接HTTPS2.png)
添加WCF服务引用后,App.config会自动生成binding/endpoint 配置:
XML Code:
<binding name="WSHttpBinding_ICommonService1">
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
若服务器配置https协议,客户端:<security mode="Transport">
XML Code:
<endpoint address="https://cs5.manonwo.com/CommonService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICommonService1"
contract="MyCommonService.ICommonService" name="WSHttpBinding_ICommonService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICommonService1"
contract="MyCommonService.ICommonService" name="WSHttpBinding_ICommonService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
测试调用WCF服务接口:
C# Code:
private void button3_Click(object sender, EventArgs e)
{
byte[] loginTicket = GetLoginer();
MyCommonService.CommonServiceClient svc = new MyCommonService.CommonServiceClient();
//调用WCF接口,获取单据号码
var no = svc.GetDataSN(loginTicket, "AA", true);
ShowResult(no);
svc.Close();
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
byte[] loginTicket = GetLoginer();
MyCommonService.CommonServiceClient svc = new MyCommonService.CommonServiceClient();
//调用WCF接口,获取单据号码
var no = svc.GetDataSN(loginTicket, "AA", true);
ShowResult(no);
svc.Close();
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
获取单据号码,测试正常:
![贴图图片-WCF添加服务引用连接HTTPS4](http://www.csframework.com/upload/image/WCF添加服务引用连接HTTPS4.png)
当WCF接口发生变动,比如添加接口、修改接口参数,此时客户端必须更新服务引用:
![贴图图片-WCF添加服务引用连接HTTPS3](http://www.csframework.com/upload/image/WCF添加服务引用连接HTTPS3.png)
![C/S框架网|原创精神.创造价值.打造精品](http://www.csframework.com/images/our03.gif)
扫一扫加作者微信
![C/S框架网作者微信](http://www.csframework.com/images/wechatcode.jpg)
![C/S框架网|原创作品.质量保障.竭诚为您服务](http://www.csframework.com/images/CS框架网提供高效优质服务.jpg)
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网