它正在工作然后它突然停止工作。我一定做了点什么。 我正在从MVC控制器调用我的服务。我正在使用NHibernate和我在SO answer中找到的服务运行器 Service.Session和Service.RequestContext为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);
}
}
这是我遇到问题的服务之一。
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;
//});
}