Skip to content

OperationContext and Broadcasting and clients managements in server side

Ali Yousefi edited this page Feb 23, 2019 · 6 revisions

OperationContext is a static class that help you to manage clients and context of client for example you can save an object with OperationContext per user and use that object to all of methods, or you can query to client and get callbacks of another client you can get all clients per another client call or another plan (from a thread or task method of server).

            //get all connected clients
            var allClients = OperationContext.Current.AllServerClients;
            //get current client called your server methods
            var oneClient = OperationContext.Current.Client;
            //get all clients with same thread
            var client = OperationContext.Current.Clients;

            //get all callbacks of client that you can broadcast to them
            var allCallbacks = OperationContext.Current.GetAllClientServices<IClientCallbackInterface>();

            //get all callbacks of client that you can broadcast to them but not current client called this method
            var allCallbacksButMe = OperationContext.Current.GetAllClientServicesButMe<IClientCallbackInterface>();

            //get all callbacks context (with clientinfo,session and ip and etc) of client that you can broadcast to them but not current client called this method
            var allCallbacksButMeContext = OperationContext.Current.GetAllClientServicesButMe<IClientCallbackInterface>();

            //get all callbacks context (with clientinfo,session and ip and etc) of client that you can broadcast to them
            var allCallbacksContext = OperationContext.Current.GetAllClientClientContextServices<IClientCallbackInterface>();

            //get current client callback or get client callback by session or client info
            var allCurrentCallbacks = OperationContext.Current.GetClientService<IClientCallbackInterface>();

            //get current client callback context (with clientinfo,session and ip and etc) or get client callback by session or client info
            var allCurrentCallbacksContext = OperationContext.Current.GetClientContextService<IClientCallbackInterface>();

            //get all callbacks of clients with linq query that you can broadcast to them but not current client called this method
            var queryCallbacks = OperationContext.Current.GetListOfClientServices<IClientCallbackInterface>();

            //get all callbacks context (with clientinfo,session and ip and etc) of clients with linq query that you can broadcast to them but not current client called this method
            var queryCallbacksContext = OperationContext.Current.GetListOfClientContextServices<IClientCallbackInterface>();

            //get all callbacks context (with clientinfo,session and ip and etc) of clients with ignore parameter linq query (you will take client that is not exist list parameter) broadcast to them
            var withoutQueryCallbacksContext = OperationContext.Current.GetListOfExcludeClientContextServices<IClientCallbackInterface>();
            //get all callbacks context of clients with ignore parameter linq query (you will take client that is not exist list parameter) broadcast to them
            var withoutQueryCallbacks = OperationContext.Current.GetListOfExcludeClientServices<IClientCallbackInterface>();

You can get a single instance service with this method (single instance is used on ServiceContractAttribute):

var service = OperationContext.Current.GetService<YourServiceClass>();

Set a setting and get a setting of client calls

Use this code to set and get setting:

//for example set it into your client login method
OperationContext<TestSetting>.CurrentSetting = new TestSetting() { Name = yourName };
//and get client setting from another method you know who called your methods another exampleT user name or user id in database
var name= OperationContext<TestSetting>.Name;

Like:

 public class TestSetting
    {
        public string Name { get; set; }
    }

    public class TestServerModel : ITestServerModel
    {
        public Tuple<string> HelloWorld(string yourName)
        {
            OperationContext<TestSetting>.CurrentSetting = new TestSetting() { Name = yourName };

            return new Tuple<string>("hello: " + yourName);
        }

        public Tuple<string> WhoAmI()
        {
            return new Tuple<string>("you are : " + OperationContext<TestSetting>.CurrentSetting.Name);
        }
    }

Note: get OperationContext from async methods:

1. get the context at beginning of method

OperationContext currentContext = OperationContext.Current;

2. then use

OperationContextBase.SetSetting(new CurrentUserInfo() { IsAdmin = user.IsAdmin, Key = Guid.NewGuid().ToString() }, currentContext);

Why? because async await methods always running in another thread, if you use OperationContext.Current this will return null because signalgo will lost Current context in another thread,so we need to save current context from first line of method for use later.

what is CurrentUserInfo? read here: OperationContext In Http Protocol and Http Headers