要向熟练运用WCF,首先需要掌握它的实际应用编码,才能正确的理解这一工具的应用特点。在这里我们将会为大家详细介绍一下WCF应用编码的相关代码编写,方便大家理解,让朋友们从中获得一些帮助。
成都创新互联于2013年成立,是专业互联网技术服务公司,拥有项目成都网站制作、网站建设、外贸网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元原平做网站,已为上家服务,为原平各地企业和个人服务,联系电话:18980820575
先来看看这段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(); - //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
.
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对象,然后通过WCF应用编码创建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中。#t#
3、WCF的客户端代理可以通过ChannelFactory来创建,只要为ChannelFactory
public class ContentReceiverClient : ClientBase
如:public class ContentReceiverClient : ClientBase
4、当使用ChannelFactory创建客户代理时请调用IDisposable方法关闭资源
using (iMonitor as IDisposable)如果使用Client : ClientBase
base.Channel.接口方法
则需要在调用完后Client.Close()关闭资源。
以上就是我们为大家详细介绍的有关WCF应用编码的相关介绍。
网站标题:WCF应用编码具体实现步骤讲解
文章出自:http://www.csdahua.cn/qtweb/news43/541843.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网