sql-serverserviceservicestackidbconnection

Service's Db is null in ServiceStack v4


I've used the free version of ServiceStack for a couple of years now and have recently purchased v4 for use in a project.

I have read through the release notes and associated blogs but cannot find any guidance on the issue I am having...

Having made all corresponding changes defined in the release notes I am stuck with a NullReferenceException on ServiceStack.Service.get_Db(). Here's some code snippets that might help:

Global.asax...

using Funq;
using ServiceStack;
using ServiceStack.OrmLite;
using ServiceStack.Data;
using ServiceStack.Caching;
using ServiceStack.MiniProfiler;
using ServiceInterface;
using System.Web;
using System;

namespace WebRole
{
    public class AppHost : AppHostHttpListenerBase
    {
        public AppHost() : base("Web Services", typeof(CompaniesService).Assembly) { }

        public override void Configure(Container container)
        {
            container.Register(new OrmLiteConnectionFactory(@"Server=tcp:myServer.database.windows.net,1433;Database=myDatabase;User ID=myUser;Password=myPassword;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;", SqlServerDialect.Provider));

            using (var resetCompanies = container.Resolve<ResetCompaniesService>())
            {
                resetCompanies.Any(null);
            }

ResetCompanies.cs...

using System;
using System.Collections.Generic;
using ServiceStack.OrmLite;
using ServiceStack;
using ServiceModel.Types;

namespace ServiceInterface
{
    [Route("/reset-Companies", "GET,POST")]
    [Api("Resets the database back to the original Top 3 movies.")]
    public class ResetCompanies : IReturn<ResetCompaniesResponse>
    {
    }

    /// <summary>
    ///     Define your ServiceStack web service response (i.e. Response DTO).
    /// </summary>
    public class ResetCompaniesResponse
    {
    }



    /// <summary>
    ///     Create your ServiceStack rest-ful web service implementation.
    /// </summary>
    public class ResetCompaniesService : Service
    {
        public static List<Company> Top3Companies = new List<Company>
                                                   {
                                                       new Company
                                                           {
                                                               Code = "C1",
                                                               Name = "Company 1"
                                                           },
                                                       new Company
                                                           {
                                                               Code = "C2",
                                                               Name = "Company 2"
                                                           },
                                                       new Company
                                                           {
                                                               Code = "C3",
                                                               Name = "Company 3"
                                                           }
                                                   };

        public object Any(ResetCompanies request)
        {
            //Executes the specified delegate against the configured database.
            Db.DropAndCreateTable<Company>();
            Db.SaveAll(Top3Companies);

            return new ResetCompaniesResponse();
        }
    }
}

The NullReferenceException occurs at

Db.DropAndCreateTable<Company>();

Here's the stack trace

at ServiceStack.Service.get_Db()
at ServiceInterface.ResetCompaniesService.Any(ResetCompanies request) in c:\Users\me\Documents\Visual Studio 2013\Projects\Project1\ServiceInterface\ResetCompanies.cs:line 51
at WebRole.AppHost.Configure(Container container) in c:\Users\me\Documents\Visual Studio 2013\Projects\Project1\WebRole\Global.asax.cs:line 23
at ServiceStack.ServiceStackHost.Init()
at WebRole.Global.Application_Start(Object sender, EventArgs e) in c:\Users\me\Documents\Visual Studio 2013\Projects\Project1\WebRole\Global.asax.cs:line 35

Has anyone got any ideas? Certainly looks like another breaking change in v4 that I missed or am unaware of...


Solution

  • The built-in Service class expects to resolve an IDbConnectionFactory which you can do by registering OrmLiteConnectionFactory as an IDbConnectionFactory, e.g:

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