1

我正在创建一个需要生成 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:\Projects\MiB\MiB\Mib\Global.asax.cs:line 25

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

pptPresentation.Slides[0]

也会失败。

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

4

1 回答 1

1

所以事实证明这很简单。

项目中没有您自己的名为“Slide”的类。一旦改名,冲突就消失了。

于 2013-10-28T11:36:53.177 回答