2012-02-20 4 views
1

サイトで使用する高負荷のASP .NET MVC2 WebサイトとWCFサービスがあります。早い時期に私は必要なたびにプロキシーを作成し、それを閉じなかった。私のprevious questionを参照してください(私の大変ありがとうSOユーザRichard Blewett)私はこのプロキシを閉じるべきであることを知りました。それ以外の方法では、セッションの制限に成功します。アプリケーション全体に対して1つのwcfクライアントプロキシを維持する

今、私はプロキシを作成していますが、アプリが起動してから、それをチェックし、必要に応じて再作成します。コードは次のとおりです:

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(); 
     } 
    } 

だから、あなたはどう思いますか?それは正常に動作するはずですか?ときどき私のアプリが固執しました。クライアントプロキシの状態が正しく決定されず、アプリケーションが壊れたプロキシを使用しようとしているからだと私は思う。


POSTのEDITはまた、TCPモニタに私は、サイトからのサービスへの確立された接続の多くを参照してください。なぜそれは1つのグローバルを使用する代わりにたくさんのconnectiongを作成するのですか?たぶんサービスメソッドを呼び出すと何らかの例外が発生して、状態にフォールト状態になることがありますか?

あなたのお役に立ってください!

答えて

1

新しいチャネルを作成する前に障害が発生した場合は、チャネルを中止する必要があると思います。 新しいクライアントを作成する場合は、古いクライアントを終了/中止してください。 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);    
     } 
} 
+0

OK、ありがとうございました! 'client.InnerChannel.State'を' client.State'または 'client.State'だけでチェックするべきですか? – kseen

+0

client.Stateはうまくいくはず –

+0

私はあなたのソリューションを試しましたが、ただ1つではなくたくさんの接続があります。私は間違っているの? – kseen

関連する問題