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 10:52:34

PDA访问WCF

2010-02-24 15:20:23

WCF Message

2010-02-22 16:19:25

WCF自托管

2009-12-21 16:04:45

WCF Dispose

2009-12-21 14:49:27

2009-12-03 17:17:32

软路由配置

2010-06-23 15:41:44

Linux Bash

2010-02-22 17:07:50

WCF绑定元素

2010-02-25 15:25:19

WCF通道

2010-03-01 10:12:54

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 14:17:20

WCF配置文件

2010-02-26 13:40:28

WCF消息头

2010-02-23 13:03:34

WCF序列化

2010-03-01 16:31:58

WCF实现SOA

2010-03-01 14:56:48

WCF服务引用

2010-02-24 13:48:44

MSMQ使用WCF
点赞
收藏

51CTO技术栈公众号