ServiceStack v4 中服务的 Db 为空

Posted

技术标签:

【中文标题】ServiceStack v4 中服务的 Db 为空【英文标题】:Service's Db is null in ServiceStack v4 【发布时间】:2014-02-17 06:22:00 【问题描述】:

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

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

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

全球.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】:

内置的Service类expects to resolve an IDbConnectionFactory可以通过将OrmLiteConnectionFactory注册为IDbConnectionFactory来实现,例如:

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

【讨论】:

以上是关于ServiceStack v4 中服务的 Db 为空的主要内容,如果未能解决你的问题,请参考以下文章

ServiceStack.Redis之IRedisClient(转载)

如何在 ServiceStack 5.0 项目中使用来自 ServiceStack 4.0 的服务模型?

在运行时在 ServiceStack.OrmLite 中获取类的表名/避免硬编码表名

具有多个数据库服务器的 ServiceStack OrmLite

ServiceStack.Redis:配置为使请求和响应类/ dto是同一类?

如何在 ServiceStack OrmLite 中检索自动递增的 ID?