• 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
  • 相关阅读:
    redis和memcache的区别
    c语言行编辑程序
    C语言栈的实现
    双向链表
    静态链表的合并
    静态链表的创建
    链表
    将非递减有序排列(L L1)归并为一个新的线性表L2 线性表L2中的元素仍按值非递减
    C语言合并两个集合(L,L1) 将L1中不在L中的元素插入到L线性表中
    oracle--JOB任务
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3020404.html
Copyright © 2020-2023  润新知