Esempio n. 1
0
File: Blip.cs Progetto: markvx/Blip
        /// <summary>
        /// Handle incoming data from the websocket.
        /// </summary>
        /// <remarks>Check it is valid, handle if RPC.</remarks>
        /// <param name="client">The client connection.</param>
        /// <param name="jsonMessage">The message payload as JSON.</param>
        private void HandleMessage(IWebSocketConnection client, String jsonMessage)
        {
            // Convert to BlipRequest.
            BlipRequest request = null;

            try
            {
                request = JsonConvert.DeserializeObject <BlipRequest>(jsonMessage);
                request.Validate();
            }
            catch (Exception e)
            {
                if (LogWarning != null)
                {
                    LogWarning(this, "Dropped bad Blip request from " + client.ConnectionInfo.ClientIpAddress);
                }
                return;
            }

            // Locate target delegate.
            Delegate target;

            if (!RegisteredServices.TryGetValue(request.Target, out target))
            {
                if (LogWarning != null)
                {
                    LogWarning(this, "Missing RPC registered handler for target from " + client.ConnectionInfo.ClientIpAddress);
                }
                return;
            }

            // Dynamic invoke.
            String responseJson = null;

            try
            {
                var result = target.DynamicInvoke(request.Arguments);
                responseJson = JsonConvert.SerializeObject(new BlipResponse()
                {
                    Target = request.Call, Success = true, Result = result
                });
            }
            catch (Exception e)
            {
                var err = e.InnerException;
                if (err != null)
                {
                    e = err;
                }
                responseJson = JsonConvert.SerializeObject(new BlipResponse()
                {
                    Target = request.Call, Success = false, Result = new { Message = e.Message, Stacktrace = e.StackTrace }
                });
            }

            // Pass it back.
            DispatchData(client, responseJson);
        }
Esempio n. 2
0
        public static object Resolve(Type type)
        {
            var serviceName = GetServiceRegistrationName(type);

            if (RegisteredServices.TryGetValue(serviceName, out var service))
            {
                return(service);
            }
            throw new Exception($"Service {serviceName} is not registered.");
        }
Esempio n. 3
0
 public static T Resolve <T>(string serviceName, bool dontThrow = false)
 {
     if (RegisteredServices.TryGetValue(serviceName, out var service))
     {
         return((T)service);
     }
     if (dontThrow)
     {
         return((T)(object)null);
     }
     throw new Exception($"Service {serviceName} is not registered.");
 }
Esempio n. 4
0
        public object ResolveService(Type type)
        {
            if (instances.TryGetValue(type, out object value))
            {
                return(value);
            }

            if (RegisteredServices.TryGetValue(type, out Type implementation))
            {
                ConstructorInfo[] infoCollection = GatherConstructors(implementation);
                return(ConstructService(implementation, infoCollection));
            }


            throw new ArgumentException(
                      $"No services are registered that implement the given type: {type.ToString()}");
        }
Esempio n. 5
0
        public static bool TryResolve(Type type, out object service)
        {
            var serviceName = GetServiceRegistrationName(type);

            return(RegisteredServices.TryGetValue(serviceName, out service));
        }