CustomUserSession的CustomProperty始终为null

时间:2013-10-24 19:51:32

标签: asp.net-mvc session null servicestack

我是ServiceStack的新手,我正在尝试在ServiceStack和ASP MVC 4 Controller之间共享会话。我一直在关注bootstrap api项目,所以我有:

AppHost.cs

ControllerBase<CustomUserSession>

public class CustomUserSession : AuthUserSession
{
    public string CustomProperty1 { get; set; }
    public string CustomProperty2 { get; set; }    
}

public class AppHost
    : AppHostBase
{       
    public AppHost() //Tell ServiceStack the name and where to find your web services
        : base("StarterTemplate ASP.NET Host", typeof(HelloService).Assembly) { }

    public override void Configure(Funq.Container container)
    {
        Plugins.Add(new SessionFeature());
        container.Register<ICacheClient>(new MemoryCacheClient());

        //Set JSON web services to return idiomatic JSON camelCase properties
        ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;

        //Configure User Defined REST Paths
        Routes
          .Add<Hello>("/hello")
          .Add<Hello>("/hello/{Name*}");

        //Uncomment to change the default ServiceStack configuration
        //SetConfig(new EndpointHostConfig {
        //});

        //Enable Authentication
        //ConfigureAuth(container);

        //Register all your dependencies
        container.Register(new TodoRepository());           

        //Set MVC to use the same Funq IOC as ServiceStack
        ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
        ServiceStackController.CatchAllController = reqCtx => container.TryResolve<HomeController>();
    }

    /* Uncomment to enable ServiceStack Authentication and CustomUserSession
    private void ConfigureAuth(Funq.Container container)
    {
        var appSettings = new AppSettings();

        //Default route: /auth/{provider}
        Plugins.Add(new AuthFeature(() => new CustomUserSession(),
            new IAuthProvider[] {
                new CredentialsAuthProvider(appSettings), 
                new FacebookAuthProvider(appSettings), 
                new TwitterAuthProvider(appSettings), 
                new BasicAuthProvider(appSettings), 
            })); 

        //Default route: /register
        Plugins.Add(new RegistrationFeature()); 

        //Requires ConnectionString configured in Web.Config
        var connectionString = ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString;
        container.Register<IDbConnectionFactory>(c =>
            new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));

        container.Register<IUserAuthRepository>(c =>
            new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>()));

        var authRepo = (OrmLiteAuthRepository)container.Resolve<IUserAuthRepository>();
        authRepo.CreateMissingTables();
    }
    */

    public static void Start()
    {
        new AppHost().Init();
    }
}

就像启动ControllerBase一样:

public class ControllerBase : ServiceStackController<CustomUserSession>
{ 
}

我的HomeController继承自ControllerBase:

public class HomeController : ControllerBase
{

public virtual ActionResult Index()
{
    ViewBag.Message = "Sharing Sessions Btw SS and ASP MVC";
    return View();
}

[HttpGet]
public ActionResult Login()
{
    return View();
}

[HttpPost]
public ActionResult Login(User request)
{
    var user_Session = SessionFeature.GetOrCreateSession<CustomUserSession>(CacheClient);
    return Json(user_Session);
}

public ActionResult Contact()
{
    ViewBag.Message = "Your contact page.";
    return View();
}
}

所以问题是当我在Login窗体上单击Submit时,它调用HomeController的Login(用户请求)方法,CustomUserSession.CustomProperty1和CustomUserSession.CustomProperty2始终为null!我的意思是,如果我在这个方法中调用SessionFeature.GetOrCreateSession(CacheClient),为什么它们为null;

希望你们能帮助我!

0 个答案:

没有答案