日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
WCF應(yīng)用編碼具體實(shí)現(xiàn)步驟講解

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

成都創(chuàng)新互聯(lián)于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元原平做網(wǎng)站,已為上家服務(wù),為原平各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575

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

 
 
 
  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();  
  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.
    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、通過(guò)using (ServiceHost serviceHost = new ServiceHost(typeof(ServiceMonitor))) 初始化了一個(gè)ServiceHost對(duì)象,然后通過(guò)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可以通過(guò)serviceHost.Description.Behaviors.Add(behavior);把內(nèi)置或或自定義的Behavior添加到ServiceHost中。#t#

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

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

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


新聞標(biāo)題:WCF應(yīng)用編碼具體實(shí)現(xiàn)步驟講解
本文URL:http://m.5511xx.com/article/coeeecg.html