Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

丁松杰 / Pole

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • Pole
  • src
  • Pole.Sagas.Client
  • PoleSagaServiceCollectionExtensions.cs
Find file
BlameHistoryPermalink
  • dingsongjie's avatar
    添加 sagas数据库初始化功能 · fa6f2610
    dingsongjie committed 5 years ago
    fa6f2610
PoleSagaServiceCollectionExtensions.cs 1.87 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Pole.Core;
using Pole.Core.Utils;
using Pole.Sagas.Core;
using Pole.Sagas.Core.Abstraction;
using Pole.Sagas.Core.Exceptions;
using static Pole.Sagas.Server.Grpc.Saga;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class PoleSagaServiceCollectionExtensions
    {
        public static void AddSagas(this StartupConfig startupOption, Action<PoleSagasOption> configAction)
        {
            startupOption.Services.Configure(configAction);
            startupOption.Services.AddSingleton<IActivityFinder, ActivityFinder>();
            startupOption.Services.AddSingleton<IEventSender, EventSender>();
            startupOption.Services.AddSingleton<ISagaFactory, SagaFactory>();
            using(var provider = startupOption.Services.BuildServiceProvider())
            {
                var sagasOption = provider.GetRequiredService<IOptions<PoleSagasOption>>().Value;
                startupOption.Services.AddGrpcClient<SagaClient>(o =>
                {
                    o.Address = new Uri(sagasOption.SagasServerHost);
                });
            }
            var baseActivityType = typeof(IActivity<>);
            foreach (var assembly in AssemblyHelper.GetAssemblies())
            {

                foreach (var type in assembly.GetTypes().Where(m => m.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == baseActivityType) && m.IsClass && !m.IsAbstract))
                {
                    if (!type.FullName.EndsWith("Activity"))
                    {
                        throw new ActivityNameIrregularException(type);
                    }
                    startupOption.Services.AddScoped(type);
                }
            }
        }
    }
}