为整个应用程序保留一个 wcf 客户端代理 [英] Keeping one wcf client proxy for whole app

查看:28
本文介绍了为整个应用程序保留一个 wcf 客户端代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有高负载的 ASP .NET MVC2 网站和该网站使用的 WCF 服务.早期我每次需要时都会创建一个代理,甚至没有关闭它.请参阅我的上一个问题(非常感谢 SO 用户 Richard Blewett)我发现我应该关闭这个代理.以其他方式它将成功会话限制.

I have highload ASP .NET MVC2 website and WCF service that site uses. Early I created one proxy every time I need it and even didn't close it. Refer to my previous question (with my big thanks for SO user Richard Blewett) I found out that I should close this proxy. In other way it will succeed sessions limit.

现在,我在应用程序启动时创建代理,然后只需检查它并在需要时重新创建它.所以,这是代码:

Now, I'm creating proxy one time app starts and then just check it and recreate it if is needed. So, here is the code:

public static bool IsProxyValid(MyServ.MyService client) {
    bool result = true;
    if ((client == null) || (client.State != System.ServiceModel.CommunicationState.Opened) // || (client.InnerChannel.State != CommunicationState.Opened)
        )            
        result = false;

    return result;
}

public static AServ.AServClient GetClient(HttpContext http) {            
    if (!IsProxyValid((MyService)http.Application["client"]))
        http.Application["client"] = new MyService();
    return (MyService)http.Application["client"];
}

public static MyServ.MyService GetClient(HttpContextBase http)
{
    if (!IsProxyValid((MyService)http.Application["client"]))
        http.Application["client"] = new MyService();
    return (MyService)http.Application["client"];
}

public ActionResult SelectDepartment(string departments)
    {
       try
        {
            MyService svc = CommonController.GetClient(this.HttpContext);                
            Department[] depsArray = svc.GetData(departments);

            // .... I cut here ....

            return View();
        }
        catch (Exception exc)
        {
            // log here                
            return ActionUnavailable();
        }
    }

那么,你们怎么看?它应该正常工作吗?有时我的应用程序卡住了.我认为这是因为客户端代理状态确定不正确,应用程序尝试使用损坏的代理.

So, what do you guys think about it? Should it work properly? Sometimes my app stucked. I think it is because client proxy state determines uncorrectly and app tries to use broken proxy.

发布编辑

也在 TCP Monitor 中,我看到了很多从站点到服务的已建立连接.为什么它会创建很多连接而不是使用一个全局连接?也许在调用服务方法时发生了一些异常使其成为错误状态?

Also in TCP Monitor I see a lot of established connections from site to service. Why it creates a lot of connectiong insteads of using one global? Maybe some exception occured while invoking service method makes it faulted state?

希望得到你们的帮助!

推荐答案

我认为如果频道出现故障,您需要在创建新频道之前中止频道,并且如果您创建新客户端,请确保关闭/中止旧客户端,为此使用类似的东西(这个在单例中与 DI 一起使用)

I think you need to abort the channel if it gets faulted before creating a new one and Make sure to close/ abort old client if you creating the new client, use something like this for that (this one is used with DI in singleton)

public class MyServiceClientInitializer : IMyServiceClientInitializer
 {
        [ThreadStatic]
        private static MyServ.MyService _client;

        public MyServ.MyService Client
        {
            get
            {
                if (_client == null
                    || (_client.State != CommunicationState.Opened
                            && _client.State != CommunicationState.Opening))
                    IntializeClient();

                return _client;
            }
        }

        private void IntializeClient()
        {
            if (_client != null)
            {
                if (_client.State == CommunicationState.Faulted)
                {
                    _client.Abort();
                }
                else
                {
                    _client.Close();    
                }
            }

            string url = //get url;

            var binding = new WSHttpBinding();
            var address = new EndpointAddress(url);

            _client = new MyServ.MyService(binding, address);            
        }
}

这篇关于为整个应用程序保留一个 wcf 客户端代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆