示例#1
0
        /// <summary>
        /// Adds serializer support.
        /// </summary>
        /// <param name="services">The service collection.</param>
        /// <param name="configure">The configuration delegate.</param>
        /// <returns>The service collection.</returns>
        public static IServiceCollection AddSerializer(this IServiceCollection services, Action <ISerializerBuilder> configure = null)
        {
            // Only add the services once.
            var context = GetFromServices <ConfigurationContext>(services);

            if (context is null)
            {
                context = new ConfigurationContext(services);
                foreach (var asm in ReferencedAssemblyHelper.GetRelevantAssemblies(services))
                {
                    context.Builder.AddAssembly(asm);
                }

                services.Add(context.CreateServiceDescriptor());
                services.AddOptions();
                services.AddSingleton <IConfigureOptions <TypeManifestOptions>, DefaultTypeManifestProvider>();
                services.AddSingleton <TypeResolver, CachedTypeResolver>();
                services.AddSingleton <TypeConverter>();
                services.TryAddSingleton(typeof(ListActivator <>));
                services.TryAddSingleton(typeof(DictionaryActivator <,>));
                services.TryAddSingleton <CodecProvider>();
                services.TryAddSingleton <ICodecProvider>(sp => sp.GetRequiredService <CodecProvider>());
                services.TryAddSingleton <IDeepCopierProvider>(sp => sp.GetRequiredService <CodecProvider>());
                services.TryAddSingleton <IFieldCodecProvider>(sp => sp.GetRequiredService <CodecProvider>());
                services.TryAddSingleton <IBaseCodecProvider>(sp => sp.GetRequiredService <CodecProvider>());
                services.TryAddSingleton <IValueSerializerProvider>(sp => sp.GetRequiredService <CodecProvider>());
                services.TryAddSingleton <IActivatorProvider>(sp => sp.GetRequiredService <CodecProvider>());
                services.TryAddSingleton(typeof(IFieldCodec <>), typeof(FieldCodecHolder <>));
                services.TryAddSingleton(typeof(IBaseCodec <>), typeof(BaseCodecHolder <>));
                services.TryAddSingleton(typeof(IValueSerializer <>), typeof(ValueSerializerHolder <>));
                services.TryAddSingleton(typeof(DefaultActivator <>));
                services.TryAddSingleton(typeof(IActivator <>), typeof(ActivatorHolder <>));
                services.TryAddSingleton <WellKnownTypeCollection>();
                services.TryAddSingleton <TypeCodec>();
                services.TryAddSingleton(typeof(IDeepCopier <>), typeof(CopierHolder <>));
                services.TryAddSingleton(typeof(IBaseCopier <>), typeof(BaseCopierHolder <>));

                // Type filtering
                services.AddSingleton <ITypeFilter, DefaultTypeFilter>();

                // Session
                services.TryAddSingleton <SerializerSessionPool>();
                services.TryAddSingleton <CopyContextPool>();

                services.AddSingleton <IGeneralizedCodec, CompareInfoCodec>();
                services.AddSingleton <IGeneralizedCopier, CompareInfoCopier>();
                services.AddSingleton <IGeneralizedCodec, WellKnownStringComparerCodec>();

                services.AddSingleton <ExceptionCodec>();
                services.AddSingleton <IGeneralizedCodec>(sp => sp.GetRequiredService <ExceptionCodec>());
                services.AddSingleton <IGeneralizedBaseCodec>(sp => sp.GetRequiredService <ExceptionCodec>());

                // Serializer
                services.TryAddSingleton <ObjectSerializer>();
                services.TryAddSingleton <Serializer>();
                services.TryAddSingleton(typeof(Serializer <>));
                services.TryAddSingleton(typeof(ValueSerializer <>));
                services.TryAddSingleton <DeepCopier>();
                services.TryAddSingleton(typeof(DeepCopier <>));
            }

            configure?.Invoke(context.Builder);

            return(services);
        }