/// <summary> /// Adds the function handler to the service. All methods defined as MSB function via <see cref="MsbFunctionAttribute"/> will be added as <see cref="Function"/>s. /// </summary> /// <param name="functionHandler">Function handler that should be added.</param> public void AddFunctionHandler(AbstractFunctionHandler functionHandler) { if (this.registeredFunctionHandlerAndRelatedFunctions.ContainsKey(functionHandler)) { Log.Info($"Nothing to do, FunctionHandler '{functionHandler.GetType().FullName}' already registered to Service '{this.Uuid}'"); } else { List <Function> addedFunctions = new List <Function>(); foreach (var functionHandlerMethod in functionHandler.GetType().GetRuntimeMethods()) { if (!functionHandlerMethod.IsPublic || functionHandlerMethod.DeclaringType == typeof(object)) { continue; } Function function = new Function(functionHandler, functionHandlerMethod); this.AddFunction(function); addedFunctions.Add(function); } this.registeredFunctionHandlerAndRelatedFunctions.Add(functionHandler, addedFunctions); Log.Info($"FunctionHandler '{functionHandler.GetType().FullName}' registered to Service '{this.Uuid}'"); } }
private string GetFunctionIdFromHandler(AbstractFunctionHandler functionhandler, MethodInfo methodInfo) { var functionId = string.Empty; // Process path of IFunctionHandler var functionHandlerId = string.Empty; if (functionhandler.GetType().GetTypeInfo().GetCustomAttributes(typeof(MsbFunctionHandlerAttribute), true).FirstOrDefault() is MsbFunctionHandlerAttribute functionHandlerAttribute) { functionHandlerId = functionHandlerAttribute.Id; if (functionHandlerId.Equals(string.Empty)) { functionHandlerId = functionhandler.GetType().Name; Log.Warn("Id of MsbFunctionHandlerAttribute is not set. Name of type is used as id."); } } else { functionHandlerId += functionhandler.GetType().Name; } functionId += functionHandlerId; if (!functionId.EndsWith("/") && !functionId.StartsWith("/")) { functionId = functionId + "/"; } functionId += this.GetFunctionIdFromMethodInfo(methodInfo); return(functionId); }
/// <summary> /// Removes the function handler to the service. All MSB function that belong to this function handler will be removed. /// </summary> /// <param name="functionHandler">The function handler.</param> public void RemoveFunctionHandler(AbstractFunctionHandler functionHandler) { if (this.registeredFunctionHandlerAndRelatedFunctions.ContainsKey(functionHandler)) { List <Function> addedFunctions = this.registeredFunctionHandlerAndRelatedFunctions[functionHandler]; foreach (var function in addedFunctions) { this.RemoveFunction(function); } this.registeredFunctionHandlerAndRelatedFunctions.Remove(functionHandler); Log.Info($"FunctionHandler '{functionHandler.GetType().FullName}' removed from Service '{this.Uuid}'"); } else { Log.Info($"Nothing to do, FunctionHandler '{functionHandler.GetType().FullName}' not registered to Service '{this.Uuid}'"); } }