您的位置:知识库 » .NET技术

WCF分布式开发步步为赢系列的(4):WCF服务可靠性传输配置与编程开发

作者: Frank Xu Lei  来源: 博客园  发布时间: 2009-04-02 10:16  阅读: 5591 次  推荐: 0   原文链接   [收藏]  

   【3】编程方式实现可靠性传输:

 

     至于编程配置方式,这里我们稍作介绍,TCP绑定和WS绑定提供了略微不同的属性来配置可靠性。这里的例子我们继续使用的是针对TCP编程实现可靠性传输的示例代码:

//采用自托管方式,也可以是IIS、WAS,Windows服务等用户自定义程序托管服务
    public class WCFHost
    {
        
static void Main(string[] args)
        {

            
//反射方式创建服务实例,
            
//Using方式生命实例,可以在对象生命周期结束时候,释放非托管资源
            using (ServiceHost host = new ServiceHost(typeof(WCFService.WCFService)))
            {
                
//相同的服务注册多个基地址
                
//添加服务和URI,用户资源标识符
                Uri tcpAddress = new Uri("net.tcp://localhost:8001/WCFService");
                
//Uri httpAddress = new Uri("http://localhost:8002/WCFService");
                
//Uri pipeAddress = new Uri("net.pipe://localhost:8002/WCFService");
                SecurityMode securityMode = new SecurityMode();//实例化安全模型
                
//设置可靠性会话为真
                NetTcpBinding netTcpBinding = new NetTcpBinding(securityMode, true);
                
//添加服务终结点,方式不变
                host.AddServiceEndpoint(typeof(WCFService.IWCFService), netTcpBinding, tcpAddress);
                
//host.AddServiceEndpoint(typeof(WCFService.IWCFService), new WSHttpBinding(), httpAddress);
                
//host.AddServiceEndpoint(typeof(WCFService.IWCFService), new NetNamedPipeBinding(), pipeAddress);
                ////判断是否以及打开连接,如果尚未打开,就打开侦听端口
                if (host.State != CommunicationState.Opening)
                    host.Open();
                
//显示运行状态
                Console.WriteLine("Host is runing! and state is {0}", host.State);
                
//等待输入即停止服务
                Console.Read();


            }
        }

 

    SecurityMode securityMode = new SecurityMode();这个语句就是实例化安全模型,NetTcpBinding netTcpBinding = new NetTcpBinding(securityMode, true);传入的参数为true,就是启用可靠性传递。添加服务终结点,方式不变host.AddServiceEndpoint(typeof(WCFService.IWCFService), netTcpBinding, tcpAddress);这样就编程实现了TCP消息的可靠性传递。

0
0
标签:WCF

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻