ServiceStack v4中Service的Db为空

时间:2014-01-26 12:43:11

标签: sql-server service servicestack idbconnection

我已经使用免费版ServiceStack几年了,并且最近购买了v4用于项目。

我已阅读发行说明及相关博客,但找不到有关我所遇问题的任何指导......

在发布说明中定义了所有相应的更改后,我在ServiceStack.Service.get_Db()上遇到了NullReferenceException。以下是一些可能有用的代码片段:

...的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();
        }
    }
}

NullReferenceException发生在

Db.DropAndCreateTable<Company>();

这是堆栈跟踪

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

有没有人有任何想法?当然看起来像v4中的另一个突破性变化,我错过了或者没有意识到......

1 个答案:

答案 0 :(得分:1)

内置的服务expects to resolve an IDbConnectionFactory,您可以将 OrmLiteConnectionFactory 注册为IDbConnectionFactory,例如:

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