WCF应用编码具体实现步骤讲解
创始人
2024-06-23 12:21:04
0

要向熟练运用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();  
  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.
    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.CreateChannel 方法传入Binding和Address参数即可,当然也可以通过
public class ContentReceiverClient : ClientBase, T
如:public class ContentReceiverClient : ClientBase, IMonitor 方式创建

4、当使用ChannelFactory创建客户代理时请调用IDisposable方法关闭资源
using (iMonitor as IDisposable)如果使用Client : ClientBase, T 创建客户代理如:
base.Channel.接口方法
则需要在调用完后Client.Close()关闭资源。

以上就是我们为大家详细介绍的有关WCF应用编码的相关介绍。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...