asp.net web开发 ASP.NET Core Web API下事件驱动型架构的实(11)

电脑杂谈  发布时间:2018-01-14 13:09:17  来源:网络整理

private readonly ILogger logger;

private readonly IEventHandlerExecutionContext context;

public PassThroughEventBus(IEventHandlerExecutionContext context,

ILogger<PassThroughEventBus> logger)

{

this .context = context;

this .logger = logger;

logger.LogInformation($ "PassThroughEventBus构造函数调用完成。Hash Code:{this.GetHashCode()}." );

eventQueue.EventPushed += EventQueue_EventPushed;

}

private async void EventQueue_EventPushed( object sender, EventProcessedEventArgs e)

=> await this .context.HandleEventAsync(e.Event);

public Task PublishAsync<TEvent>(TEvent @ event , CancellationToken cancellationToken = default )

where TEvent : IEvent

=> Task.Factory.StartNew(() => eventQueue.Push(@ event ));

public void Subscribe<TEvent, TEventHandler>()

where TEvent : IEvent

where TEventHandler : IEventHandler<TEvent>

{

if (! this .context.HandlerRegistered<TEvent, TEventHandler>())

{

this .context.RegisterHandler<TEvent, TEventHandler>();

}

}

#region IDisposable Support

private bool disposedValue = false ; // To detect redundant calls

void Dispose( bool disposing)

{

if (!disposedValue)

{

if (disposing)

{

this .eventQueue.EventPushed -= EventQueue_EventPushed;

logger.LogInformation($ "PassThroughEventBus已经被Dispose。Hash Code:{this.GetHashCode()}." );

}

disposedValue = true ;

}

}

public void Dispose() => Dispose( true );

#endregion

}

// Startup.cs

public void ConfigureServices(IServiceCollection services)

{

this .logger.LogInformation( "正在对服务进行配置..." );


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-60185-11.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...