1

我已经使用 ServiceStack 的免费版本几年了,最近购买了 v4 用于一个项目。

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

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

全球.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);
            }

重置公司.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 中的另一个重大变化......

4

1 回答 1

1

内置的Service期望解析一个 IDbConnectionFactory,您可以通过将OrmLiteConnectionFactory注册为来完成IDbConnectionFactory,例如:

container.Register<IDbConnectionFactory>(c =>
    new OrmLiteConnectionFactory(connString, SqlServerDialect.Provider));
于 2014-01-26T13:01:14.113 回答