-
-
Notifications
You must be signed in to change notification settings - Fork 175
Hosting和DI注入控制器
Henry edited this page Sep 10, 2019
·
1 revision
组件在新版本中支持使用.NET Core
的DependencyInjection
组件注入控制器,这样极大方便地对控器一些功能类型通过DI
注入到控器中。
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services
.AddSingleton<UserService>()
.UseBeetlexHttp(o => {
o.Port = 8080;
o.LogToConsole = true;
o.LogLevel = BeetleX.EventArgs.LogType.Debug;
o.SetDebug();
}, typeof(Program).Assembly);
});
builder.Build().Run();
引用组件并添加BeetleX.FastHttpApi.Hosting
名称空间后就可以执行UseBeetlexHttp
把服务添加到Host
.由样这个扩展组件也引入了DependencyInjection
所以可能通过相关功能添加对应注入的类型。
[Controller]
public class Home
{
public Home(HttpApiServer server,UserService userService)
{
mHttpApiServer = server;
mUserService = userService;
}
private UserService mUserService;
private HttpApiServer mHttpApiServer;
public string Hello(string name)
{
return $"hello {name}";
}
}
public class UserService
{
public bool Login(string name,string pwd)
{
return name == "admin" && pwd == "123456";
}
}
引入DependencyInjection
组件后,就可以在控制器中定义注入的类型。