2

I have some project which use ServiceStack. I have strange problem with resolving IEnumerable. I have six services implement some logic and one service, which manage they.

This is code my service:

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);
    }
}

This is 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();
    }
}

This is 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();
        }
    }
}

This is my RestApi class:

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);
                    }
                });
        }
    }

I have strange behavior: sometimes my programm gets data too slow. When I debugs programm I see that breakpoint stop on line _container.Resolve<T>() and then nothing happens. Does anyone have a similar problem and how you fixed it?

I would be grateful to receive an answer.

Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75
Anna Miroshnichenko
  • 1,143
  • 1
  • 7
  • 24
  • When i first started using ServiceStack, i was already using Castle Windsor, so i set up WindsorContainerAdapter. However, the system seemed too heavyweight (w/NHibernate too). I found it much easier to do everything the "servicestack way". Coming from Windsor, you will find it easy to cut over to ServiceStack Funq directly. Also, if _container.Resolve() is failing, it is probably due to Castle Windsor. – Raul Nohea Goodness Feb 27 '14 at 23:09

0 Answers0