Подтвердить что ты не робот

Взаимодействие с зависимой линией Azure сервисной ткани

Есть ли способ вставить зависимости в конструктор Actor Service Fabric?

4b9b3361

Ответ 1

Немного пообщавшись в этой области с dotPeek некоторое время назад (пытаясь разрешить участников из области жизненного цикла Autofac для каждого вызова), я думаю, что трюк состоит в том, чтобы создать собственную реализацию StatelessActorServiceFactory, а ваш собственный метод расширения для регистрации актера с ним. Хотя класс factory помечен как внутренний, его интерфейс (IStatelessServiceFactory) и тип сервиса, который он создает (StatelessActorServiceInstance), являются общедоступными. К сожалению, это не похоже, что StatelessActorServiceInstance был разработан для расширения (я надеюсь, что это просто недосмотр).

К сожалению, похоже, что WcfActorCommunicationProvider также помечен как внутренний, поэтому вам в значительной степени придется создавать свой собственный конвейер с нуля:

  • Внедрите собственный IStatelessServiceFactory
  • Внедрите собственный IStatelessServiceInstance, IActorService
  • Внедрите свой собственный IActorCommunicationProvider
  • Внедрите свой собственный IActorHost

На самом деле это не стоит больше усилий, не так ли?: -/

То, где я сейчас сдался. Я не думаю, что это стоит попробовать сделать свой собственный пока, учитывая относительную незрелость публичного API, поскольку, если такие функции появятся вообще, они, вероятно, сделают это так, что это сломается что бы вы ни реализовали.

Ответ 2

Обновление

Теперь все на github и myget: https://github.com/s-innovations/S-Innovations.ServiceFabric.Unity

и интегрируется с инжекцией зависимостей ядра aspnet без особых хлопот, проверьте примеры readme.md


Я долгое время являюсь пользователем Unity и решил сделать основные методы расширения необходимыми, чтобы иметь хороший опыт инъекции зависимости при работе с актерами.

Теперь моя program.cs выглядит так:

internal static class Program
{
    /// <summary>
    /// This is the entry point of the service host process.
    /// </summary>
    private static void Main()
    {
        try
        {
            using (var container = new UnityContainer())
            {
                container.RegisterType<IMessageProcessorClientFactory, DummyFactory>(new HierarchicalLifetimeManager());
                container.RegisterType<IMessageClusterConfigurationStore, test>(new HierarchicalLifetimeManager());

                container.WithFabricContainer();
                container.WithActor<MessageClusterActor>();
                container.WithActor<QueueListenerActor>();
                container.WithStatelessFactory<ManagementApiServiceFactory>("ManagementApiServiceType");
                container.WithActor<VmssManagerActor>();

                ServiceFabricEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(ManagementApiService).Name);

                Thread.Sleep(Timeout.Infinite);  // Prevents this host process from terminating to keep the service host process running.
            }
        }
        catch (Exception e)
        {
            ServiceFabricEventSource.Current.ActorHostInitializationFailed(e.ToString());
            throw;
        }
    }
}

где я в актерах и службах может просто вставлять свои зависимости в конструкторы.

public class VmssManagerActor : StatefulActor<VmssManagerActor.ActorState>, IVmssManagerActor, IRemindable
{
    public const string CheckProvision = "CheckProvision";

    /// <summary>
    /// Cluster Configuration Store
    /// </summary>       
    protected IMessageClusterConfigurationStore ClusterConfigStore { get; private set; }

    public VmssManagerActor(IMessageClusterConfigurationStore clusterProvider)
    {
        ClusterConfigStore = clusterProvider;
    }

Если вы считаете, что это полезно и мне хотелось бы включить его в пакет nuget, включите этот ответ.

Одна нота о реализации, каждый актер получит свою собственную область. Это означает, что все зависимости, зарегистрированные с помощью "HierarchicalLifetimeManager", которые реализуют IDisposable, автоматически будут удалены в акторе OnDeactivationAsync. Это было сделано путем динамического проксирования класса actor с динамическим типом, который перехватывает вызов OnDeactivationAsync. Для этого для работы актер должен быть общедоступным.

IActorDeactivationInterception.cs

namespace SInnovations.Azure.ServiceFabric.Unity.Abstraction
{
    /// <summary>
    /// The <see cref="IActorDeactivationInterception"/> interface for defining an OnDeactivateInterception
    /// </summary>
    public interface IActorDeactivationInterception
    {
        void Intercept();
    }
}

ActorProxyTypeFactory.cs

namespace SInnovations.Azure.ServiceFabric.Unity.Actors
{
    using System;
    using System.Linq;
    using System.Reflection;
    using System.Reflection.Emit;
    using SInnovations.Azure.ServiceFabric.Unity.Abstraction;

    public class ActorProxyTypeFactory
    {
        /// <summary>
        /// Creates a new instance of the <see cref="ActorProxyTypeFactory"/> class.
        /// </summary>
        /// <param name="target"></param>
        public ActorProxyTypeFactory(Type target)
        {
            this.target = target;
        }

        /// <summary>
        /// Creates the proxy registered with specific interceptor.
        /// </summary>
        /// <returns></returns>
        public static T Create<T>(IActorDeactivationInterception deactivation, params object[] args)
        {
            return (T)new ActorProxyTypeFactory(typeof(T)).Create(new object[] { deactivation }.Concat(args).ToArray());
        }
        public static Type CreateType<T>()
        {
            return new ActorProxyTypeFactory(typeof(T)).CreateType();
        }
        /// <summary>
        /// Creates the proxy registered with specific interceptor.
        /// </summary>
        /// <returns></returns>
        public object Create(object[] args)
        {
            BuidAssembly();
            BuildType();
            InterceptAllMethods();

            Type proxy = this.typeBuilder.CreateType();

            return Activator.CreateInstance(proxy, args);
        }

        public Type CreateType()
        {
            BuidAssembly();
            BuildType();
            InterceptAllMethods();

            Type proxy = this.typeBuilder.CreateType();
            return proxy;
            //  return Activator.CreateInstance(proxy, args);
        }

        /// <summary>
        /// Builds a dynamic assembly with <see cref="AssemblyBuilderAccess.RunAndSave"/> mode.
        /// </summary>
        /// <returns></returns>
        public void BuidAssembly()
        {
            AssemblyName assemblyName = new AssemblyName("BasicProxy");
            AssemblyBuilder createdAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
            // define module
            this.moduleBuilder = createdAssembly.DefineDynamicModule(assemblyName.Name);
        }

        public void BuildType()
        {
            if (!target.IsPublic)
            {
                throw new ArgumentException("Actors have to be public defined to proxy them");
            }


            this.typeBuilder =
                this.moduleBuilder.DefineType(target.FullName + "Proxy", TypeAttributes.Class | TypeAttributes.Public, target);
            this.fldInterceptor = this.typeBuilder.DefineField("interceptor", typeof(IActorDeactivationInterception), FieldAttributes.Private);

            foreach (var constructor in target.GetConstructors())
            {
                //  Type[] parameters = new Type[1];

                ParameterInfo[] parameterInfos = constructor.GetParameters();
                Type[] parameters = new Type[parameterInfos.Length + 1];

                parameters[0] = typeof(IActorDeactivationInterception);


                for (int index = 1; index <= parameterInfos.Length; index++)
                {
                    parameters[index] = parameterInfos[index - 1].ParameterType;
                }

                ConstructorBuilder constructorBuilder =
                    typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameters);

                for (int argumentIndex = 0; argumentIndex < parameters.Length; argumentIndex++)
                    constructorBuilder.DefineParameter(
                        argumentIndex + 1,
                        ParameterAttributes.None,
                        $"arg{argumentIndex}");

                ILGenerator generator = constructorBuilder.GetILGenerator();

                generator.Emit(OpCodes.Ldarg_0);

                for (int index = 1; index < parameters.Length; index++)
                {
                    generator.Emit(OpCodes.Ldarg, index + 1);
                }

                generator.Emit(OpCodes.Call, constructor);

                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldarg_1);
                generator.Emit(OpCodes.Stfld, fldInterceptor);
                generator.Emit(OpCodes.Ret);
            }
        }

        /// <summary>
        /// Builds a type in the dynamic assembly, if already the type is not created.
        /// </summary>
        /// <returns></returns>
        public void InterceptAllMethods()
        {

            const MethodAttributes targetMethodAttributes =
                MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig;

            var methodInfo = target.GetMethod("OnDeactivateAsync", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
            {
                if (methodInfo.IsVirtual)
                {
                    Type[] paramTypes = GetParameterTypes(methodInfo.GetParameters());

                    MethodBuilder methodBuilder =
                        typeBuilder.DefineMethod(methodInfo.Name, targetMethodAttributes, methodInfo.ReturnType, paramTypes);

                    ILGenerator ilGenerator = methodBuilder.GetILGenerator();


                    ilGenerator.Emit(OpCodes.Ldarg_0);
                    ilGenerator.Emit(OpCodes.Ldfld, fldInterceptor);
                    ilGenerator.Emit(OpCodes.Call, typeof(IActorDeactivationInterception).GetMethod("Intercept"));

                    ilGenerator.Emit(OpCodes.Ldarg_0);
                    ilGenerator.Emit(OpCodes.Call, methodInfo);
                    ilGenerator.Emit(OpCodes.Ret);

                    return;


                }
            }
        }



        private Type[] GetParameterTypes(ParameterInfo[] parameterInfos)
        {
            Type[] parameters = new Type[parameterInfos.Length];

            int index = 0;

            foreach (var parameterInfo in parameterInfos)
            {
                parameters[index++] = parameterInfo.ParameterType;
            }
            return parameters;
        }

        private TypeBuilder typeBuilder;
        private ModuleBuilder moduleBuilder;
        private readonly Type target;
        private FieldInfo fldInterceptor;

    }

}

OnActorDeactivateInterceptor.cs

namespace SInnovations.Azure.ServiceFabric.Unity.Actors
{
    using Microsoft.Practices.Unity;
    using SInnovations.Azure.ServiceFabric.Unity.Abstraction;

    public class OnActorDeactivateInterceptor : IActorDeactivationInterception
    {
        private readonly IUnityContainer container;
        public OnActorDeactivateInterceptor(IUnityContainer container)
        {
            this.container = container;
        }

        public void Intercept()
        {
            this.container.Dispose();
        }
    }
}

UnityFabricExtensions.cs

namespace SInnovations.Azure.ServiceFabric.Unity
{
    using System;
    using System.Fabric;
    using Microsoft.Practices.Unity;
    using Microsoft.ServiceFabric.Actors;
    using SInnovations.Azure.ServiceFabric.Unity.Abstraction;
    using SInnovations.Azure.ServiceFabric.Unity.Actors;

    public static class UnityFabricExtensions
    {
        public static IUnityContainer WithFabricContainer(this IUnityContainer container)
        {
            return container.WithFabricContainer(c => FabricRuntime.Create());
        }
        public static IUnityContainer WithFabricContainer(this IUnityContainer container, Func<IUnityContainer,FabricRuntime> factory)
        {
            container.RegisterType<FabricRuntime>(new ContainerControlledLifetimeManager(), new InjectionFactory(factory));
            return container;
        }

        public static IUnityContainer WithActor<TActor>(this IUnityContainer container) where TActor : ActorBase
        {
            if (!container.IsRegistered<IActorDeactivationInterception>())
            {
                container.RegisterType<IActorDeactivationInterception, OnActorDeactivateInterceptor>(new HierarchicalLifetimeManager());
            }

            container.RegisterType(typeof(TActor), ActorProxyTypeFactory.CreateType<TActor>(),new HierarchicalLifetimeManager());
            container.Resolve<FabricRuntime>().RegisterActorFactory(() => {
                try {
                    var actor = container.CreateChildContainer().Resolve<TActor>();
                    return actor;
                }
                catch (Exception ex)
                {
                    throw;
                }
                });

            return container;
        }


        public static IUnityContainer WithStatelessFactory<TFactory>(this IUnityContainer container, string serviceTypeName) where TFactory : IStatelessServiceFactory
        {
            if (!container.IsRegistered<TFactory>())
            {
                container.RegisterType<TFactory>(new ContainerControlledLifetimeManager());
            }
            container.Resolve<FabricRuntime>().RegisterStatelessServiceFactory(serviceTypeName, container.Resolve<TFactory>());
            return container;
        }
        public static IUnityContainer WithStatefulFactory<TFactory>(this IUnityContainer container, string serviceTypeName) where TFactory : IStatefulServiceFactory
        {
            if (!container.IsRegistered<TFactory>())
            {
                container.RegisterType<TFactory>(new ContainerControlledLifetimeManager());
            }
            container.Resolve<FabricRuntime>().RegisterStatefulServiceFactory(serviceTypeName, container.Resolve<TFactory>());
            return container;
        }
        public static IUnityContainer WithService<TService>(this IUnityContainer container, string serviceTypeName) 
        {
            container.Resolve<FabricRuntime>().RegisterServiceType(serviceTypeName, typeof(TService));
            return container;
        }
    }
}

Ответ 3

Я знаю, что это устарело, но для документаций DI теперь поддерживается в платформе Reliable Actor, как и следовало ожидать.

public class ActorOne : Actor<MyActorState>, IMyActor{

private readonly IDependency _dependency;

public ActorOne(IDependency dependency)
{
    _dependency = dependency;
}}

И затем вы регистрируете Актера с его зависимостью от Сервисной ткани следующим образом:

using (FabricRuntime fRuntime = FabricRuntime.Create()){

fRuntime.RegisterActor(() => new ActorOne(new MyDependency());
Thread.Sleep(Timeout.Infinite);}

Ответ 4

Почему бы просто не использовать какое-либо корневое поле в акторе и разрешить его из контейнера с вложенными зависимостями в конструкторе Actor? Если это плохое решение, объясните, почему:

public class StatelessActor2 : Actor, IStatelessActor2
{
    private ConfiguredContainer _container;
    private IRootElement _rootElement;

    public StatelessActor2()

    {
        _container = new ConfiguredContainer(); //... container is configured in it constructor
        _rootElement = _container.Resolve<IRootElement>();
    }

    public async Task<string> DoWorkAsync()
    {
        // Working with a RootElement with all dependencies are injected..
        return await Task.FromResult(_rootElement.WorkingWithInjectedStuff());
    }
}

Ответ 5

Если вы используете Autofac, у них есть специальный пакет интеграции для этого:

https://alexmg.com/introducing-the-autofac-integration-for-service-fabric/ https://www.nuget.org/packages/Autofac.ServiceFabric/

Короче говоря, регистрация выполняется с использованием ActorRuntime.RegisterActorAsync/ServiceRuntime.RegisterServiceAsync, как и следовало ожидать. Однако более проблематичная часть, а именно релиз объекта, автоматически обрабатывается в переопределениях OnDeactivateAsync/OnCloseAsync/OnAbort с использованием динамического прокси. Поддерживается также и область продолжительности жизни.

На момент написания это все еще в Alpha, хотя (только что выпущено в прошлом месяце).