0

I working on a project using masstransit Courier. I am trying to inject some service dependencies into my Activities but this is not working. Can someone help how to use DI in MT activity. I could not find any working example with Activity implementation and DI

In my below code I am trying to inject IMyservice into my activity. But my activity constructor is never executing. Am I doing something wrong here?

 public class DownloadImageActivity :
        Activity<DownloadImageArguments, DownloadImageLog>
    {

public DownloadImageActivity (IMyService service)
{
_service=service;
}
 Task<ExecuteResult> Execute(ExecutionContext<DownloadImageArguments> context);
        Task<CompensationResult> Compensate(CompensateContext<DownloadImageLog> context);
    }

my service program code ActivityService.cs

_busControl = Bus.Factory.CreateUsingRabbitMq(x =>
        {
            IRabbitMqHost host = x.Host(new Uri(ConfigurationManager.AppSettings["RabbitMQHost"]), h =>
            {
                h.Username("guest");
                h.Password("guest");
            });

            x.ReceiveEndpoint(host, ConfigurationManager.AppSettings["completepitaskqueue"], e =>
            {
                e.PrefetchCount = 100;

                e.ExecuteActivityHost<CompletePiTaskActivity, ComplePiTaskArguments>(Program.Container);
            //  e.ExecuteActivityHost(
            //  DefaultConstructorExecuteActivityFactory<CompletePiTaskActivity, ComplePiTaskArguments>.ExecuteFactory, c => c.UseRetry(r => r.Immediate(5)));
            });

My program.cs

public static IContainer Container;
    static int Main(string[] args)
    {
        ConfigureLogger();
        Container = builder.Build();
        ActivityHelper.Container = Container;

        // MassTransit to use Log4Net
        Log4NetLogger.Use();

        return (int)HostFactory.Run(x => x.Service<ActivityService>());
4

1 回答 1

0

It depends upon which container you are using, some are more supported than others (out of the box, anyway, any should work).

For example, to configure a receive endpoint with an activity using Autofac, you would specify:

cfg.ReceiveEndpoint(host, "execute-activity", e =>
{
    e.ExecuteActivityHost<TActivity, TArguments>(container);
});

Where container is either the builder context or the container. This will register the appropriate scope providers and activity factories for your activity.

This uses the extension method: https://github.com/MassTransit/MassTransit/blob/develop/src/Containers/MassTransit.AutofacIntegration/AutofacExtensions.cs#L300

于 2019-01-07T14:58:51.197 回答