ServiceStack不会自动连接和注册AppSettings

本文关键字:注册 AppSettings 连接 ServiceStack | 更新日期: 2023-09-27 18:02:40

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

所以,我有一个基于servicestack的自托管控制台windows应用程序(默认使用IoC 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" },
            });
            // Enable plugins
            Plugins.Add(...);
        }
    }

someeservice的AppSettings属性根本没有初始化:

    public class SomeService : Service
    {
        public IAppSettings AppSettings { get; set; }
        public SomeResponse Get(SomeRequest request)
        {
            // Exception: AppSettings == null
            var someValue = AppSettings.Get<string>("Key1");
            // ...
        }
    }

这怎么可能?怎么了?

ServiceStack不会自动连接和注册AppSettings

ServiceStack默认在Funq中注册IAppSettings

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

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

按预期工作,即:

  • http://127.0.0.1:8088 appsettings/Key1。json => {"Value":"Value1"}

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