NOTE: This repository has been archived. The decision to archive this repository is based on prioritizing the use of the official interceptors approach provided by the Temporal team. If you are interested in exploring the official interceptors approach, you can refer to the Temporal samples repository: https://github.com/temporalio/samples-dotnet
Source generator for the Temporal .NET SDK that generates proxy classes for interfaces containing activity methods. The proxy classes enable dependency injection for your activities, making it easier to manage dependencies and services.
Add the TemporalActivityGen package to your project for example using local nuget feed.
To generate proxy classes, use the GenerateActivityProxy attribute on interfaces that contain activity methods, and the Activity attribute on the methods themselves.
[GenerateActivityProxy]publicinterfaceIOrderProcessingActivities{publicstaticreadonlyIOrderProcessingActivitiesRef=ActivityRefs.Create<IOrderProcessingActivities>();[Activity("notify-orders-processed")]publicTaskNotifyOrderProcessed(ProcessOrderInputinput);[Activity]publicTask<ShipItemsOutput>ShipItems(ShipItemsInputinput);publicTaskNonActivityMethod();}services.AddScoped<IOrderProcessingActivities,OrderProcessingActivities>();Use the ActivityProxyHelper class to discover and register the generated proxy classes with the TemporalWorker.
IServiceProviderserviceProvider= ...
using var worker=newTemporalWorker(client,newTemporalWorkerOptions{Activities=ActivityProxyHelper.DiscoverAndRegisterActivityProxies(serviceProvider,typeof(OrderProcessingActivities).Assembly).ToList(),});//------------------------------------------------------------------------------// <auto-generated>// This code was generated by the TemporalActivityGen source generator//// Changes to this file may cause incorrect behavior and will be lost if// the code is regenerated.// </auto-generated>//------------------------------------------------------------------------------
#pragma warning disable CS1591// publicly visible type or member must be documented
#nullable enable
usingTemporalActivityGen;usingTemporalio;usingTemporalio.Activities;usingMicrosoft.Extensions.DependencyInjection;[assembly:global::TemporalActivityGen.ActivityProxyRegistration(typeof(global::TestApplication.OrderProcessingActivitiesProxy))]namespaceTestApplication;publicpartialclassOrderProcessingActivitiesProxy:global::TestApplication.IOrderProcessingActivities{privatereadonlyglobal::System.IServiceProvider_serviceProvider;publicOrderProcessingActivitiesProxy(global::System.IServiceProviderserviceProvider){_serviceProvider=serviceProvider;}[Activity("notify-orders-processed")]publicasyncglobal::System.Threading.Tasks.TaskNotifyOrderProcessed(global::TestApplication.NotifyOrderProcessedInputinput){awaitusingvarscope=_serviceProvider.CreateAsyncScope();varimpl=scope.ServiceProvider.GetRequiredService<global::TestApplication.IOrderProcessingActivities>();awaitimpl.NotifyOrderProcessed(input);}[Activity]publicasyncglobal::System.Threading.Tasks.Task<global::TestApplication.ShipItemsOutput>ShipItems(global::TestApplication.ShipItemsInputinput){awaitusingvarscope=_serviceProvider.CreateAsyncScope();varimpl=scope.ServiceProvider.GetRequiredService<global::TestApplication.Activities.IShippingActivities>();returnawaitimpl.ShipItems(input);}}