public virtual void ProcessRequest(HttpContext context) { //写入MVC的版本到HttpHeader里面 //AddVersionHeader(httpContext); //移除参数 //RemoveOptionalRoutingParameters(); //过滤文件请求 //string url = context.Request.RawUrl; //if (!url.Contains(".")) //{ //1.从当前的RouteData里面得到请求的控制器名称 string domainName = RouteData.RouteValue["addindomain"].ToString(); string controllerName = RouteData.RouteValue["controller"].ToString(); string actionName = RouteData.RouteValue["action"].ToString(); if (domainName.ToLower() != "AddInsDomain".ToLower()) { try { if (RouteData.RouteValue.ContainsKey("postData")) { object[] parameters = ((List <object>)RouteData.RouteValue["postData"]).ToArray(); object obj = AddIn.Entity.AddInManager.Execute(domainName, controllerName, actionName, parameters); context.Response.Write(obj.ToString()); } else { object obj = AddIn.Entity.AddInManager.Execute(domainName, controllerName, actionName, null); context.Response.Write(obj.ToString()); } } catch (Exception ex) { context.Response.Write(ex.Message); } context.Response.End(); } else { //2.得到控制器工厂 IAddInControllerFactory factory = new AddInControllerFactory(); //3.通过默认控制器工厂得到当前请求的控制器对象 IAddInController controller = factory.CreateController(RouteData, controllerName); if (controller == null) { context.Response.Write("找不到控制器!"); return; } try { //4.执行控制器的Action controller.Execute(RouteData); } catch (Exception ex) { Common.Log.Write("/" + domainName + "/" + controllerName + "/" + actionName + ":" + ex.ToString()); context.Response.Write(ex.Message); } finally { //5.释放当前的控制器对象 factory.ReleaseController(controller); } } //} }
protected void Application_Start() { // TODO maybe put this somewhere else (not in global.asax) // TODO maybe move all of this into the App class with "IAppConfig" // initialize logger TraceListener traceListener = new TextWriterTraceListener(Path.Combine(AppConfig.LogPath, string.Format("repository_{0:yyyy'-'MM'-'dd__HHmmss}.log", DateTime.Now))); // TODO introduce flags/settings for controlling logging levels, but for now include everything traceListener.Filter = new EventTypeFilter(SourceLevels.All); traceListener.TraceOutputOptions = TraceOptions.ThreadId | TraceOptions.DateTime; Web.TraceSources.Content.Listeners.Add(traceListener); Web.TraceSources.AddInManager.Listeners.Add(traceListener); Repository.TraceSources.ContentManagerSource.Listeners.Add(traceListener); Repository.TraceSources.ContentSearcherSource.Listeners.Add(traceListener); // this might be stupid, but it fixes things for iisexpress Directory.SetCurrentDirectory(HostingEnvironment.ApplicationPhysicalPath); // set up add-in system AddInSource officalSource = new AddInSource("Official LostDoc repository add-in feed", AppConfig.AddInRepository, isOfficial: true); // intialize MEF // core 'add-ins' var currentAssembly = Assembly.GetExecutingAssembly(); var assemblyName = currentAssembly.GetName(); string corePackageId = assemblyName.Name; string corePackageVersion = assemblyName.Version.ToString(); AggregateCatalog catalog = new AggregateCatalog(); // load other sources from site-settings (not config) AddInRepository repository = new AddInRepository(officalSource); AddInManager addInManager = new AddInManager(repository, AppConfig.AddInInstallPath, AppConfig.AddInPackagePath); // when the catalog changes, discover and route all ApiControllers catalog.Changed += (sender, args) => this.UpdateWebApiRegistry(args); //// TODO for debugging only //Debugger.Break(); //Debugger.Launch(); // now register core libs catalog.Catalogs.Add(new AddInCatalog(new ApplicationCatalog(), corePackageId, corePackageVersion)); // hook event so that installed add-ins get registered in the catalog, if composition occurs after this fires // or if recomposition is enabled, no restart should be requried addInManager.Installed += (sender, args) => catalog.Catalogs.Add(new AddInCatalog(new DirectoryCatalog(args.InstallationPath), args.Package.Id, args.Package.Version)); // delete and redeploy all installed packages, this will trigger the Installed event ^ // this acts as a crude "remove/overwrite plugins that were in use when un/installed" hack addInManager.Restore(); // create container CompositionContainer container = new CompositionContainer(catalog); // set up template resolver var lazyProviders = container.GetExports <IFileProvider>(ContractNames.TemplateProvider); var realProviders = lazyProviders.Select(lazy => lazy.Value); TemplateResolver templateResolver = new TemplateResolver(realProviders.ToArray()); // load template Template template = new Template(container); template.Load(templateResolver, AppConfig.Template); // set up content manager ContentManager contentManager = new ContentManager(new ContentSettings { ContentPath = AppConfig.ContentPath, // TODO make this configurable IgnoreVersionComponent = VersionComponent.Patch, RepositoryPath = AppConfig.RepositoryPath, Template = template }); // set up notifaction system NotificationManager notifications = new NotificationManager(); // initialize app-singleton App.Initialize(container, contentManager, addInManager, notifications, traceListener); // MVC init AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); // inject our custom IControllerFactory for the Admin interface IControllerFactory oldControllerFactory = ControllerBuilder.Current.GetControllerFactory(); IControllerFactory newControllerFactory = new AddInControllerFactory(AdministrationAreaRegistration.Name, container, oldControllerFactory); ControllerBuilder.Current.SetControllerFactory(newControllerFactory); // TODO figure out if we actually need this // hook in our MEF based IHttpController instead of the default one //GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerTypeResolver), new AddInHttpControllerTypeResolver(App.Instance.Container)); }