【问题标题】:wcf callback, call client from outside the Service classwcf 回调,从 Service 类外部调用客户端
【发布时间】:2014-09-08 14:44:08
【问题描述】:

所以我有一个拥有多个客户端的 selfhostet 服务 (netTcpBinding)。 现在我想通过程序其他部分的回调调用客户端...

类似

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

(后来,我们订阅了客户端)...

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

实际上我有 2 个服务(Extern Rest/WS,Intern netTcp)正在运行,我想实现类似的东西:

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

如果你喜欢,我也可以添加一些代码。 问候

【问题讨论】:

    标签: c# wcf callback nettcpbinding


    【解决方案1】:

    好的,我以某种方式做到了(不太喜欢)。

    “实习生”客户所依赖的服务如下所示。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 2011-02-13
      • 1970-01-01
      • 2018-03-02
      • 2012-11-27
      • 1970-01-01
      相关资源
      最近更新 更多