WCF应用的编码实现

先来看看代码,然后再解说一下。
    class Program
    
{
        
static void Main(string[] args)
        
{
            AppDomain.CurrentDomain.UnhandledException 
+= new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            
using (ServiceHost serviceHost = new ServiceHost(typeof(ServiceMonitor)))
            
{
                NetNamedPipeBinding binding 
= new NetNamedPipeBinding();
                binding.Security.Mode 
= NetNamedPipeSecurityMode.None;
                binding.ReceiveTimeout 
= TimeSpan.Parse("00:00:05");
                binding.MaxReceivedMessageSize 
= 6553600;
                binding.ReaderQuotas.MaxStringContentLength 
= 6553600;

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

                
//ServiceMetadataBehavior behavior = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
                
//if (behavior == null)
                
//{
                
//    behavior = new ServiceMetadataBehavior();
                
//    serviceHost.Description.Behaviors.Add(behavior);
                
//}

                serviceHost.Opened 
+= delegate
                
{
                    Console.WriteLine(
"正在运行的服务提供IMonitor功能..");
                }
;
                serviceHost.Open();

                
while (true)
                
{
                    Console.WriteLine(
"服务正在运行,要退出请键入exit");
                    
string cmd = Console.ReadLine();
                    
if (cmd == "exit")
                        
break;
                }

            }

        }


        
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        
{
            Console.WriteLine(
"刚才的操作发生异常,信息如下:");
            Console.Write(e.ToString());
        }

    }



    [ServiceContract]
    
public interface IMonitor
    
{
        [OperationContract]
        
void Record(string key, string value);
    }


    
public class ServiceMonitor : IMonitor
    
{
        
public void Record(string key, string value)
        
{
            Console.WriteLine(
string.Format("Key = {0}", key));
            Console.WriteLine(
string.Format("Value = {0}", value));
            Console.WriteLine(
new string('*'50));
        }

    }


    
public static class ServiceMonitorClientManager
    
{
        
public static void Record(string key, string value)
        
{
            
try
            
{
                EndpointAddress address 
= new EndpointAddress("net.pipe://localhost/ServiceMonitor");
                NetNamedPipeBinding binding 
= new NetNamedPipeBinding();
                binding.Security.Mode 
= NetNamedPipeSecurityMode.None;
                binding.SendTimeout 
= TimeSpan.Parse("00:00:01");
                binding.ReaderQuotas.MaxStringContentLength 
= 6553600;
                binding.MaxReceivedMessageSize 
= 6553600;

                IMonitor iMonitor 
= ChannelFactory<IMonitor>.CreateChannel(binding, address);
                
using (iMonitor as IDisposable)
                
{
                    iMonitor.Record(key, value);
                }

            }

            
catch (System.ServiceModel.CommunicationObjectFaultedException) { }
            
catch (System.ServiceModel.EndpointNotFoundException) { }
        }

    }

1、通过using (ServiceHost serviceHost = new ServiceHost(typeof(ServiceMonitor))) 初始化了一个ServiceHost对象,然后通过编码创建ServiceEndpoint然后添加到ServiceHost对象中,根据ABC规则,ServiceEndpoint的创建最少需要传入Contract、Binding、Address,例如:
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中。

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()关闭资源。
原文地址:https://www.cnblogs.com/chenjunbiao/p/1760212.html