自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF應(yīng)用編碼具體實(shí)現(xiàn)步驟講解

開發(fā) 開發(fā)工具
WCF應(yīng)用編碼的實(shí)際操作方法比較復(fù)雜,不過,我們可以通過不斷的實(shí)踐來進(jìn)行熟練的應(yīng)用。在這篇文章中將會(huì)針對(duì)此做一個(gè)詳細(xì)介紹。

要向熟練運(yùn)用WCF,首先需要掌握它的實(shí)際應(yīng)用編碼,才能正確的理解這一工具的應(yīng)用特點(diǎn)。在這里我們將會(huì)為大家詳細(xì)介紹一下WCF應(yīng)用編碼的相關(guān)代碼編寫,方便大家理解,讓朋友們從中獲得一些幫助。

先來看看這段WCF應(yīng)用編碼,然后再解說一下。

  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("正在運(yùn)行的服務(wù)提供IMonitor功能..");  
  23. };  
  24. serviceHost.Open();  
  25. while (true)  
  26. {  
  27. Console.WriteLine("服務(wù)正在運(yùn)行,要退出請(qǐng)鍵入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("剛才的操作發(fā)生異常,信息如下:");  
  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))) 初始化了一個(gè)ServiceHost對(duì)象,然后通過WCF應(yīng)用編碼創(chuàng)建ServiceEndpoint然后添加到ServiceHost對(duì)象中,根據(jù)ABC規(guī)則,ServiceEndpoint的創(chuàng)建最少需要傳入Contract、Binding、Address,例如:

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

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

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

4、當(dāng)使用ChannelFactory創(chuàng)建客戶代理時(shí)請(qǐng)調(diào)用IDisposable方法關(guān)閉資源
using (iMonitor as IDisposable)如果使用Client : ClientBase<T>, T 創(chuàng)建客戶代理如:
base.Channel.接口方法
則需要在調(diào)用完后Client.Close()關(guān)閉資源。

以上就是我們?yōu)榇蠹以敿?xì)介紹的有關(guān)WCF應(yīng)用編碼的相關(guān)介紹。

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2009-12-07 10:46:08

WCF框架

2010-02-22 10:52:34

PDA訪問WCF

2010-02-22 16:19:25

WCF自托管

2010-02-24 15:20:23

WCF Message

2009-12-21 16:04:45

WCF Dispose

2009-12-03 17:17:32

軟路由配置

2009-12-21 14:49:27

2010-06-23 15:41:44

Linux Bash

2010-03-01 10:12:54

WCF異步操作

2010-02-22 17:07:50

WCF綁定元素

2010-02-25 15:25:19

WCF通道

2010-03-02 09:39:11

保護(hù)WCF服務(wù)

2010-02-24 13:06:27

WCF使用Nhiber

2010-02-23 14:48:38

WCF事件通知

2010-02-23 14:17:20

WCF配置文件

2010-02-26 13:40:28

WCF消息頭

2010-03-01 14:56:48

WCF服務(wù)引用

2010-03-01 16:31:58

WCF實(shí)現(xiàn)SOA

2010-02-24 13:48:44

MSMQ使用WCF

2010-02-23 13:03:34

WCF序列化
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)