3

It was working then it suddenly stopped working. I must have did something. I'm calling my service from an MVC controller. I'm using NHibernate with a service runner I found in this SO answer Service.Session and Service.RequestContext are null.

public class AppHost : AppHostBase
{
    static ILog log;

    //Tell ServiceStack the name and where to find your web services
    public AppHost() : base("Play it Forward", typeof(GiveawayService).Assembly)
    {
        LogManager.LogFactory = new Log4NetFactory();
        log = LogManager.GetLogger(typeof(AppHost));
        // This is a singleton NHibernate session factory.
        Container.Register(SessionManager.SessionFactory);
    }

    public override void Configure(Container container)
    {
        SetConfig(
            new EndpointHostConfig
            {
                GlobalResponseHeaders =
                {
                    { "Access-Control-Allow-Origin", "*" },
                    { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
                },
                EnableFeatures = Feature.All.Remove(GetDisabledFeatures()),
                ServiceStackHandlerFactoryPath = "api"
            });

        var appSettings = new AppSettings();
        Plugins.Add(
            new AuthFeature(
                () => new SteamUserSession(),
                new IAuthProvider[] { new SteamOpenIdOAuthProvider(appSettings) }));
        Plugins.Add(new SessionFeature());

        container.Register<ICacheClient>(c => new MemoryCacheClient());
        // TODO: Implement Redis
        //container.Register<ICacheClient>(c => new BasicRedisClientManager());
        container.RegisterAutoWired<GiveawayService>();
        container.RegisterAutoWired<UserService>();
        container.RegisterAutoWired<SecureUserService>();
        container.RegisterAutoWired<GamesService>();

        ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
        ServiceStackController.CatchAllController = reqCtx => container.TryResolve<HomeController>();
        log.InfoFormat("AppHost Configured: {0}", DateTime.Now);
    }

    public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext actionContext)
    {
        return new BaseServiceRunner<TRequest>(this, actionContext);
    }
}

This one of my services where I'm having the problem.

public object Get(FindClosedGiveaways request)
{
    if (request.Take > 50 || request.Take == null)
    {
        request.Take = 50;
    }
    if (request.StartIndex == null)
    {
        request.StartIndex = 0;
    }
        //return RequestContext.ToOptimizedResultUsingCache(
        //    base.Cache,
        //    cacheKey,
        //    TimeSpan.FromMinutes(15),
        //    () =>
        //    {
                var session = RequestContext.GetItem("session") as ISession;
                IQueryable<Entities.Giveaway> q =
                    session.Query<Entities.Giveaway>()
                        .Fetch(x => x.Giveable)
                        .Fetch(x => x.User)
                        .Fetch(x => x.Winner)
                        .Where(x => x.EndDate > DateTime.UtcNow.Subtract(new TimeSpan, 0, 0, 0)));

                if (request.UserSteamID != null)
                {
                    q = q.Where(x => request.UserSteamID == x.User.SteamID);
                }
                else if (request.UserID != null)
                {
                    q = q.Where(x => request.UserID == x.User.ID);
                }
                q = q.OrderByDescending(x => x.EndDate).Skip(request.StartIndex.Value).Take(request.Take.Value);
                List<GiveawaySummary> list = q.ConvertAll(
                    x =>
                    {
                        // If GiveawaySummary contains another class as a property, it 
                        var giv = x.TranslateTo<GiveawaySummary>();
                        giv.GifterUsername = x.User.Username;
                        giv.GifterSteamID = x.User.SteamID;
                        giv.WinnerUsername = x.Winner.Username;
                        giv.WinnerSteamID = x.Winner.SteamID;
                        if (x.Giveable.Type == "PiF.Entities.Subscription")
                        {
                            giv.Subscription = x.Giveable.TranslateTo<Subscription>();
                        }
                        else
                        {
                            giv.App = x.Giveable.TranslateTo<App>();
                        }
                        return giv;
                   });
                return list;
        //});
    }
Community
  • 1
  • 1
Robert Baker
  • 25,593
  • 1
  • 20
  • 21
  • Some more information about "stopped working" would be nice. Does it just stall, is there any error message? What is the normal behaviour to the current one (as exact as possible)? – Azrael Jul 15 '13 at 23:16
  • I mean it used to work as in RequestContext should be injected by the IoC and now for some reason it's not. I uninstalled all nuget packages, reinstalled them. – Robert Baker Jul 16 '13 at 01:49
  • In order to access `RequestContext` you need to make sure your service implements `IRequiresRequestContext`. Not only does this interface provide the actual `RequestContext` property, but it signals to ServiceStack to inject the current `RequestContext` into the service. I believe if your service inherits from ServiceStack's `Service` class this should be taken care of. – rossipedia Jul 16 '13 at 06:24
  • My service does implement Service. It was working, I must have did something to cause it. I did install the AutoFunq nuget package but I uninstalled it. I was kind of all over the place in code when I noticed it broke. It's not injecting a few properties it should be. – Robert Baker Jul 16 '13 at 13:53
  • Does the issue appear in all services or only this one? – Mike Jul 18 '13 at 01:07
  • All services. I looked at my webconfig's to see if something is screwed up there, but it doesn't look like it. – Robert Baker Jul 18 '13 at 03:08

0 Answers0