Storm
Storm

Reputation: 4475

How do I get ServiceStack to work in an MVC4 project?

I created a new MVC 4 Project, and updated it from NuGet with all required ServiceStack packages.

I added this to my Web.config:

<location path="ss">
  <system.web>
    <!-- httpHandlers added for ServiceStack, this will work for IIS6 and Below -->
    <httpHandlers>
      <add path="*" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*"/>
    </httpHandlers>
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
      <add name="ServiceStack.Factory" path="*" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true"/>
    </handlers>
  </system.webServer>
</location>

My AppHost:

namespace MySql_Test.App_Start
{
    public class ApplicationHost : AppHostHttpListenerBase
    {
        public ApplicationHost()
            : base("PersonService", typeof(PersonService).Assembly)
        {
        }

        public override void Configure(Funq.Container container)
        {
            SetConfig(new HostConfig { HandlerFactoryPath = "ss" });

            container.Register<IDbConnectionFactory>(
                new OrmLiteConnectionFactory(ConfigurationManager.ConnectionStrings["DuDealsConnStr"].ConnectionString,
                    ServiceStack.OrmLite.MySql.MySqlDialectProvider.Instance));
        }
    }

    public class PersonService : Service
    {
    }
}

My Global.cs:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        (new ApplicationHost()).Init();

        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

When I build and run the project and try to navigate to localhost/ss/metadata it just keeps giving me a 404 page.

Upvotes: 1

Views: 122

Answers (1)

Scott
Scott

Reputation: 21521

The default MVC4 project uses WebApi by default. You need to prevent WebApi from registering to receive the requests that you want ServiceStack to handle.

See the ServiceStack documentation, Create your first webservice instructions:

Disable WebApi from the default MVC4 VS.NET template
If you are using MVC4 then you need to comment line in global.asax.cs to disable WebApi

//WebApiConfig.Register(GlobalConfiguration.Configuration);

So set your Application_Start to simply be:

protected void Application_Start()
{
    (new ApplicationHost()).Init();
}

If you want ServiceStack to work alongside WebApi then you will need to tell WebApi to not handle requests starting with your ss/ path:

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

    AreaRegistration.RegisterAllAreas();

    // Ignore ServiceStack routes
    RouteTable.Routes.IgnoreRoute("ss/{*pathInfo}");

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

Hope that helps.

Upvotes: 3

Related Questions