扩展方法检测客户端队列是否存在

public static class QeuedServiceHelper
    {
        public static void VerifyQeues()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration
                (ConfigurationUserLevel.None);
            ServiceModelSectionGroup  group=ServiceModelSectionGroup.GetSectionGroup(config);
            foreach(ChannelEndpointElement endPoint in group.Client.Endpoints )
            {
                if (endPoint.Binding == "netMsmqBinding")
                {
                    string queue = GetQueueFromUri(endPoint.Address);
                    if (MessageQueue.Exists(queue) == false)
                    {
                        MessageQueue.Create(queue);
                    }
                }
            }
        }
    }
}

使用ConfigurationManager提供了类型安全的方法来解析配置文件。它会加载WCF的配置数据(在ServiceModel     SectionGroup里),然后遍历所有的终结点。对于每个使用MSMQ绑定的终结点,VerifyQueue()会在必要时创建队列

 

以上方法也可以简化为:

            QeuedServiceHelper.VerifyQeues();
            MyContractClient proxy = new MyContractClient();
            proxy.MyMeThod();
            proxy.close();

应该注意的是客户端在发出队列调用之前调用QeuedServiceHelper.VerifyQeues(); 一次

posted on 2014-10-29 11:23  黎木  阅读(209)  评论(0编辑  收藏  举报

导航