日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WCF 客户端调用服务操作的两种方法

發布時間:2024/4/14 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WCF 客户端调用服务操作的两种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本節的主要內容:1、通過代理類的方式調用服務操作。2、通過通道的方式調用服務操作。3、代碼下載

一、通過代理類的方式調用服務操作(兩種方式添加代理類)

1.手動編寫代理類,如下:

客戶端契約:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.ServiceModel; 7 namespace y.WcfFirst.Client.Proxys 8 { 9 [ServiceContract] 10 public interface IHello 11 { 12 [OperationContract] 13 string Say(string name); 14 } 15 } View Code

客戶端代理類:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.ServiceModel; 7 using System.ServiceModel.Channels; 8 namespace y.WcfFirst.Client.Proxys 9 { 10 public class HelloProxy:ClientBase<IHello>,IHello 11 { 12 public HelloProxy() 13 : base() 14 { 15 } 16 17 public string Say(string name) 18 { 19 return base.Channel.Say(name); 20 } 21 } 22 } View Code

客戶端app.config文件:

1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <system.serviceModel> 4 <client> 5 <endpoint name="wcfFirst" 6 address="net.tcp://localhost:6666/hello" 7 binding="netTcpBinding" 8 contract="y.WcfFirst.Client.Proxys.IHello"></endpoint> 9 </client> 10 </system.serviceModel> 11 </configuration> View Code

客戶端的調用:

1 using (HelloProxy proxy = new HelloProxy()) 2 { 3 Console.WriteLine("Recevie from Server:{0}", proxy.Say(name)); 4 proxy.Close(); 5 } View Code

2.通過Metadata方式產生代理類。

服務端需要對app.config進行配置如下:

客戶端的操作步驟:先運行服務端(host)。

a.在客戶端點擊Add Service Reference按鈕,添加Service引用。如下圖:

b.輸入Address地址:http://localhost:8888,點擊"GO",獲取服務操作。并且重命名Namespace,如下圖:

c.客戶端對代理類的調用。

1 using (HelloClient clientProxy = new HelloClient()) 2 { 3 Console.WriteLine("Recevie from Server:{0}", clientProxy.Say(name)); 4 clientProxy.Close(); 5 } View Code

二、通過通道方式調用服務操作

1.客戶端契約,如下:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.ServiceModel; 7 namespace y.WcfFirst.ClientChannel.Proxy 8 { 9 [ServiceContract] 10 public interface IHello 11 { 12 [OperationContract] 13 string Say(string name); 14 } 15 } View Code

2.客戶端配置文件的設置:如下:

1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <system.serviceModel> 4 <client> 5 <endpoint name="wcfFirst" 6 address="net.tcp://localhost:6666/hello" 7 binding="netTcpBinding" 8 contract="y.WcfFirst.ClientChannel.Proxy.IHello"></endpoint> 9 </client> 10 </system.serviceModel> 11 </configuration> View Code

3.客戶端調用服務操作:如下:

1 ChannelFactory<IHello> factory = new ChannelFactory<IHello>("wcfFirst"); 2 IHello channelProxy = factory.CreateChannel(); 3 using(channelProxy as IDisposable) 4 { 5 Console.WriteLine("Recevie from Server:{0}", channelProxy.Say(name)); 6 } View Code

?

demo

轉載于:https://www.cnblogs.com/liuxiaoji/p/4538584.html

總結

以上是生活随笔為你收集整理的WCF 客户端调用服务操作的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。