代码优先 MVC 应用程序在访问幻灯片时与 PowerPoint 错误交互



我正在创建一个应用程序,该应用程序需要生成ppt幻灯片的缩略图和图形表示。其代码看起来并不棘手:

pptPresentation = pptApplication.Presentations.Open(ppt, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
var filename = DateTime.Now.Ticks.ToString() + ".png";
var thumbPath = Path.Combine(root, "thumbs") + filename;
foreach(Microsoft.Office.Interop.PowerPoint.Slide slide in pptPresentation.Slides)
{
    slide.Export(thumbPath, "png", 160, 120);
}

问题在于,在项目中使用此代码(它甚至无法运行)时,此代码在应用程序启动时失败。我在失败的代码行旁边放了一个箭头。

protected void Application_Start()
    {
        Database.SetInitializer(new DatabaseSeeder());
        MibContext ctx = new MibContext();
  -->   ctx.Database.Initialize(true);
        if (!WebSecurity.Initialized)
            WebSecurity.InitializeDatabaseConnection("MibContext", "UserProfile", "UserId", "UserName", autoCreateTables: true);
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    }

错误是可怕的:对象引用未设置为对象的实例。

at System.Data.Metadata.Edm.ObjectItemConventionAssemblyLoader.TryCreateStructuralType(Type type, StructuralType cspaceType, EdmType& newOSpaceType)
at System.Data.Metadata.Edm.ObjectItemConventionAssemblyLoader.TryCreateType(Type type, EdmType cspaceType, EdmType& newOSpaceType)
at System.Data.Metadata.Edm.ObjectItemConventionAssemblyLoader.LoadTypesFromAssembly()
at System.Data.Metadata.Edm.ObjectItemAssemblyLoader.Load()
at System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, ObjectItemLoadingSessionData loadingData)
at System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, KnownAssembliesSet knownAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage, Object& loaderCookie, Dictionary`2& typesInLoading, List`1& errors)
at System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(ObjectItemCollection objectItemCollection, Assembly assembly, Boolean loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage)
at System.Data.Metadata.Edm.MetadataWorkspace.LoadFromAssembly(Assembly assembly, Action`1 logLoadMessage)
at System.Data.Metadata.Edm.MetadataWorkspace.LoadFromAssembly(Assembly assembly)
at System.Data.Entity.Infrastructure.DbCompiledModel.CreateObjectContext[TContext](DbConnection existingConnection)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.LazyInternalContext.MarkDatabaseInitialized()
at System.Data.Entity.Database.Initialize(Boolean force)
at Mib.MvcApplication.Application_Start() in c:ProjectsMiBMiBMibGlobal.asax.cs:line 25

如果我注释掉 foreach 部分(它是导致问题的 foreach 行),那么应用程序将按预期运行。

pptPresentation.Slides[0]

也会失败。

奇怪的是,它没有达到这个,因为失败发生在AppStart部分。我不知道这些事情之间的联系是什么,甚至不知道从哪里开始解决这个问题。

所以结果很简单。

项目中没有自己的名为"幻灯片"的类。一旦重新命名,冲突就消失了。

最新更新