DasAmigo
DasAmigo

Reputation: 333

How do I build an ServiceStack.Host.Mvc project?

I'm trying to test out ServiceStack for an MVC 4 App in VS2012:

However I immediately get the following 3 errors on build even after following the special instructions for MvC.

They're all related to the following code in AppHost.cs

// Requires ConnectionString configured in Web.Config
var connectionString = ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString;
container.Register<IDbConnectionFactory>(c =>
    new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));

Upvotes: 1

Views: 484

Answers (2)

mythz
mythz

Reputation: 143379

That code is not being used by any of the example services so you can just comment it out, e.g:

//container.Register<IDbConnectionFactory>(c =>
//    new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));

Using OrmLite in ServiceStack

But if you want to use SqlServer with ServiceStack's OrmLite you can install it from NuGet:

PM> Install-Package ServiceStack.OrmLite.SqlServer

Using an alternative RDBMS

Otherwise you can install your preferred RDBMS solution, e.g. for PostgreSQL, install:

PM> Install-Package ServiceStack.OrmLite.PostgreSQL

And you would change it to use the PostgreSQL provider, e.g:

container.Register<IDbConnectionFactory>(c =>
    new OrmLiteConnectionFactory(connectionString, PostgreSqlDialect.Provider));

Querying your database inside ServiceStack Services and Razor Views

You can then use the Db database inside your services or razor views like:

In your AppHost:

using (var db = container.Resolve<IDbConnectionFactory>().Open()) {
    Db.CreateTableIfNotExists<TestDb>();
}

Use Db property in your ServiceStack services (or Razor Views):

public object Any(TestDb request)
{
    Db.Insert(request);
    return Db.SingleById<TestDb>(request.Id);
}

*TestDb can be any POCO, Request DTO, etc.

Upvotes: 2

ianschol
ianschol

Reputation: 686

Check to make sure the dependency packages are in your references and using'ed as necessary.

  <dependency id="WebActivator" version="1.5" />
  <dependency id="ServiceStack.Mvc" version="4.0" />
  <dependency id="ServiceStack.Server" version="4.0" />
  <dependency id="ServiceStack.OrmLite.SqlServer" version="4.0" />

List pulled from https://github.com/ServiceStack/ServiceStack/blob/master/NuGet/ServiceStack.Host.Mvc/servicestack.host.mvc.nuspec

Upvotes: 2

Related Questions