WCF 客户端调用几种方式
WCF 客户端调用几种方式
我们首先先新建一个WCF服务项目(代码没有改变,都是默认生成),然后把它部署到IIS上面,为我们下面客户端调用做准备(当然IIS宿主只是其中一种,还有Windows服务、Winform程序、控制台程序中进行寄宿);
方式一:利用工具svcutil.exe命令生成代理类
svcutil.exe {终结点}/out:{输出文件.cs} /config:{配置文件.config}
如: svcutil.exe http://localhost:8089/Service1.svc?wsdl /out:Client.cs /config:app.config
1:首先开打Visual Studio 命令提示
2:输入生成客户端及配置文件的命令(注意命令的空格及路径);
3:生成成功后会在相应的路径找到文件,并把它复制到我们项目内;
4:引入System.RuntimeSerivalization及System.ServiceModel
5:客户端调用代码如下:
Service1Client ClientHost = new Service1Client();
MessageBox.Show(ClientHost.GetData( 5 ));
方式二:直接在项目引用服务
1:这种方式比较简单,直接在项目增加引用服务便可以;
2:同样也要引入System.RuntimeSerivalization及System.ServiceModel
3:客户端调用代码如下:
ServiceReference1.Service1Client ServerHost = new ServiceReference1.Service1Client();
MessageBox.Show(ServerHost.GetData( 5 ));
*用此种方式会在配置文件里自动生成一些相关配置如下:
<? xml version="1.0" encoding="utf-8" ?>
< configuration >
< system.serviceModel >
< bindings >
< basicHttpBinding >
< binding name ="BasicHttpBinding_IService1" />
</ basicHttpBinding >
</ bindings >
< client >
< endpoint address ="http://localhost:14187/Service1.svc" binding ="basicHttpBinding"
bindingConfiguration ="BasicHttpBinding_IService1" contract ="ServiceReference1.IService1"
name ="BasicHttpBinding_IService1" />
</ client >
</ system.serviceModel >
</ configuration >
方式三:使用ChannelFactory调用
1:首先若是我们要采用配置时 在app.config里增加配置如下:
<? xml version="1.0" encoding="utf-8" ?>
< configuration >
< system.serviceModel >
< client >
< endpoint name ="ClientTestServer" address ="http://localhost:8089/Service1.svc"
binding ="basicHttpBinding" contract ="WindowsForIIS3.IService1" ></ endpoint >
</ client >
</ system.serviceModel >
</ configuration >
2:这边要注意是我们服务契约接口复制到项目中;当然把服务契约单独建立一个类库然后再引用做法会更好:
3:客户端调用代码如下:
ChannelFactory<IService1> factory = new ChannelFactory<IService1>( " ClientTestServer " );
IService1 proxy = factory.CreateChannel();
MessageBox.Show(proxy.GetData( 5 ));
*上面采用的是ChannelFactory调用配置文件的方式,也可以采用ChannelFactory编码方式;代码如下:
using (ChannelFactory<IService1> channelFactory = new ChannelFactory<IService1>( new BasicHttpBinding(), " http://localhost:8089/Service1.svc " ))
{
IService1 proxy = channelFactory.CreateChannel();
MessageBox.Show(proxy.GetData( 5 ));
}
方式四:使用ClientBase方式调用
1:app.config里增加配置如下:
<? xml version="1.0" encoding="utf-8" ?>
< configuration >
< system.serviceModel >
< client >
< endpoint name ="ClientTestServer" address ="http://localhost:8089/Service1.svc"
binding ="basicHttpBinding" contract ="WindowsForIIS3.IService1" ></ endpoint >
</ client >
</ system.serviceModel >
</ configuration >
2:先建一个代理类分别继承:ClientBase<服务契约接口>,服务契约接口 代码如下:
using System.Runtime.Remoting;
using System.ServiceModel;
namespace WindowsForIIS3
{
public class ServerProxy:ClientBase < IService1 > ,IService1
{
public string GetData(int ValueCount)
{
return base.Channel.GetData(ValueCount);
}
}
}
3:客户端调用代码如下:
ServerProxy proxy = new ServerProxy();
MessageBox.Show(proxy.GetData(5));
*通过ClientBase对象进行服务调用,其内部也是调用ChannelFactory创建的服务代理
以上这些方法来进行WCF客户端的调用;在测试时遇到一个错误顺便记录如下(解决方式修改配置文件:binding="basicHttpBinding"):
若是文章对您有帮助记得帮忙推荐一下!
标签: WCF
作者: Leo_wl
出处: http://HdhCmsTestcnblogs测试数据/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息