public ActionResult Create([Bind(Include = "Name, Description")] Service service, HttpPostedFileBase image = null) { if (ModelState.IsValid) { if (image != null) { service.ImageMimeType = image.ContentType; service.ImageData = new byte[image.ContentLength]; image.InputStream.Read(service.ImageData, 0, image.ContentLength); } try { if (!ServicesAccessor.CreateService(service)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } return(RedirectToAction("Index", "SiteAdmin")); } return(RedirectToAction("Index", "SiteAdmin")); }
public ActionResult Edit(Service newService, HttpPostedFileBase image = null) //([Bind(Include = "Id, Name, Description")] Service newService) { if (ModelState.IsValid) { if (image != null) { newService.ImageMimeType = image.ContentType; newService.ImageData = new byte[image.ContentLength]; image.InputStream.Read(newService.ImageData, 0, image.ContentLength); } var oldService = _repository.Services.FirstOrDefault(b => b.Id == newService.Id); try { if (ServicesAccessor.UpdateService(oldService, newService)) { return(RedirectToAction("Index", "SiteAdmin", new { area = "" })); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } } return(View(newService)); }
private IServiceLocator InitializeContainer(IServiceProvider services) { using (tracer.StartActivity(Strings.DevEnvFactory.CreatingComposition)) { // Allow dependencies of VS exported services. var composition = services.TryGetService <SComponentModel, IComponentModel>(); // Keep track of assemblies we've already added, to avoid duplicate registrations. var addedAssemblies = new Dictionary <string, Assembly>(); // Register built-in components from Clide assembly. var clideAssembly = Assembly.GetExecutingAssembly(); addedAssemblies.Add(clideAssembly.Location.ToLowerInvariant(), clideAssembly); // Register hosting package assembly. var servicesAssembly = services.GetType().Assembly; addedAssemblies[servicesAssembly.Location.ToLowerInvariant()] = servicesAssembly; var installPath = GetInstallPath(services); foreach (var providedAssemblyFile in services.GetType() .GetCustomAttributes <ProvideComponentsAttribute>(true) .Select(attr => Path.Combine(installPath, attr.AssemblyFile))) { if (!File.Exists(providedAssemblyFile)) { throw new InvalidOperationException(Strings.DevEnvFactory.ClideProvidedComponentsNotFound( services.GetType().FullName, Path.GetFileName(providedAssemblyFile), providedAssemblyFile)); } var providedAssembly = Assembly.LoadFrom(providedAssemblyFile); if (!addedAssemblies.ContainsKey(providedAssembly.Location.ToLowerInvariant())) { addedAssemblies.Add(providedAssembly.Location.ToLowerInvariant(), providedAssembly); } } var packageManifestFile = Path.Combine(installPath, "extension.vsixmanifest"); if (File.Exists(packageManifestFile)) { tracer.Info(Strings.DevEnvFactory.ExtensionManifestFound(packageManifestFile)); var manifestDoc = XDocument.Load(packageManifestFile); ThrowIfClideIsMefComponent(manifestDoc); // NOTE: we don't warn anymore in this case, since a single package may have // a mix of plain VS exports as well as clide components. // Since we use CommonComposition, only the types with the ComponentAttribute // will be made available in the Clide container, not the others, and no // duplicates would be registered. //WarnIfClideComponentIsAlsoMefComponent(packageManifestFile, manifestDoc); foreach (string clideComponent in GetClideComponents(manifestDoc)) { var assemblyFile = Path.Combine(installPath, clideComponent); tracer.Info(Strings.DevEnvFactory.ClideComponentDeclared(clideComponent, assemblyFile)); if (clideComponent == ClideAssembly) { tracer.Warn(Strings.DevEnvFactory.ClideNotNecessaryAsComponent(clideComponent)); continue; } if (!File.Exists(assemblyFile)) { throw new InvalidOperationException(Strings.DevEnvFactory.ClideComponentNotFound(packageManifestFile, clideComponent, assemblyFile)); } var componentAssembly = Assembly.LoadFrom(assemblyFile); if (!addedAssemblies.ContainsKey(componentAssembly.Location.ToLowerInvariant())) { addedAssemblies.Add(componentAssembly.Location.ToLowerInvariant(), componentAssembly); } } } else { tracer.Info(Strings.DevEnvFactory.ExtensionManifestNotFound(packageManifestFile)); } var catalog = new ComponentCatalog(addedAssemblies.Values.ToArray()); var providers = composition != null ? new ExportProvider[] { new ServicesExportProvider(services), composition.DefaultExportProvider } : new ExportProvider[] { new ServicesExportProvider(services) }; var container = new CompositionContainer(catalog, providers); // Make the service locator itself available as an export. var serviceLocator = new ServicesAccessor(services, new Lazy <IServiceLocator>(() => serviceLocators[services])); container.ComposeParts(serviceLocator); return(new ExportsServiceLocator(container)); } }
private IServiceLocator InitializeContainer(IServiceProvider services) { using (tracer.StartActivity(Strings.DevEnvFactory.CreatingComposition)) { // Allow dependencies of VS exported services. var composition = services.GetService<SComponentModel, IComponentModel>(); // Keep track of assemblies we've already added, to avoid duplicate registrations. var addedAssemblies = new Dictionary<string, Assembly>(); // Register built-in components from Clide assembly. var clideAssembly = Assembly.GetExecutingAssembly(); addedAssemblies.Add(clideAssembly.Location.ToLowerInvariant(), clideAssembly); // Register hosting package assembly. var servicesAssembly = services.GetType().Assembly; addedAssemblies[servicesAssembly.Location.ToLowerInvariant()] = servicesAssembly; var installPath = GetInstallPath(services); var packageManifestFile = Path.Combine(installPath, "extension.vsixmanifest"); if (File.Exists(packageManifestFile)) { tracer.Info(Strings.DevEnvFactory.ExtensionManifestFound(packageManifestFile)); var manifestDoc = XDocument.Load(packageManifestFile); ThrowIfClideIsMefComponent(manifestDoc); // NOTE: we don't warn anymore in this case, since a single package may have // a mix of plain VS exports as well as clide components. // Since we use CommonComposition, only the types with the ComponentAttribute // will be made available in the Clide container, not the others, and no // duplicates would be registered. //WarnIfClideComponentIsAlsoMefComponent(packageManifestFile, manifestDoc); foreach (string clideComponent in GetClideComponents(manifestDoc)) { var assemblyFile = Path.Combine(installPath, clideComponent); tracer.Info(Strings.DevEnvFactory.ClideComponentDeclared(clideComponent, assemblyFile)); if (clideComponent == ClideAssembly) { tracer.Warn(Strings.DevEnvFactory.ClideNotNecessaryAsComponent(clideComponent)); continue; } if (!File.Exists(assemblyFile)) throw new InvalidOperationException(Strings.DevEnvFactory.ClideComponentNotFound(packageManifestFile, clideComponent, assemblyFile)); var componentAssembly = Assembly.LoadFrom(assemblyFile); if (!addedAssemblies.ContainsKey(componentAssembly.Location.ToLowerInvariant())) addedAssemblies.Add(componentAssembly.Location.ToLowerInvariant(), componentAssembly); } } else { tracer.Info(Strings.DevEnvFactory.ExtensionManifestNotFound(packageManifestFile)); } var catalog = new ComponentCatalog(addedAssemblies.Values.ToArray()); var container = new CompositionContainer(catalog, composition.DefaultExportProvider, new ServicesExportProvider(services)); // Make the service locator itself available as an export. var serviceLocator = new ServicesAccessor(services, new Lazy<IServiceLocator>(() => serviceLocators[services])); container.ComposeParts(serviceLocator); return new ExportsServiceLocator(container); } }
public ActionResult Delete(int id) { ServicesAccessor.DeleteService(id); return(RedirectToAction("Index", "SiteAdmin", new { area = "" })); }