grunge
grunge

Reputation: 135

wcf callback, call client from outside the Service class

So i have a selfhostet Service (netTcpBinding) that has multiple clients. Now i would like to call the client via callback from a other part of the program...

something like

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

(later, we have subscribed clients)...

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

Actualy i have 2 Services (Extern Rest/WS, Intern netTcp) running and i would like to implement something like:

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

If you like, i can add some code as well. Greetings

Upvotes: 1

Views: 307

Answers (1)

grunge
grunge

Reputation: 135

okay, i did it somehow (dont like it much).

The Service where the "intern" client are hagning on looks like this.

[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

Where- and whenever i like to access the list of clients i do this:

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

Upvotes: 2

Related Questions