1

我的 mvc 项目中有 ServiceStack,我正在尝试在 ServiceStack 和 ASP MVC 之间共享会话。我按照https://github.com/ServiceStack/ServiceStack/wiki/Sessions中的所有步骤共享会话,但是当我尝试在我的 asp mvc 控制器中获取 UserSession 的值时,它显示一个 NULL VAUE...为什么是 Null?我有这个代码

应用主机.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();
        }
    }
}

HomeController.com

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

所以 user_Session 是空的……你能帮帮我吗?

4

1 回答 1

0

Try inheriting from ServiceStackController, e.g:

public class HomeController : ServiceStackController<CustomUserSession>
{
    [HttpPost]
    public ActionResult Login(User request)
    {
        CustomUserSession userSession = base.UserSession;
        return Json(userSession);
    }
}

Also if you want to use a Custom UserSession (i.e. other than the AuthUserSession default) you need to tell ServiceStack how to create it, by specifying it in the AuthFeature constructor:

Plugins.Add(new AuthFeature(() => new CustomUserSession(),
    new IAuthProvider[] {
        new CredentialsAuthProvider(appSettings), 
    })); 
于 2013-10-25T05:50:07.363 回答