/// <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)); }
/// <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()); }
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); }
/// <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; }
/// <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(); }
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 }
/// <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); }
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), })); }; }
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); }
public ActorHost( ActorTypeInformation actorTypeInfo, ActorId id, JsonSerializerOptions jsonSerializerOptions, ILoggerFactory loggerFactory, IActorProxyFactory proxyFactory) { this.ActorTypeInfo = actorTypeInfo; this.Id = id; this.LoggerFactory = loggerFactory; this.ProxyFactory = proxyFactory; }
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); }
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); }
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); }
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); }
/// <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); } }
/// <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; }
/// <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); }
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); }
/// <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)); }
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); }
/// <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); }
/// <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); } }
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())); }
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()); }
public override ActorActivator CreateActivator(ActorTypeInformation type) { return(new DependencyInjectionActorActivator(services, type)); }