c#-从Service Fabric WebAPI控制器写入ServiceEventSource
作者:互联网
在我的有状态服务中,可以通过调用以下内容来写入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解决方案一样.
如果使用现成提供的OwinCommuncationController和Startup类,则可以初始化容器并将其添加到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展示它,但对任何IoC都采用类似的方法(Windsor,Unity,Ninject,AutoFac …).
对于TinyIoC,添加NuGet TinyIoC和TinyIoC.AspNetExtensions,并添加实现IDependencyResolver的类:
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/
不更新您的启动,以允许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方法的方式进行一些更改,以便也提供容器.
现在,您要做的就是将StatelessServiceContext作为依赖项添加到ApiController的构造函数中,并将其作为成员存储在控制器中:
public ValuesController(StatelessServiceContext serviceContext)
{
_serviceContext = serviceContext;
}
并在控制器动作中使用它:
ServiceEventSource.Current.ServiceMessage(_serviceContext, "this is my log message");
如何执行此操作,何时何地可以创建容器,如何解析控制器等都有很多变体.关于如何设置ASP.NET WebApi和IoC依赖项注入,应该有大量指南.
标签:asp-net-web-api,azure-service-fabric,c 来源: https://codeday.me/bug/20191111/2022224.html