using System; using System.Collections.Generic; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using MQTTnet.AspNetCore; using MQTTnet.AspNetCore.Extensions; using Plugin; using WalkingTec.Mvvm.Core; using WalkingTec.Mvvm.Core.Extensions; using WalkingTec.Mvvm.Core.Support.FileHandlers; using WalkingTec.Mvvm.Mvc; namespace IoTGateway { public class Startup { public IConfiguration ConfigRoot { get; } public Startup(IWebHostEnvironment env, IConfiguration config) { ConfigRoot = config; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDistributedMemoryCache(); services.AddWtmSession(36000, ConfigRoot); services.AddWtmCrossDomain(ConfigRoot); services.AddWtmAuthentication(ConfigRoot); services.AddWtmHttpClient(ConfigRoot); services.AddWtmSwagger(); services.AddWtmMultiLanguages(ConfigRoot); services.AddMvc(options => { options.UseWtmMvcOptions(); }) .AddJsonOptions(options => { options.UseWtmJsonOptions(); }) .SetCompatibilityVersion(CompatibilityVersion.Version_3_0) .ConfigureApiBehaviorOptions(options => { options.UseWtmApiOptions(); }) .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) .AddWtmDataAnnotationsLocalization(typeof(Program)); services.AddWtmContext(ConfigRoot, (options) => { options.DataPrivileges = DataPrivilegeSettings(); options.CsSelector = CSSelector; options.FileSubDirSelector = SubDirSelector; options.ReloadUserFunc = ReloadUser; }); //MQTTServer services.AddHostedMqttServer(mqttServer => { mqttServer.WithoutDefaultEndpoint(); }) .AddMqttConnectionHandler() .AddConnections(); services.AddHostedService(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IOptionsMonitor configs, DeviceService deviceService) { IconFontsHelper.GenerateIconFont(); app.UseExceptionHandler(configs.CurrentValue.ErrorHandler); app.UseStaticFiles(); app.UseWtmStaticFiles(); app.UseRouting(); app.UseWtmMultiLanguages(); app.UseWtmCrossDomain(); app.UseAuthentication(); app.UseAuthorization(); app.UseSession(); app.UseWtmSwagger(); app.UseWtm(); app.UseEndpoints(endpoints => { //MqttServerWebSocket endpoints.MapConnectionHandler("/mqtt", options => { options.WebSockets.SubProtocolSelector = MqttSubProtocolSelector.SelectSubProtocol; }); endpoints.MapControllerRoute( name: "areaRoute", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); app.UseWtmContext(); } /// /// Wtm will call this function to dynamiclly set connection string /// 框架会调用这个函数来动态设定每次访问需要链接的数据库 /// /// ActionContext /// Connection string key name public string CSSelector(ActionExecutingContext context) { //To override the default logic of choosing connection string, //change this function to return different connection string key //根据context返回不同的连接字符串的名称 return null; } /// /// Set data privileges that system supports /// 设置系统支持的数据权限 /// /// data privileges list public List DataPrivilegeSettings() { List pris = new List(); //Add data privilege to specific type //指定哪些模型需要数据权限 return pris; } /// /// Set sub directory of uploaded files /// 动态设置上传文件的子目录 /// /// IWtmFileHandler /// subdir name public string SubDirSelector(IWtmFileHandler fh) { return null; } /// /// Custom Reload user process when cache is not available /// 设置自定义的方法重新读取用户信息,这个方法会在用户缓存失效的时候调用 /// /// /// /// public LoginUserInfo ReloadUser(WTMContext context, string account) { return null; } } }