我有一些使用 ServiceStack 的项目。我在解决 IEnumerable 时遇到了奇怪的问题。我有六个服务实现了一些逻辑和一个服务来管理它们。
这是我的服务代码:
public class TerminologyManagerService : ITerminologyManagerService
{
readonly IEnumerable<ITerminologyRetriever> _terminologyRetrievers;
public TerminologyManagerService(IEnumerable<ITerminologyRetriever> terminologyRetrievers)
{
_terminologyRetrievers = terminologyRetrievers;
}
public IEnumerable<ITerminologyRetriever> GetTerminologyRetrievers(string name)
{
return _terminologyRetrievers
.Where(x => x.CanHandle(name))
.OrderByDescending(x => x.Priority);
}
}
这是 WindsorContainerAdapter:
public class WindsorContainerAdapter : IContainerAdapter, IDisposable
{
readonly IWindsorContainer _container;
public WindsorContainerAdapter(IWindsorContainer container)
{
_container = container;
}
public T TryResolve<T>()
{
if (_container.Kernel.HasComponent(typeof (T)))
return (T) _container.Resolve(typeof (T));
return default(T);
}
public T Resolve<T>()
{
return _container.Resolve<T>();
}
public void Dispose()
{
_container.Dispose();
}
}
这是 WindsorDependencyResolver:
class WindsorDependencyResolver : IDependencyResolver
{
readonly IKernel _container;
public WindsorDependencyResolver(IKernel container)
{
_container = container;
}
public IDependencyScope BeginScope()
{
return new WindsorDependencyScope(_container);
}
public object GetService(Type serviceType)
{
return _container.HasComponent(serviceType)
? _container.Resolve(serviceType)
: null;
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _container.ResolveAll(serviceType).Cast<object>();
}
public void Dispose()
{
}
class WindsorDependencyScope : IDependencyScope
{
readonly IKernel _container;
readonly IDisposable _scope;
public WindsorDependencyScope(IKernel container)
{
_container = container;
_scope = container.BeginScope();
}
public object GetService(Type serviceType)
{
return _container.HasComponent(serviceType)
? _container.Resolve(serviceType)
: null;
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _container.ResolveAll(serviceType).Cast<object>();
}
public void Dispose()
{
_scope.Dispose();
}
}
}
这是我的 RestApi 类:
public class RestApi : AppHostBase
{
readonly IWindsorContainer _container;
public RestApi(IWindsorContainer container) : base("REST Api", Assembly.GetExecutingAssembly())
{
LogManager.LogFactory = new Log4NetFactory(true);
_container = container;
}
public override void Configure(Container container)
{
container.Adapter = new WindsorContainerAdapter(_container);
RequestFilters.Add((httpReq, httpResp, requestDto) =>
{
if (requestDto is BaseRequest)
{
var baseRequest = requestDto.Cast<BaseRequest>();
SystemId systemId = SystemId.Parse(baseRequest.SystemId);
container.Resolve<IOpenHealthEnvironment>().Init(systemId);
}
});
}
}
我有奇怪的行为:有时我的程序获取数据太慢。当我调试程序时,我看到断点在线停止_container.Resolve<T>()
,然后什么也没有发生。有没有人有类似的问题,你是如何解决的?
我将不胜感激收到答复。