Hosting和DI注入控制器
组件在新版本中支持使用.NET Core的DependencyInjection组件注入控制器,这样极大方便地对控器一些功能类型通过DI注入到控器中。
引用组件

定义Host服务
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组件后,就可以在控制器中定义注入的类型。
访问Beetlex的Github

浙公网安备 33010602011771号