gpt4 book ai didi

c# - ServiceStack 不会自动连接和注册 AppSettings

转载 作者:行者123 更新时间:2023-11-30 23:22:02 24 4
gpt4 key购买 nike

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

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");

// ...
}
}

怎么可能?怎么了?

最佳答案

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查看您是否有任何可能阻碍 AppHost 初始化的 StartUpErrors

关于c# - ServiceStack 不会自动连接和注册 AppSettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38960882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com