2014-09-08 4 views
1

그래서 여러 클라이언트가있는 자체 호스트 서비스 (netTcpBinding)가 있습니다. 지금은 ...wcf 콜백, 서비스 클래스 외부에서 클라이언트를 호출

뭔가 (이후, 우리가 가입 한 고객)

ServiceHost shintern = new ServiceHost(typeof(InternalService)); 
shintern.Open(); 

처럼 ... 프로그램의 다른 부분에서 콜백을 통해 클라이언트를 호출하고 싶은

shintern.GetClients().ForEach(...client_function()); 

Actualy은 내가 실행 2 개 서비스 (통근 휴식/WS, 인턴 netTcp)가 그리고 난 같은 것을 구현하고 싶습니다 :

ServiceExtern::GetSomethingFromInternClients() 
    { 
     //return values of clients connected to intern Service. 
    } 

마음에 드시면 일부 코드도 추가 할 수 있습니다. 인사말

답변

2

오케이, 나는 그것을 어떻게 든 해냈다.

"인턴"클라이언트가 서성 거리는 서비스는 다음과 같습니다.

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)] 
    public class pwGateInternalService : IpwGateInternalService 
    { 
     static List<SchoolCallback> m_schools = new List<SchoolCallback>(); 

     //non contract Functions: 

     public List<SchoolCallback> GetClientList() 
     { 
      return m_schools; 
     } 

     //Contract Functions: 

     public ServiceStatus Connect(string schoolname) 
     { 
      ServiceStatus result = new ServiceStatus(); 
      int schoolid = Config.GetIdentifier(schoolname); 

      //add to dynamic list of schools 
      IpwGateInternalCallback callback = OperationContext.Current.GetCallbackChannel<IpwGateInternalCallback>(); 
      if (m_schools.Find(x => x.callback == callback) == null) 
      { 
       SchoolCallback school = new SchoolCallback(schoolid, schoolname, callback); 
       m_schools.Add(school); 

       result.status = eStatus.success; 
      } 
      else 
      { 
       //already found 
       result.status = eStatus.error; 
       result.strError = "a client with your name is already connected"; 

       //TODO 
       //mail? 
      } 
      return null; 
     } 

     public void Disconnect() 
     { 
      //remove from dynamic list 
      IpwGateInternalCallback callback = OperationContext.Current.GetCallbackChannel<IpwGateInternalCallback>(); 
      SchoolCallback school = m_schools.Find(x => x.callback == callback); 
      if (school != null) 
      { 
       m_schools.Remove(school); 
      } 


     }//Disconnect() 
    }//callback interface 

어디에 - 나는 클라이언트 목록에 접근하고자 할 때마다 나는이 수행

pwGateInternalService internService = new pwGateInternalService(); 
List<SchoolCallback> schools = internService.GetClientList(); 
SchoolCallback school = schools.Find(x => x.identifier == targetschool.identifier); 

if (school != null) 
{ 
    user = school.callback.GetUser(username); 
}