[英]wcf callback, call client from outside the Service class
所以我有一個具有多個客戶端的selfhostet服務(netTcpBinding)。 現在我想通過程序其他部分的回調來調用客戶端...
就像是
ServiceHost shintern = new ServiceHost(typeof(InternalService));
shintern.Open();
(后來,我們已經訂閱了客戶)...
shintern.GetClients().ForEach(...client_function());
實際上,我有2個服務(Extern Rest / WS,實習生netTcp)正在運行,我想實現以下功能:
ServiceExtern::GetSomethingFromInternClients()
{
//return values of clients connected to intern Service.
}
如果您願意,我也可以添加一些代碼。 問候
好的,我以某種方式做到了(不要太喜歡它)。
“實習生”客戶端所處的服務看起來像這樣。
[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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.