成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

WCF應用編碼具體實現步驟講解

開發 開發工具
WCF應用編碼的實際操作方法比較復雜,不過,我們可以通過不斷的實踐來進行熟練的應用。在這篇文章中將會針對此做一個詳細介紹。

要向熟練運用WCF,首先需要掌握它的實際應用編碼,才能正確的理解這一工具的應用特點。在這里我們將會為大家詳細介紹一下WCF應用編碼的相關代碼編寫,方便大家理解,讓朋友們從中獲得一些幫助。

先來看看這段WCF應用編碼,然后再解說一下。

  1. class Program  
  2. {  
  3. static void Main(string[] args)  
  4. {  
  5. AppDomain.CurrentDomain.UnhandledException += new 
    UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);  
  6. using (ServiceHost serviceHost = new ServiceHost
    (typeof(ServiceMonitor)))  
  7. {  
  8. NetNamedPipeBinding binding = new NetNamedPipeBinding();  
  9. binding.Security.Mode = NetNamedPipeSecurityMode.None;  
  10. binding.ReceiveTimeout = TimeSpan.Parse("00:00:05");  
  11. binding.MaxReceivedMessageSize = 6553600;  
  12. binding.ReaderQuotas.MaxStringContentLength = 6553600;  
  13. serviceHost.AddServiceEndpoint(typeof(IMonitor), 
    binding, "net.pipe://localhost/ServiceMonitor");  
  14. //ServiceMetadataBehavior behavior = serviceHost.Description.
    Behaviors.Find
    <ServiceMetadataBehavior>();  
  15. //if (behavior == null)  
  16. //{  
  17. // behavior = new ServiceMetadataBehavior();  
  18. // serviceHost.Description.Behaviors.Add(behavior);  
  19. //}  
  20. serviceHost.Opened += delegate  
  21. {  
  22. Console.WriteLine("正在運行的服務提供IMonitor功能..");  
  23. };  
  24. serviceHost.Open();  
  25. while (true)  
  26. {  
  27. Console.WriteLine("服務正在運行,要退出請鍵入exit");  
  28. string cmd = Console.ReadLine();  
  29. if (cmd == "exit")  
  30. break;  
  31. }  
  32. }  
  33. }  
  34. static void CurrentDomain_UnhandledException(object sender, 
    UnhandledExceptionEventArgs e)  
  35. {  
  36. Console.WriteLine("剛才的操作發生異常,信息如下:");  
  37. Console.Write(e.ToString());  
  38. }  
  39. }  
  40. [ServiceContract]  
  41. public interface IMonitor  
  42. {  
  43. [OperationContract]  
  44. void Record(string key, string value);  
  45. }  
  46. public class ServiceMonitor : IMonitor  
  47. {  
  48. public void Record(string key, string value)  
  49. {  
  50. Console.WriteLine(string.Format("Key = {0}", key));  
  51. Console.WriteLine(string.Format("Value = {0}", value));  
  52. Console.WriteLine(new string('*', 50));  
  53. }  
  54. }  
  55. public static class ServiceMonitorClientManager  
  56. {  
  57. public static void Record(string key, string value)  
  58. {  
  59. try  
  60. {  
  61. EndpointAddress address = new EndpointAddress
    ("net.pipe://localhost/ServiceMonitor");  
  62. NetNamedPipeBinding binding = new NetNamedPipeBinding();  
  63. binding.Security.Mode = NetNamedPipeSecurityMode.None;  
  64. binding.SendTimeout = TimeSpan.Parse("00:00:01");  
  65. binding.ReaderQuotas.MaxStringContentLength = 6553600;  
  66. binding.MaxReceivedMessageSize = 6553600;  
  67. IMonitor iMonitor = ChannelFactory<IMonitor>.
    CreateChannel(binding, address);  
  68. using (iMonitor as IDisposable)  
  69. {  
  70. iMonitor.Record(key, value);  
  71. }  
  72. }  
  73. catch (System.ServiceModel.CommunicationObjectFaultedException) { }  
  74. catch (System.ServiceModel.EndpointNotFoundException) { }  
  75. }  

1、通過using (ServiceHost serviceHost = new ServiceHost(typeof(ServiceMonitor))) 初始化了一個ServiceHost對象,然后通過WCF應用編碼創建ServiceEndpoint然后添加到ServiceHost對象中,根據ABC規則,ServiceEndpoint的創建最少需要傳入Contract、Binding、Address,例如:

  1. serviceHost.AddServiceEndpoint(typeof(IMonitor), 
    binding, "net.pipe://localhost/ServiceMonitor"); 

2、創建ServiceHost后還可以添加相應的IServiceBehavior實現例如:內置的ServiceMetadataBehavior等,也可以創建自定義的Behavior
public class CustomBehavior :IServiceBehavior可以通過serviceHost.Description.Behaviors.Add(behavior);把內置或或自定義的Behavior添加到ServiceHost中。#t#

3、WCF的客戶端代理可以通過ChannelFactory來創建,只要為ChannelFactory<T>.CreateChannel 方法傳入Binding和Address參數即可,當然也可以通過
public class ContentReceiverClient : ClientBase<T>, T
如:public class ContentReceiverClient : ClientBase<IMonitor>, IMonitor 方式創建

4、當使用ChannelFactory創建客戶代理時請調用IDisposable方法關閉資源
using (iMonitor as IDisposable)如果使用Client : ClientBase<T>, T 創建客戶代理如:
base.Channel.接口方法
則需要在調用完后Client.Close()關閉資源。

以上就是我們為大家詳細介紹的有關WCF應用編碼的相關介紹。

責任編輯:曹凱 來源: 博客園
相關推薦

2009-12-07 10:46:08

WCF框架

2010-02-22 16:19:25

WCF自托管

2010-02-24 15:20:23

WCF Message

2010-02-22 10:52:34

PDA訪問WCF

2009-12-21 16:04:45

WCF Dispose

2009-12-21 14:49:27

2010-06-23 15:41:44

Linux Bash

2009-12-03 17:17:32

軟路由配置

2010-02-22 17:07:50

WCF綁定元素

2010-02-25 15:25:19

WCF通道

2010-03-01 10:12:54

WCF異步操作

2010-02-23 14:17:20

WCF配置文件

2010-02-26 13:40:28

WCF消息頭

2010-02-24 13:06:27

WCF使用Nhiber

2010-03-02 09:39:11

保護WCF服務

2010-02-23 14:48:38

WCF事件通知

2010-02-23 13:03:34

WCF序列化

2010-02-24 13:48:44

MSMQ使用WCF

2009-12-22 14:54:52

WCF安全

2010-03-01 16:31:58

WCF實現SOA
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲国产精品一区二区第一页 | 久久99网 | 另类专区亚洲 | 亚洲www| 久久精品色欧美aⅴ一区二区 | 亚洲欧美一区二区三区国产精品 | 成人免费视频 | 国产精品久久久精品 | 欧美精品一区二区三区四区五区 | 国产毛片毛片 | 色啪网 | 精品蜜桃一区二区三区 | 免费视频99 | 国产真实精品久久二三区 | 精品视频久久久久久 | 在线免费观看成年人视频 | 精品国产乱码久久久久久影片 | 情侣黄网站免费看 | 日韩在线免费视频 | 国产h视频 | 瑞克和莫蒂第五季在线观看 | 久久亚洲一区二区 | 亚洲日本欧美日韩高观看 | 毛片网站在线观看视频 | 婷婷国产一区二区三区 | eeuss国产一区二区三区四区 | 亚洲在线一区二区 | 亚洲国产一区视频 | 成人在线观看中文字幕 | 精品国产乱码久久久久久图片 | 91精品中文字幕一区二区三区 | 中文字幕亚洲视频 | 久久精品一区二区 | 成人精品一区二区 | 亚洲欧洲综合av | 日韩喷潮| 九九亚洲精品 | 欧美日韩国产一区二区三区 | 欧美一级淫片免费视频黄 | 欧美日韩国产一区二区 | 亚洲国产一区二区三区四区 |