惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
D
DataBreaches.Net
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
Recent Announcements
Recent Announcements
The Register - Security
The Register - Security
S
Secure Thoughts
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
V2EX - 技术
V2EX - 技术
腾讯CDC
GbyAI
GbyAI
G
Google Developers Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
Jina AI
Jina AI
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
Help Net Security
Help Net Security
K
Kaspersky official blog
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
AI
AI
MongoDB | Blog
MongoDB | Blog
Scott Helme
Scott Helme
J
Java Code Geeks
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
H
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
S
Security Affairs
TaoSecurity Blog
TaoSecurity Blog
The GitHub Blog
The GitHub Blog
Hacker News: Ask HN
Hacker News: Ask HN
Martin Fowler
Martin Fowler
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI

博客园 - ruinet

使用MVP模式实现B/S和C/S平台的功能通用 WCF中使用扩展行为来验证连接的用户 Microsoft.Practices.Unity依赖注入使用实例 简洁的Asp.net菜单控件 Windows Mobile无线打印的实现 使用HTML,CSS快速导出数据到Excel 软件技术网站精选 CakePHP架构入门 升级Sql Server 2000到Sql Server 2005中要注意的问题 asp.net web开发综合技能 编写第一个Silverlight程序 Saas学习 在Windows Mobile上控制输入法 - ruinet - 博客园 在Windows Mobile创建桌面快捷方式 在仿真设备中使用主机网络 CSS,JavaSript,Html实用小代码 重启PocketPC移动设备 使用Ajax控件引发性能问题 智能移动项目打包发布经验交流
WCF通用服务请求类
ruinet · 2010-09-12 · via 博客园 - ruinet

2010-09-12 22:58  ruinet  阅读(572)  评论()    收藏  举报

类代码:

public static class ServiceRequester<T>
{
        private static IDictionary<string, ChannelFactory<T>> _channelPool= new Dictionary<string, ChannelFactory<T>>();

        private static object ayncLock = new object();
        static private ChannelFactory<T> GetChannelFactory(string endPointName)
        {
            lock (ayncLock)
            {
                string cacheName = endPointName;
                if (string.IsNullOrEmpty(endPointName))
                {
                    cacheName = typeof(T).Name;
                }
                ChannelFactory<T> _factory = null;
                _channelPool.TryGetValue(cacheName, out _factory);
                if (_factory == null)
                {
                    _factory = new ChannelFactory<T>(endPointName);
                    _channelPool.Add(endPointName, _factory);
                }
                return _factory;
            }
        }
        static public void Request(string endPointName, Action<T> action)
        {
            IClientChannel proxy = null;
            try
            {
                lock (ayncLock)
                {
                    proxy = GetChannelFactory(endPointName).CreateChannel() as IClientChannel;
                    proxy.Open();
                    action((T)proxy);
                    proxy.Close();
                }
             }
            catch (CommunicationException communicationException)
            {
                if (proxy != null)
                {
                    proxy.Abort();
                }
                throw communicationException;
            }
            catch (TimeoutException timeoutException)
            {
                if (proxy != null)
                {
                    proxy.Abort();
                }
                throw timeoutException;
            }
            catch (Exception ex)
            {
                if (proxy != null)
                {
                    proxy.Abort();
                }
                throw ex;
            }
        }
}

使用:

 ServiceRequester<ICalculateService>.Request("*", delegate(ICalculateService proxy)
            {
                proxy.Add(1, 4,1);
            });