2012-02-20 2 views
1

사이트에서 사용하는 ASP .NET MVC2 웹 사이트 및 WCF 서비스가 많습니다. 조기에 나는 필요할 때마다 하나의 프록시를 만들었고 심지어 그것을 닫지도 않았다. 내 previous question (SO 사용자 Richard Blewett에 대한 커다란 고마움과 함께)을 참조하십시오.이 프록시를 닫아야한다는 것을 알았습니다. 다른 방법으로 세션 제한을 계승합니다.전체 응용 프로그램에 대해 하나의 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 편집 또한 TCP 모니터에서 나는 사이트에서 서비스에 설정된 연결을 많이 참조하십시오. 왜 그것은 하나의 글로벌을 사용하는 대신 많은 연결을 생성합니까? 어쩌면 서비스 메소드를 호출하는 것이 상태를 잘못 만드는 동안 일부 예외가 발생했을 수 있습니까?

귀하의 도움이 필요하십니까?

답변

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

솔루션을 사용해 보았지만 그 중 하나가 아니라 많은 연결이 있습니다. 내가 뭘 잘못하고있어? – kseen

관련 문제