Example #1
0
    private void StartReceiving()
    {
        Task.Run(() =>
        {
            foreach (var msg in Mailbox.GetConsumingEnumerable())
            {
                if (!TypeHandlers.ContainsKey(msg.GetType()))
                {
                    System.UnhandledMessages.Tell(new ActorSystem.UnhandledMessage()
                    {
                        Message = msg
                    });
                    continue;
                }

                try
                {
                    TypeHandlers[msg.GetType()](msg);
                }
                catch (System.Exception ex)
                {
                    Parent.Tell(ex);
                }
            }
        });
    }
Example #2
0
        /// <summary>
        /// Registers a deserializer for item type-specific data.
        /// </summary>
        ///
        /// <param name="typeId">
        /// The unique identifier for the item type-specific data as defined
        /// by the HealthVault service.
        /// </param>
        ///
        /// <param name="itemTypeClass">
        /// The class that implements the item type-specific data. It must
        /// be public, derive from <see cref="ThingBase"/>, and
        /// have a default constructor.
        /// </param>
        ///
        /// <param name="overwriteExisting">
        /// <b>true</b> to register the new deserializer even if the type
        /// already has a deserializer registered; <b>false</b> to throw an
        /// exception because a deserializer is already registered.
        /// </param>
        ///
        /// <exception cref="ArgumentException">
        /// The <paramref name="typeId"/> parameter is <see cref="System.Guid.Empty"/> or
        /// the <paramref name="itemTypeClass"/> parameter does not derive from
        /// <see cref="ThingBase"/>.
        /// </exception>
        ///
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="itemTypeClass"/> parameter is <b>null</b>.
        /// </exception>
        ///
        /// <exception cref="TypeHandlerAlreadyRegisteredException">
        /// The <paramref name="typeId"/> parameter already has a handler
        /// registered and <paramref name="overwriteExisting"/> is <b>false</b>.
        /// </exception>
        ///
        public static void RegisterTypeHandler(
            Guid typeId,
            Type itemTypeClass,
            bool overwriteExisting = false)
        {
            if (typeId == Guid.Empty)
            {
                throw new ArgumentException(Resources.TypeIdGuidEmpty, nameof(typeId));
            }

            Validator.ThrowIfArgumentNull(itemTypeClass, nameof(itemTypeClass), Resources.ThingTypeClassNull);

            if (!itemTypeClass.GetTypeInfo().IsSubclassOf(typeof(ThingBase)))
            {
                throw new ArgumentException(Resources.TypeClassNotThing, nameof(itemTypeClass));
            }

            if (TypeHandlers.ContainsKey(typeId) && !overwriteExisting)
            {
                throw new TypeHandlerAlreadyRegisteredException(Resources.TypeHandlerAlreadyRegistered);
            }

            TypeHandlers[typeId] = new ThingTypeHandler(typeId, itemTypeClass);
            s_typeHandlersByClassName[itemTypeClass.Name] = TypeHandlers[typeId];
        }
 /// <summary>
 /// Process a constant expression, if possible. Some forms of constant expressions
 /// are actually just stubs for dealing with something that needs to be injected into
 /// the process.
 /// </summary>
 /// <param name="expression"></param>
 /// <returns></returns>
 protected override Expression VisitConstant(ConstantExpression expression)
 {
     if (TypeHandlers == null)
     {
         throw new InvalidOperationException("Can't visit a constant expression unless the typehandlers have been initalized");
     }
     return(TypeHandlers.ProcessConstantReferenceAsExpression(expression, MEFContainer));
 }
Example #4
0
 public SerializationContext()
 {
     DateTimeOptions = new DateTimeOptions();
     EnumOptions     = new EnumOptions();
     ArrayOptions    = new ArrayOptions();
     MapOptions      = new MapOptions();
     JsonOptions     = new JsonOptions();
     TypeHandlers    = new TypeHandlers(this);
 }
Example #5
0
        /// <summary>
        /// Gets the <see cref="Type"/> instance of the class that is registered to
        /// handle this type id.
        /// </summary>
        /// <remarks>
        /// This method looks up the type id in the list of types that the SDK understands how
        /// to process. If new types were added to the HealthVault service since this SDK was
        /// released, this method will not return them.
        ///
        /// To retrieve information about the types from the HealthVault service,
        /// use the <see cref="GetHealthRecordItemTypeDefinitionAsync(IConnectionInternal)"/> method.
        /// </remarks>
        /// <param name="typeId">The ID of the associated type</param>
        /// <returns>The typeId.</returns>
        public static Type GetRegisteredTypeForTypeId(Guid typeId)
        {
            if (TypeHandlers.ContainsKey(typeId))
            {
                ThingTypeHandler itemTypeHandler = TypeHandlers[typeId];
                return(itemTypeHandler.ItemTypeClass);
            }

            return(null);
        }
Example #6
0
        internal static ThingBase DeserializeItem(XPathNavigator thingNav)
        {
            ThingBase result;
            Guid      typeId = GetTypeId(thingNav);

            ThingTypeHandler handler = null;

            if (typeId == s_applicationSpecificId)
            {
                // Handle application specific health item records by checking for handlers
                // for the application ID and subtype tag. If the handler doesn't exist
                // the default handler will be picked up below.

                AppDataKey appDataKey = GetAppDataKey(thingNav);

                if (appDataKey != null)
                {
                    if (s_appSpecificHandlers.ContainsKey(appDataKey.AppId))
                    {
                        if (s_appSpecificHandlers[appDataKey.AppId].ContainsKey(appDataKey.SubtypeTag))
                        {
                            handler =
                                s_appSpecificHandlers[appDataKey.AppId][appDataKey.SubtypeTag];
                        }
                    }
                }
            }

            if (handler == null &&
                TypeHandlers.ContainsKey(typeId))
            {
                handler = TypeHandlers[typeId];
            }

            if (handler != null)
            {
                result = (ThingBase)Activator.CreateInstance(handler.ItemTypeClass);
            }
            else
            {
                result = new ThingBase(typeId);
            }

            result.ParseXml(thingNav, thingNav.OuterXml);

            return(result);
        }
            /// <summary>
            /// Some method is being called. Offer plug-ins a chance to transform this method call.
            /// </summary>
            /// <param name="expression"></param>
            /// <returns></returns>
            protected override Expression VisitMethodCall(MethodCallExpression expression)
            {
                //
                // Give the various type handlers a chance to alter the expression call if they want.
                //

                var expr = TypeHandlers.ProcessMethodCall(expression, GeneratedCode, CodeContext, MEFContainer);

                //
                // If it is still an expression call, then we need to allow the arguments to transform.
                //

                if (expr is MethodCallExpression)
                {
                    var mc = expr as MethodCallExpression;

                    var transformedArgs = from a in mc.Arguments
                                          select a.Resolve(GeneratedCode, CodeContext, MEFContainer);

                    expr = Expression.Call(mc.Object, mc.Method, transformedArgs);
                }

                return(expr);
            }
Example #8
0
 public SmartSqlBuilder AddTypeHandler(TypeHandler typeHandler)
 {
     TypeHandlers.Add(typeHandler);
     return(this);
 }
Example #9
0
 internal static bool HasTypeHandler(Type type) => TypeHandlers.ContainsKey(type);