zoukankan      html  css  js  c++  java
  • WCF初探-10:WCF客户端调用服务

    创建WCF 服务客户端应用程序需要执行下列步骤:

    • 获取服务终结点的服务协定、绑定以及地址信息
    • 使用该信息创建 WCF 客户端
    • 调用操作
    • 关闭该 WCF 客户端对象

    WCF客户端调用服务存在以下特点:

    • 服务和客户端使用托管属性、接口和方法对协定进行建模。 若要连接客户端应用程序中的服务,则需要获取该服务协定的类型信息。通常,我们使用Svcutil.exe(ServiceModel Metadata Utility Tool)来完成,也可以直接在客户端项目上引用服务地址完成。它们会从服务中下载元数据,并使用您选择的语言将其转换到托管源代码文件中,同时还创建一个您可用于配置 WCF 客户端对象的客户端应用程序配置文件
    • WCF 客户端是表示某一个WCF服务的一个本地对象,客户端可以使用这种形式与远程服务进行通信。 WCF 客户端类型可以实现目标服务协定,因此当您创建一个服务协定,并对其进行配置后,就可以直接使用客户端对象调用服务操作。 WCF 运行时将方法调用转换为消息,然后将这些消息发送到服务,侦听回复,并将这些值作为返回值或 out 参数(或 ref 参数)返回到 WCF 客户端对象中。
    • 创建并配置了客户端对象后,请创建一个 try/catch 块,如果该对象是本地对象,则以相同的方式调用操作,然后关闭 WCF 客户端对象。 当客户端应用程序调用第一个操作时,WCF 将自动打开基础通道,并在回收对象时关闭基础通道。 (或者,还可以在调用其他操作之前或之后显式打开和关闭该通道。)
    • 不应该使用 using 块来调用WCF服务方法。因为C# 的“using”语句会导致调用 Dispose()。 它等效于 Close(),当发生网络错误时可能会引发异常。 由于对 Dispose() 的调用是在“using”块的右大括号处隐式发生的,因此导致异常的根源往往会被编写代码和阅读代码的人所忽略。 这是应用程序错误的潜在根源。

    WCF客户端调用服务方式:

    • WCF通信机制由它自身复杂的体系结构所决定,但WCF服务给我们提供了两种不同的机制来创建客户端程序调用,一种是ClientBase<TChannel>类,另一种是ChannelFactory<TChannel> 类。
    • ClientBase<TChannel>:创建客户端代理类的基类,客户端代理类通过继承该基类,调用WCF的内部通信机制来实现WCF客户端与服务端的通信。代理类是一个公开单个CLR接口来表示服务契约的CLR类,代理类和服务契约很相似,但是他有着附加的方法来管理代理的生命周期和连接服务。通过visual studio 右键添加服务引用和通过svcutil.exe命令行工具生成的客户端都属于这种方式。(如果不熟悉svcutil.exe,请参照WCF初探-1:认识WCF)
    • ChannelFactory<TChannel>:使用通道工厂类取决于你是否拥有描述服务契约的本地接口。最大的好处是你可以已扩展的方式更容易的修改通道的通信机制,如果你需要共享服务和客户端之间的契约组件,那么使用ChannelFactory<TChannel>可以更有效的节省时间,但客户端必须完成对服务契约组件的引用。
    • ClientBase<TChannel>和ChannelFactory<TChannel>的差异:

        

    WCF客户端调用服务示例:

    • 解决方案如下图所示:

        

    • 工程结构说明:

            Service:类库程序,定义服务契约和实现,里面包含User数据契约和GetInfo()获取用户信息的服务契约方法。

            IUserInfo.cs的代码如下:  

      
    using System.ServiceModel;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    
    namespace Service
    {
        [ServiceContract]
        public interface IUserInfo
        {
            [OperationContract]
            User[] GetInfo(int? id=null);
        }
    
        [DataContract]
        public class User
        {
            [DataMember]
            public int ID { get; set; }
            [DataMember]
            public string Name { get; set; }
            [DataMember]
            public int Age { get; set; }
            [DataMember]
            public string Nationality { get; set; }
        }
    }
    View Code

        UserInfo.cs的代码如下:  

      
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Service
    {
        public class UserInfo:IUserInfo
        {
            public User[] GetInfo(int? id=null)
            {
                List<User> Users = new List<User>();
                Users.Add(new User { ID = 1, Name = "JACK", Age = 20, Nationality = "CHINA" });
                Users.Add(new User { ID = 2, Name = "TOM", Age = 18, Nationality = "JAPAN" });
                Users.Add(new User { ID = 3, Name = "SMITH", Age = 22, Nationality = "KOREA" });
                Users.Add(new User { ID = 4, Name = "ALENCE", Age = 21, Nationality = "INDIA" });
                Users.Add(new User { ID = 5, Name = "JOHN", Age = 22, Nationality = "SINGAPORE" });
    
                if (id != null)
                {
                    return Users.Where(x => x.ID == id).ToArray();
                }
                else
                {
                    return Users.ToArray();
                }
            }
        }
    }
    View Code    

        Host:控制台应用程序,添加对Service程序集的引用,寄宿服务程序。

            Program.cs代码如下

      
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Service;
    using System.ServiceModel;
    
    namespace Host
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(UserInfo)))
                {
                    host.Opened += delegate { Console.WriteLine("服务已经启动,按任意键终止!"); };
                    host.Open();
                    Console.Read();
                }
            }
        }
    }
    View Code

        App.config代码如下:

      
    <?xml version="1.0"?>
    <configuration>
        <system.serviceModel>
            
            <services>
                <service name="Service.UserInfo" behaviorConfiguration="mexBehavior">
                    <host>
                        <baseAddresses>
                            <add baseAddress="http://localhost:1234/UserInfo/"/>
                        </baseAddresses>
                    </host>
                    <endpoint address="" binding="wsHttpBinding" contract="Service.IUserInfo" />
                    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
                </service>
            </services>
    
            <behaviors>
                <serviceBehaviors>
                    <behavior name="mexBehavior">
                        <serviceMetadata httpGetEnabled="true"/>
                        <serviceDebug includeExceptionDetailInFaults="true"/>
                    </behavior>
                </serviceBehaviors>
            </behaviors>
        </system.serviceModel>
    </configuration>
    View Code   

        Client1:控制台应用程序,添加对服务终结点地址http://localhost:1234/UserInfo/的引用,设置服务命名空间为UserInfoServiceRef,生成客户端代理类和配置文件代码后,完成Client1对服务的调用。

        Program.cs的代码如下:

      
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Client1.UserInfoServiceRef;
    
    namespace Client1
    {
        class Program
        {
            static void Main(string[] args)
            {
                UserInfoClient proxy = new UserInfoClient();
                User[] Users = proxy.GetInfo(null);
                Console.WriteLine("{0,-10}{1,-10}{2,-10}{3,-10}","ID","Name","Age","Nationality");
                for(int i=0;i<Users.Length;i++)
                {
                      Console.WriteLine("{0,-10}{1,-10}{2,-10}{3,-10}",
                        Users[i].ID.ToString(),
                        Users[i].Name.ToString(),
                        Users[i].Age.ToString(),
                        Users[i].Nationality.ToString());
                }
    
                Console.Read();
            }
        }
    }
    View Code

        Client2:控制台应用程序,使用svcutil.exe工具生成客户端代理类,在命令行中输入以下命令:

        1.在运行中输入cmd打开命令行,输入 cd C:Program Files (x86)Microsoft SDKsWindowsv7.0ABin

        2.输入svcutil.exe /out:f: UserInfoClient.cs /config:f:App.config http://localhost:1234/UserInfo/ (注意:端口号改成本机服务寄宿的端口号)    

        3.将生成的App.config和UserInfoClient.cs复制到Client2的工程目录下,完成Program.cs代码,代码和Client1的代码一样。

        Client3:控制台应用程序,添加对Service程序集的引用,完成Program.cs代码,代码如下:  

      
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Service;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    
    namespace Client3
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                EndpointAddress address = new EndpointAddress("http://localhost:1234/UserInfo");
                WSHttpBinding binding = new WSHttpBinding();
                ChannelFactory<IUserInfo> factory = new ChannelFactory<IUserInfo>(binding,address);
                IUserInfo channel = factory.CreateChannel();
    
    
                User[] Users = channel.GetInfo(null);
                Console.WriteLine("{0,-10}{1,-10}{2,-10}{3,-10}", "ID", "Name", "Age", "Nationality");
                for (int i = 0; i < Users.Length; i++)
                {
                    Console.WriteLine("{0,-10}{1,-10}{2,-10}{3,-10}",
                      Users[i].ID.ToString(),
                      Users[i].Name.ToString(),
                      Users[i].Age.ToString(),
                      Users[i].Nationality.ToString());
                }
    
                ((IChannel)channel).Close();
                factory.Close();
                Console.Read();
            }
        }
    }
    View Code

        查看服务运行结果,结果如下图所示:

        

    • 客户端代理类,我们以svcutil.exe生成的代理类文件为例子,查看UserInfoClient.cs,代码如下:
    复制代码
    namespace Service
    {
        using System.Runtime.Serialization;
        
        
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
        [System.Runtime.Serialization.DataContractAttribute(Name="User", Namespace="http://schemas.datacontract.org/2004/07/Service")]
        public partial class User : object, System.Runtime.Serialization.IExtensibleDataObject
        {
            
            private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
            
            private int AgeField;
            
            private int IDField;
            
            private string NameField;
            
            private string NationalityField;
            
            public System.Runtime.Serialization.ExtensionDataObject ExtensionData
            {
                get
                {
                    return this.extensionDataField;
                }
                set
                {
                    this.extensionDataField = value;
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public int Age
            {
                get
                {
                    return this.AgeField;
                }
                set
                {
                    this.AgeField = value;
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public int ID
            {
                get
                {
                    return this.IDField;
                }
                set
                {
                    this.IDField = value;
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public string Name
            {
                get
                {
                    return this.NameField;
                }
                set
                {
                    this.NameField = value;
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public string Nationality
            {
                get
                {
                    return this.NationalityField;
                }
                set
                {
                    this.NationalityField = value;
                }
            }
        }
    }
    
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="IUserInfo")]
    public interface IUserInfo
    {
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IUserInfo/GetInfo", ReplyAction="http://tempuri.org/IUserInfo/GetInfoResponse")]
        Service.User[] GetInfo(System.Nullable<int> id);
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    public interface IUserInfoChannel : IUserInfo, System.ServiceModel.IClientChannel
    {
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    public partial class UserInfoClient : System.ServiceModel.ClientBase<IUserInfo>, IUserInfo
    {
        
        public UserInfoClient()
        {
        }
        
        public UserInfoClient(string endpointConfigurationName) : 
                base(endpointConfigurationName)
        {
        }
        
        public UserInfoClient(string endpointConfigurationName, string remoteAddress) : 
                base(endpointConfigurationName, remoteAddress)
        {
        }
        
        public UserInfoClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(endpointConfigurationName, remoteAddress)
        {
        }
        
        public UserInfoClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(binding, remoteAddress)
        {
        }
        
        public Service.User[] GetInfo(System.Nullable<int> id)
        {
            return base.Channel.GetInfo(id);
        }
    }
    复制代码

        我们可以看到客户端代理类中有着数据契约、服务契约,并且客户端代理类UserInfoClient继承ClientBase<IUserInfo>, IUserInfo,通过WCF内部通道机制完成对服务的调用。   

       public Service.User[] GetInfo(System.Nullable<int> id)

        {

              returnbase.Channel.GetInfo(id);

       }

        

    • ChannelFactory<IUserInfo>通道工厂通过:

        EndpointAddress address = new EndpointAddress("http://localhost:1234/UserInfo");

            WSHttpBinding binding = new WSHttpBinding();

            ChannelFactory<IUserInfo> factory = new ChannelFactory<IUserInfo>(binding,address);

            IUserInfochannel = factory.CreateChannel();     

            创建通道,完成对WCF服务的调用,注意:此处的工厂虽然提供了很多重载的方法,但我们这里属于客户端代码编程,因此我们读取不到配置文件信息,所以我把终结点信息

       和绑定信息写到了代码中,如果想通过ChannelFactory<IUserInfo>读取配置文件信息,我们需要手动实现其扩展机制。这里以后的博文再做讨论

    • 总结:我们通过实际的例子完成了对WCF客户端调用服务的几种方式的验证,也完成了代理类和通道工厂对服务调用的相关概念的诠释,在后面的博文中我将介绍客户端调用WCF服务的其他特性。

     

  • 相关阅读:
    线程中死锁的demo
    发布.net core程序碰到的问题
    .net core Identity学习(三) 第三方认证接入
    .net Identity学习(二)OAuth
    .net core Identity学习(一)注册登录
    Git常用操作
    log4net使用
    c#中的Quartz
    jquery中的deferred
    .net core应用部署在IIS上
  • 原文地址:https://www.cnblogs.com/linybo/p/13305875.html
Copyright © 2011-2022 走看看