public static async Task <object> InvokeMethod(ConnectionSocketDataModel userConnectionData, ReceiveSocketDataModel receiveDataModel) { try { var channel = GetChannelList().Where(w => w.ChannelSlugUrl == userConnectionData.ChannelSlugUrl).FirstOrDefault(); var mainTypeData = Type.GetType(channel.ChannelClassFullName); var mainParamConstructor = SummonParameter(mainTypeData); var mainConstructor = mainTypeData.GetConstructors().FirstOrDefault(); var mainConstructorDeclare = mainConstructor.Invoke(mainParamConstructor); #region SetPropertieValueAfterNewInstance var hubContext = mainTypeData.GetProperty(nameof(Hub.Context)); var hubContextInstance = Activator.CreateInstance(hubContext.PropertyType, userConnectionData.ConnectionId, userConnectionData.ChannelSlugUrl); //mainConstructorDeclare is same a class varaible new instance hubContext.SetValue(mainConstructorDeclare, hubContextInstance as HubContext); #endregion var mainMethod = mainTypeData.GetMethod(receiveDataModel.InvokeMethodName); var IsNotReturn = (mainMethod.ReturnType == typeof(void) || mainMethod.ReturnType == typeof(Task)); object response = null; if (IsAsyncMethod(mainMethod, receiveDataModel.InvokeMethodName)) { // check invoke async dynamic invokeAsync = mainMethod.Invoke(mainConstructorDeclare, receiveDataModel.MessageJson); if (IsNotReturn) { await invokeAsync; } else { response = await invokeAsync; } } else { if (IsNotReturn) { mainMethod.Invoke(mainConstructorDeclare, receiveDataModel.MessageJson); } else { response = mainMethod.Invoke(mainConstructorDeclare, receiveDataModel.MessageJson); } } return(response); } catch (Exception e) { //TODO : log return(e); } }
protected static ConnectionSocketDataModel SetConnectionSocketList(string channelSlugUrl, string connectionId, WebSocket webSocket) { var newConnectionData = new ConnectionSocketDataModel { ChannelSlugUrl = channelSlugUrl, ConnectionId = connectionId, WebSocket = webSocket }; ConnectionSocketList.Add(newConnectionData); return(newConnectionData); }