Service Stack / MVC: & ldquo; AppHostBase.Instance has already been configured & rdquo; error - but does not understand why / how to prevent

advertisements

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?


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.