Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service Stack/MVC: "AppHostBase.Instance has already been set" error - but can't understand why/how to prevent

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?

like image 582
Program.X Avatar asked Jan 21 '12 16:01

Program.X


2 Answers

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.

like image 116
mythz Avatar answered Oct 03 '22 08:10

mythz


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!

like image 38
Vladimir Mitic Avatar answered Oct 03 '22 06:10

Vladimir Mitic