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"):





本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2025年4月    »
123456
78910111213
14151617181920
21222324252627
282930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864