Esempio n. 1
0
        /// <summary>
        /// Registers an actor with the runtime.
        /// </summary>
        /// <typeparam name="TActor">Type of actor.</typeparam>
        /// <param name="actorServiceFactory">An optional delegate to create actor service. This can be used for dependency injection into actors.</param>
        public void RegisterActor <TActor>(Func <ActorTypeInformation, ActorService> actorServiceFactory = null)
            where TActor : Actor
        {
            var actorTypeInfo = ActorTypeInformation.Get(typeof(TActor));

            this.actorServicesFunc.Add(actorTypeInfo, actorServiceFactory);
        }
        private ActorManager CreateActorManager(Type type, ActorActivator activator = null)
        {
            var registration = new ActorRegistration(ActorTypeInformation.Get(type));
            var interactor   = new DaprHttpInteractor(clientHandler: null, "http://localhost:3500", apiToken: null);

            return(new ActorManager(registration, activator ?? new DefaultActorActivator(), JsonSerializerDefaults.Web, NullLoggerFactory.Instance, ActorProxy.DefaultProxyFactory, interactor));
        }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ActorService"/> class.
 /// </summary>
 /// <param name="actorTypeInfo">The type information of the Actor.</param>
 /// <param name="actorFactory">The factory method to create Actor objects.</param>
 public ActorService(
     ActorTypeInformation actorTypeInfo,
     Func <ActorService, ActorId, Actor> actorFactory = null)
 {
     this.ActorTypeInfo = actorTypeInfo;
     this.actorFactory  = actorFactory ?? this.DefaultActorFactory;
     this.StateProvider = new DaprStateProvider(new ActorStateProviderSerializer());
 }
Esempio n. 4
0
        public async Task CreateAsync_CallsConstructor()
        {
            var activator = new DefaultActorActivator();

            var host  = new ActorHost(ActorTypeInformation.Get(typeof(TestActor)), ActorId.CreateRandom(), JsonSerializerDefaults.Web, NullLoggerFactory.Instance, ActorProxy.DefaultProxyFactory);
            var state = await activator.CreateAsync(host);

            Assert.IsType <TestActor>(state.Actor);
        }
Esempio n. 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ActorService"/> class.
 /// </summary>
 /// <param name="actorTypeInfo">The type information of the Actor.</param>
 /// <param name="loggerFactory">The logger factory.</param>
 /// <param name="actorFactory">The factory method to create Actor objects.</param>
 public ActorService(
     ActorTypeInformation actorTypeInfo,
     ILoggerFactory loggerFactory,
     Func <ActorService, ActorId, Actor> actorFactory = null)
 {
     this.ActorTypeInfo = actorTypeInfo;
     this.actorFactory  = actorFactory ?? this.DefaultActorFactory;
     this.StateProvider = new DaprStateProvider();
     this.LoggerFactory = loggerFactory;
 }
Esempio n. 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ActorHost"/> class.
 /// </summary>
 /// <param name="actorTypeInfo">The type information of the Actor.</param>
 /// <param name="id">The id of the Actor instance.</param>
 /// <param name="loggerFactory">The logger factory.</param>
 public ActorHost(
     ActorTypeInformation actorTypeInfo,
     ActorId id,
     ILoggerFactory loggerFactory)
 {
     this.ActorTypeInfo = actorTypeInfo;
     this.Id            = id;
     this.LoggerFactory = loggerFactory;
     this.StateProvider = new DaprStateProvider();
 }
Esempio n. 7
0
        public async Task DeleteAsync_NotDisposable()
        {
            var activator = new DefaultActorActivator();

            var host  = new ActorHost(ActorTypeInformation.Get(typeof(TestActor)), ActorId.CreateRandom(), JsonSerializerDefaults.Web, NullLoggerFactory.Instance, ActorProxy.DefaultProxyFactory);
            var actor = new TestActor(host);
            var state = new ActorActivatorState(actor);

            await activator.DeleteAsync(state); // does not throw
        }
Esempio n. 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ActorHost"/> class.
 /// </summary>
 /// <param name="actorTypeInfo">The type information of the Actor.</param>
 /// <param name="id">The id of the Actor instance.</param>
 /// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/> to use for actor state persistence and message deserialization.</param>
 /// <param name="loggerFactory">The logger factory.</param>
 public ActorHost(
     ActorTypeInformation actorTypeInfo,
     ActorId id,
     JsonSerializerOptions jsonSerializerOptions,
     ILoggerFactory loggerFactory)
 {
     this.ActorTypeInfo = actorTypeInfo;
     this.Id            = id;
     this.LoggerFactory = loggerFactory;
     this.StateProvider = new DaprStateProvider(jsonSerializerOptions);
 }
Esempio n. 9
0
        public DependencyInjectionActorActivator(IServiceProvider services, ActorTypeInformation type)
        {
            this.services = services;
            this.type     = type;

            // Will be invoked to initialize the factory.
            initializer = () =>
            {
                return(ActivatorUtilities.CreateFactory(this.type.ImplementationType, new Type[] { typeof(ActorHost), }));
            };
        }
Esempio n. 10
0
        public async Task CreateAsync_CanActivateWithDI()
        {
            var activator = CreateActivator(typeof(TestActor));

            var host  = new ActorHost(ActorTypeInformation.Get(typeof(TestActor)), ActorId.CreateRandom(), JsonSerializerDefaults.Web, NullLoggerFactory.Instance, ActorProxy.DefaultProxyFactory);
            var state = await activator.CreateAsync(host);

            var actor = Assert.IsType <TestActor>(state.Actor);

            Assert.NotNull(actor.SingletonService);
            Assert.NotNull(actor.ScopedService);
        }
Esempio n. 11
0
 public ActorHost(
     ActorTypeInformation actorTypeInfo,
     ActorId id,
     JsonSerializerOptions jsonSerializerOptions,
     ILoggerFactory loggerFactory,
     IActorProxyFactory proxyFactory)
 {
     this.ActorTypeInfo = actorTypeInfo;
     this.Id            = id;
     this.LoggerFactory = loggerFactory;
     this.ProxyFactory  = proxyFactory;
 }
Esempio n. 12
0
        public async Task DeleteAsync_Disposable()
        {
            var activator = new DefaultActorActivator();

            var host  = new ActorHost(ActorTypeInformation.Get(typeof(DisposableActor)), ActorId.CreateRandom(), NullLoggerFactory.Instance);
            var actor = new DisposableActor(host);
            var state = new ActorActivatorState(actor);

            await activator.DeleteAsync(state); // does not throw

            Assert.True(actor.IsDisposed);
        }
Esempio n. 13
0
        public async Task DeleteAsync_AsyncDisposable()
        {
            var activator = new DefaultActorActivator();

            var host  = new ActorHost(ActorTypeInformation.Get(typeof(AsyncDisposableActor)), ActorId.CreateRandom(), JsonSerializerDefaults.Web, NullLoggerFactory.Instance, ActorProxy.DefaultProxyFactory);
            var actor = new AsyncDisposableActor(host);
            var state = new ActorActivatorState(actor);

            await activator.DeleteAsync(state);

            Assert.True(actor.IsDisposed);
        }
Esempio n. 14
0
        public async Task DeleteAsync_Disposable()
        {
            var activator = CreateActivator(typeof(DisposableActor));

            var host  = new ActorHost(ActorTypeInformation.Get(typeof(DisposableActor)), ActorId.CreateRandom(), JsonSerializerDefaults.Web, NullLoggerFactory.Instance, ActorProxy.DefaultProxyFactory);
            var state = await activator.CreateAsync(host);

            var actor = Assert.IsType <DisposableActor>(state.Actor);

            await activator.DeleteAsync(state); // does not throw

            Assert.True(actor.IsDisposed);
        }
Esempio n. 15
0
        public async Task DeleteAsync_AsyncDisposable()
        {
            var activator = CreateActivator(typeof(AsyncDisposableActor));

            var host  = new ActorHost(ActorTypeInformation.Get(typeof(AsyncDisposableActor)), ActorId.CreateRandom(), NullLoggerFactory.Instance);
            var state = await activator.CreateAsync(host);

            var actor = Assert.IsType <AsyncDisposableActor>(state.Actor);

            await activator.DeleteAsync(state);

            Assert.True(actor.IsDisposed);
        }
Esempio n. 16
0
 /// <summary>
 /// Creates the <see cref="ActorTypeInformation"/> from actorType.
 /// </summary>
 /// <param name="actorType">The type of class implementing the actor to create ActorTypeInformation for.</param>
 /// <param name="actorTypeInformation">When this method returns, contains ActorTypeInformation, if the creation of
 /// ActorTypeInformation from actorType succeeded, or null if the creation failed.
 /// The creation fails if the actorType parameter is null or it does not implement an actor.</param>
 /// <returns>true if ActorTypeInformation was successfully created for actorType; otherwise, false.</returns>
 /// <remarks>
 /// <para>Creation of ActorTypeInformation from actorType will fail when:</para>
 /// <para>1. <see cref="System.Type.BaseType"/> for actorType is not of type <see cref="Actor"/>.</para>
 /// <para>2. actorType does not implement an interface deriving from <see cref="IActor"/> and is not marked as abstract.</para>
 /// </remarks>
 public static bool TryGet(Type actorType, out ActorTypeInformation actorTypeInformation)
 {
     try
     {
         actorTypeInformation = Get(actorType);
         return(true);
     }
     catch (ArgumentException)
     {
         actorTypeInformation = null;
         return(false);
     }
 }
Esempio n. 17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ActorHost"/> class.
 /// </summary>
 /// <param name="actorTypeInfo">The type information of the Actor.</param>
 /// <param name="id">The id of the Actor instance.</param>
 /// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/> to use for actor state persistence and message deserialization.</param>
 /// <param name="loggerFactory">The logger factory.</param>
 /// <param name="proxyFactory">The <see cref="ActorProxyFactory" />.</param>
 /// <param name="timerManager">The <see cref="ActorTimerManager" />.</param>
 internal ActorHost(
     ActorTypeInformation actorTypeInfo,
     ActorId id,
     JsonSerializerOptions jsonSerializerOptions,
     ILoggerFactory loggerFactory,
     IActorProxyFactory proxyFactory,
     ActorTimerManager timerManager)
 {
     this.ActorTypeInfo = actorTypeInfo;
     this.Id            = id;
     this.LoggerFactory = loggerFactory;
     this.ProxyFactory  = proxyFactory;
     this.TimerManager  = timerManager;
 }
Esempio n. 18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ActorHost"/> class.
 /// </summary>
 /// <param name="actorTypeInfo">The type information of the Actor.</param>
 /// <param name="id">The id of the Actor instance.</param>
 /// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/> to use for actor state persistence and message deserialization.</param>
 /// <param name="loggerFactory">The logger factory.</param>
 /// <param name="proxyFactory">The <see cref="ActorProxyFactory" />.</param>
 /// <param name="daprInteractor">The <see cref="IDaprInteractor" />.</param>
 internal ActorHost(
     ActorTypeInformation actorTypeInfo,
     ActorId id,
     JsonSerializerOptions jsonSerializerOptions,
     ILoggerFactory loggerFactory,
     IActorProxyFactory proxyFactory,
     IDaprInteractor daprInteractor)
 {
     this.ActorTypeInfo  = actorTypeInfo;
     this.Id             = id;
     this.LoggerFactory  = loggerFactory;
     this.ProxyFactory   = proxyFactory;
     this.DaprInteractor = daprInteractor;
     this.StateProvider  = new DaprStateProvider(this.DaprInteractor, jsonSerializerOptions);
 }
Esempio n. 19
0
        public async Task DeleteAsync_DisposesScope()
        {
            var activator = CreateActivator(typeof(TestActor));

            var host  = new ActorHost(ActorTypeInformation.Get(typeof(TestActor)), ActorId.CreateRandom(), JsonSerializerDefaults.Web, NullLoggerFactory.Instance, ActorProxy.DefaultProxyFactory);
            var state = await activator.CreateAsync(host);

            var actor = Assert.IsType <TestActor>(state.Actor);

            Assert.False(actor.ScopedService.IsDisposed);

            await activator.DeleteAsync(state);

            Assert.True(actor.ScopedService.IsDisposed);
        }
Esempio n. 20
0
        /// <summary>
        /// Creates an instance of <see cref="ActorHost" /> for unit testing an actor instance.
        /// </summary>
        /// <param name="actorType">The actor type.</param>
        /// <param name="options">The <see cref="ActorTestOptions" /> for configuring the host.</param>
        /// <returns>An <see cref="ActorHost" /> instance.</returns>
        public static ActorHost CreateForTest(Type actorType, ActorTestOptions options = null)
        {
            if (actorType == null)
            {
                throw new ArgumentNullException(nameof(actorType));
            }

            options ??= new ActorTestOptions();

            return(new ActorHost(
                       ActorTypeInformation.Get(actorType),
                       options.ActorId,
                       options.JsonSerializerOptions,
                       options.LoggerFactory,
                       options.ProxyFactory,
                       options.TimerManager));
        }
Esempio n. 21
0
        public async Task CreateAsync_CreatesNewScope()
        {
            var activator = CreateActivator(typeof(TestActor));

            var host1  = new ActorHost(ActorTypeInformation.Get(typeof(TestActor)), ActorId.CreateRandom(), JsonSerializerDefaults.Web, NullLoggerFactory.Instance, ActorProxy.DefaultProxyFactory);
            var state1 = await activator.CreateAsync(host1);

            var actor1 = Assert.IsType <TestActor>(state1.Actor);

            var host2  = new ActorHost(ActorTypeInformation.Get(typeof(TestActor)), ActorId.CreateRandom(), JsonSerializerDefaults.Web, NullLoggerFactory.Instance, ActorProxy.DefaultProxyFactory);
            var state2 = await activator.CreateAsync(host2);

            var actor2 = Assert.IsType <TestActor>(state2.Actor);

            Assert.Same(actor1.SingletonService, actor2.SingletonService);
            Assert.NotSame(actor1.ScopedService, actor2.ScopedService);
        }
Esempio n. 22
0
        /// <summary>
        /// Registers an actor with the runtime.
        /// </summary>
        /// <typeparam name="TActor">Type of actor.</typeparam>
        /// <param name="actorServiceFactory">An optional delegate to create actor service. This can be used for dependency injection into actors.</param>
        public void RegisterActor <TActor>(Func <ActorTypeInformation, ActorService> actorServiceFactory = null)
            where TActor : Actor
        {
            var actorTypeInfo = ActorTypeInformation.Get(typeof(TActor));

            ActorService actorService;

            if (actorServiceFactory != null)
            {
                actorService = actorServiceFactory.Invoke(actorTypeInfo);
            }
            else
            {
                actorService = new ActorService(actorTypeInfo);
            }

            // Create ActorManagers, override existing entry if registered again.
            this.actorManagers[actorTypeInfo.ActorTypeName] = new ActorManager(actorService);
        }
Esempio n. 23
0
        /// <summary>
        /// Registers an actor with the runtime.
        /// </summary>
        /// <param name="tActor">Type of actor.</param>
        /// <param name="actorServiceFactory">An optional delegate to create actor service. This can be used for dependency injection into actors.</param>
        public void RegisterActor(Type tActor, Func <ActorTypeInformation, ActorService> actorServiceFactory = null)
        {
            if (tActor.IsSubclassOf(typeof(Actor)))
            {
                var actorTypeInfo = ActorTypeInformation.Get(tActor);

                ActorService actorService;
                if (actorServiceFactory != null)
                {
                    actorService = actorServiceFactory.Invoke(actorTypeInfo);
                }
                else
                {
                    actorService = new ActorService(actorTypeInfo);
                }

                // Create ActorManagers, override existing entry if registered again.
                this.actorManagers[actorTypeInfo.ActorTypeName] = new ActorManager(actorService);
            }
        }
Esempio n. 24
0
        private ActorManager CreateActorManager(Type type, ActorActivator activator = null)
        {
            var registration = new ActorRegistration(ActorTypeInformation.Get(type));

            return(new ActorManager(registration, activator ?? new DefaultActorActivator(), JsonSerializerDefaults.Web, NullLoggerFactory.Instance, ActorProxy.DefaultProxyFactory, new DaprHttpInteractor()));
        }
Esempio n. 25
0
 private DependencyInjectionActorActivator CreateActivator(Type type)
 {
     return(new DependencyInjectionActorActivator(CreateServices(), ActorTypeInformation.Get(type)));
 }
        private ActorManager CreateActorManager(Type type, ActorActivator activator = null)
        {
            var registration = new ActorRegistration(ActorTypeInformation.Get(type));

            return(new ActorManager(registration, activator ?? new DefaultActorActivator(), NullLoggerFactory.Instance));
        }
 /// <summary>
 /// Creates the <see cref="ActorActivator" /> for the provided <paramref name="type" />.
 /// </summary>
 /// <param name="type">The <see cref="ActorTypeInformation" />.</param>
 /// <returns>An <see cref="ActorActivator" />.</returns>
 public override ActorActivator CreateActivator(ActorTypeInformation type)
 {
     return(new DefaultActorActivator());
 }
Esempio n. 28
0
 public override ActorActivator CreateActivator(ActorTypeInformation type)
 {
     return(new DependencyInjectionActorActivator(services, type));
 }