Service Stack/MVC: “AppHostBase.Instance has alrea

2019-06-16 04:30发布

问题:

I'm trying to implement ServiceStack into my MVC3 project, and am following the tutorial at: http://www.servicestack.net/ServiceStack.Hello/

My Global.asax.cs now looks like:

    public class MvcApplication : System.Web.HttpApplication
    {
        public class Hello
        {
            public string Name { get; set; }
        }

        public class HelloResponse
        {
            public string Result { get; set; }
        }

        public class HelloService : IService<Hello>
        {
            public object Execute(Hello request)
            {
                return new HelloResponse { Result = "Hello, " + request.Name };
            }
        }

        public class HelloAppHost : AppHostBase
        {
            //Tell Service Stack the name of your application and where to find your web services
            public HelloAppHost() : base("Hello Web Services", typeof(HelloService).Assembly) { }

            public override void Configure(Container container)
            {
                //register user-defined REST-ful urls
                Routes
                  .Add<Hello>("/hello")
                  .Add<Hello>("/hello/{Name}");
            }
        }


        protected void Application_Start()
        {
            new HelloAppHost().Init();

        }
    }
}

... as per the instructions.

However, when I call the Init() method, I get the exception:

InvalidDataException: AppHostBase.Instance has already been set

Googling around for this provides no help, except the source code at http://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Common/ServiceStack.WebHost.Endpoints/AppHostBase.cs?r=1322

Looking at this, somehow, the Instance is being set. But how?

What could be causing this?

回答1:

If you've installed ServiceStack via the NuGet ServiceStack.Host.Mvc project it automatically creates the AppHost class for you inside:

App_Start/ServiceStackFramework.cs

Your example looks like you may have added your own AppHost as well. So if you remove this AppHost and use the provided one you should no longer have this error.



回答2:

The error happened to me as well after i renamed my project. Make sure you go in your bin folder and delete all dlls, in case old one from the pre-renamed project dlls are still in there!