2016-08-15 31 views
2

ServiceStack(4.0.62)不会注册并自动连线AppSettings属性。我甚至不知道如何调试这种情况,也许有人可以解释它。ServiceStack不会自动连线并注册AppSettings

所以,我有一个基于ServiceStack自托管控制台窗口的应用程序(默认的IoC Funq使用):

SomeService的AppSettings属性没有初始化所有:

public class SomeService : Service 
    { 
     public IAppSettings AppSettings { get; set; } 

     public SomeResponse Get(SomeRequest request) 
     { 
      // Exception: AppSettings == null 
      var someValue = AppSettings.Get<string>("Key1"); 

      // ... 
     } 
    } 

如何可以吗?怎么了?

回答

2

ServiceStack默认为register the IAppSettings in Funq

我也是在一个新的自我主机控制台应用程序验证了这一点使用例如:

public class AppHost : AppHostHttpListenerBase 
{ 
    public AppHost() : base("SomeServer", typeof(SomeService).Assembly) {} 
    public override void Configure(Container container) 
    { 
     SetConfig(new HostConfig { 
      DefaultContentType = MimeTypes.Json, 
      DebugMode = true, 
     }); 

     AppSettings = new DictionarySettings(new Dictionary<string, string> { 
      { "Key1", "Value1" }, 
      { "Key2", "Value2" }, 
     }); 
    } 
} 

[Route("/appsettings/{Key}")] 
public class SomeRequest 
{ 
    public string Key { get; set; } 
} 

public class SomeResponse 
{ 
    public string Value { get; set; } 
} 

public class SomeService : Service 
{ 
    public IAppSettings AppSettings { get; set; } 

    public SomeResponse Get(SomeRequest request) 
    { 
     return new SomeResponse { Value = AppSettings.Get<string>(request.Key) }; 
    } 
} 

是开头:

class Program 
{ 
    static void Main(string[] args) 
    { 
     new AppHost().Init().Start("http://*:8088/"); 
     "ServiceStack Self Host with Razor listening at http://127.0.0.1:8088".Print(); 
     Process.Start("http://127.0.0.1:8088/"); 

     Console.ReadLine(); 
    } 
} 

这是按预期工作,即:

检查?debug=requestinfo以查看是否有任何StartUpErrors这可能阻碍了AppHost的初始化。

+0

感谢您的支持。我阅读SS文档,并确定它必须默认注册IAppSettings。我尝试了你的示例 - 一切都很好。但在我的真实项目中,注册完全不起作用。一切除了注册。这是令人难以置信的,'StartUpErrors'为空,请参阅[完整日志](http://pastebin.com/yMs18HQ8)。我对ServiceStack版本错了,它是4.0.56,现在是4.0.62,但没有任何变化。在我的情况下,AppHost以“http:// +:8088 /”开头,其他选项相同。 –

+1

@AlexNewman从这里无法确定问题所在。您能否发布一个独立的repro,我可以在本地运行以查看此问题,理想情况下可以在Google Docs,Dropbox等的新Github repo或.zip共享中使用。 – mythz

+0

这是我的错,对不起,这是一个非常糟糕的问题想法使用服务的构造函数来实现一些AppSettings的行动。问题已经结束,非常感谢。 –