wcf服务引用后即使连接中断静态类也可以使用

wcf服务引用后及时连接中断静态类也可以使用

为了验证这个结果,首先创建一个控制平台应用程序,并在程序中引用服务,配置文件如下:

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IDownLoadService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:2725/WCF/DownLoadService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDownLoadService"
                contract="ServiceReferenceDl.IDownLoadService" name="BasicHttpBinding_IDownLoadService" />
        </client>
    </system.serviceModel>
</configuration>


在程序中代码如下:

 static void Main(string[] args)
        {
            ServiceReferenceDl.DownLoadServiceClient client = null;
            try
            {
                //定义绑定
                System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding()
                {
                    MaxBufferPoolSize = int.MaxValue,
                    MaxBufferSize = int.MaxValue,
                    MaxReceivedMessageSize = int.MaxValue
                };
                binding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas() { MaxArrayLength = 2147483647, MaxStringContentLength = 2147483647, MaxBytesPerRead = 4096 };
                binding.Security = new System.ServiceModel.BasicHttpSecurity { Mode = BasicHttpSecurityMode.None };
                //重新定义总结点,注意和服务引用的不一样。
                EndpointAddress address = new EndpointAddress("http://192.168.81.152:8082/WCF/DownLoadService.svc");
                //初始化成功
                client = new ServiceReferenceDl.DownLoadServiceClient(binding, address);
                //获取数据-成功
                List<ServiceReferenceDl.Rfa_Tb_DownLoadTemp> lists = client.GetDownLoadTempByUserId("lpp").ToList();
                if (lists == null || lists.Count < 1) { Console.WriteLine("获取数据为空!"); return; }
                for (int i = 0; i < lists.Count; i++)
                {
                    Console.WriteLine("时间:" + lists[i].CreateDate + "文件名称" + lists[i].FileName);
                }
                client.Close(); //关闭
            }
            catch (Exception ex)
            {
                if (client != null)
                    client.Close();
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey(true);
        }

 

结论:

在引用wcf服务后,会在本地生成引用的实体类。在重新绑定总结点后,这些实体类仍然可以用。实例化client的过程,只不过是建立客户端和服务端连接的过程,默认的实例化的方式是以默认连接地址(在config中)进行连接,也可以重新配置。

注:以上仅是个人小结,仅作为参考。本人还未详细深入了解wcf,以后或许可以更深刻的解析其中奥妙。

 

 

 

 

 

 

posted @ 2014-07-09 10:51  造梦者2013  阅读(264)  评论(0)    收藏  举报