从 Service Fabric Web API 控制器写入服务事件源

Write to ServiceEventSource from Service Fabric WebAPI controller

在我的有状态服务中,我可以通过以下调用写入 ServiceEventSource

ServiceEventSource.Current.ServiceMessage(this.Context, "this is my log message");

有谁知道如何在我的无状态 WebAPI 控制器中进行相同的调用?似乎我无法将上下文获取到控制器中。我注意到它只在我的 OwinCommunicationListener 中可用。

基本上,我希望能够像这样记录我的控制器:

public async Task<IHttpActionResult> Get(string id)
{
   ServiceEventSource.Current.ServiceMessage(this.Context, "this is my log message");
   //Do something
   return Ok(100);
}

解决这个问题的一种方法是使用依赖注入和 IoC,就像使用常规 WebAPI 解决方案一样。

如果您使用 out-of-the-box 提供的 OwinCommuncationControllerStartup class,您可以初始化容器并将其添加到 Startup.ConfigureApp(...) 方法:

public static class Startup
{
    // This code configures Web API. The Startup class is specified as a type
    // parameter in the WebApp.Start method.
    public static void ConfigureApp(IAppBuilder appBuilder)
    {
        // Configure Web API for self-host. 
        HttpConfiguration config = new HttpConfiguration();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        // Add a DependencyResolver here

        appBuilder.UseWebApi(config);
    }
}

您可以使用任何您喜欢的 IoC,在这里我将展示它用于 TinyIoC,但对于任何(Windsor、Unity、Ninject、AutoFac...)都采用类似的方法。

对于 TinyIoC 添加 NuGet TinyIoCTinyIoC.AspNetExtensions 并添加实现 IDependencyResolver:

的 class
public class TinyIoCResolver : IDependencyResolver
{
    private readonly TinyIoCContainer _container;

    public TinyIoCResolver(TinyIoCContainer container)
    {
        _container = container;
    }

    public object GetService(Type serviceType)
    {
        return _container.Resolve(serviceType);
    }

    public object GetService(Type serviceType)
    {
        try
        {
            return _container.Resolve(serviceType);
        }
        catch (TinyIoCResolutionException)
        {
            return null;
        }
    }

    public IDependencyScope BeginScope()
    {
        return new TinyIoCResolver(_container.GetChildContainer());
    }

    public void Dispose()
    {
        // Handle dispose
    }
}

注意,这只是简单的实现,为了更好地看这篇文章 http://blog.i-m-code.com/2014/04/15/tinyioc-mvc-and-webapi-configuration/

不更新您的 Startup 以允许 WebApi 使用 DependencyResolver:

public static class Startup
{
    public static void ConfigureApp(IAppBuilder appBuilder, TinyIoCContainer container)
    {
       ...

        config.DependencyResolver = new TinyIoCResolver(container);

       ...
    }
}

最后在您的服务中注册您的依赖项 (StatelessServiceContext):

internal sealed class WebApiService : StatelessService
{
    public TinyIoCContainer Container { get; private set; }

    public WebApiService(StatelessServiceContext context)
        : base(context)
    {
        Container = new TinyIoCContainer();
        Container.Register<StatelessServiceContext>(context);
    }

    protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {
        return new ServiceInstanceListener[]
        {
            new ServiceInstanceListener(serviceContext => new OwinCommunicationListener(appBuilder => Startup.ConfigureApp(appBuilder, Container), serviceContext, ServiceEventSource.Current, "ServiceEndpoint"))
        };
    }
}

请注意,您必须对 Startup.ConfigureApp 方法的调用方式稍作更改才能提供您的容器。

现在您所要做的就是在 ApiController 的构造函数中添加 StatelessServiceContext 作为依赖项,并将其作为成员存储在您的控制器中:

public ValuesController(StatelessServiceContext serviceContext) 
{
    _serviceContext = serviceContext;
}

并在您的控制器操作中使用它:

ServiceEventSource.Current.ServiceMessage(_serviceContext, "this is my log message");

如何执行此操作、何时何地可以创建容器、如何解析控制器等等有很多变体。应该有很多关于如何设置 ASP.NET WebApi 和 IoC 依赖注入的指南。