AppHostBase instance not set

2019-08-11 21:51发布

问题:

Like many of my ServiceStack questions, I'm sure this will be pretty easy.

I have an asp.net MCC4 application in which I am using ServiceStack for authentication. Because the cookies generated by the ServiceStack client aren't shared, I've followed the response in questions like this to resolve the issue:

ServiceStack Session always null in MVC Controller

In my client I have this snippet of code:

            var authService = AppHostBase.Resolve<AuthService>();
            authService.RequestContext = System.Web.HttpContext.Current.ToRequestContext();
            var AuthResponse = authService.Authenticate(new Auth
            {
                provider = "credentials",
                UserName = user.user_id,
                Password = user.password,
                RememberMe = true
            });


            if (AuthResponse.SessionId != null)
            {
                Session["IsAuthenticated"] = true;
                Session["UserID"] = AuthResponse.UserName;
                FormsAuthentication.SetAuthCookie(user.user_id, true);
                return Redirect("/home");
            }
            else
            {
                Session["IsAuthenticated"] = false;
            }

When I try and run the application, I get the error:

"AppHostBase is not initialized."

What might I be missing? I apologize in advance for my inexperience with ASP.net in general.

Update

I should point out that this is only an MCV4 client application that will be using a ServiceStack API that is part of a different project.

Update 2

I think this might be a part of my not perfect understanding of SS and how it would fit into a web application such as this. I believe what I may really be after is understanding just how this ASP.net client can hook back up with an existing session on the ServiceStack API server.

回答1:

you need to configure your appbase under the mvc4 context, just add and run your apphost configuration and call it whitin the global asax file

in my case I'm running my servicehost in a blank web app but that could work for you mvc4 as well.

    protected void Application_Start(object sender, EventArgs e)
    {

        new AppServiceHost().Init();
        RouteTable.Routes.MapHubs();
    }

in your serviceStack Application:

 public class AppServiceHost : AppHostBase
    {
        public AppServiceHost()
            : base("Rest Service", typeof (AnyServiceYouHave).Assembly)
        {
            SetConfig(new EndpointHostConfig
                {
                    //your configuration
                });
        }

         public override void Configure(Container container)
        {
            //Your configuration
        }
}

Edit: for sharing your session make user you have configured a cache provider in service stack

container.Register<ICacheClient>(new MemoryCacheClient()); //or any other