WCF應(yīng)用編碼具體實(shí)現(xiàn)步驟講解
要向熟練運(yùn)用WCF,首先需要掌握它的實(shí)際應(yīng)用編碼,才能正確的理解這一工具的應(yīng)用特點(diǎn)。在這里我們將會(huì)為大家詳細(xì)介紹一下WCF應(yīng)用編碼的相關(guān)代碼編寫,方便大家理解,讓朋友們從中獲得一些幫助。
先來看看這段WCF應(yīng)用編碼,然后再解說一下。
- 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("正在運(yùn)行的服務(wù)提供IMonitor功能..");
- };
- serviceHost.Open();
- while (true)
- {
- Console.WriteLine("服務(wù)正在運(yùn)行,要退出請(qǐng)鍵入exit");
- string cmd = Console.ReadLine();
- if (cmd == "exit")
- break;
- }
- }
- }
- static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)- {
- Console.WriteLine("剛才的操作發(fā)生異常,信息如下:");
- 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))) 初始化了一個(gè)ServiceHost對(duì)象,然后通過WCF應(yīng)用編碼創(chuàng)建ServiceEndpoint然后添加到ServiceHost對(duì)象中,根據(jù)ABC規(guī)則,ServiceEndpoint的創(chuàng)建最少需要傳入Contract、Binding、Address,例如:
- 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)介紹。