我正在创建一个需要生成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部分。我不知道这些事情之间的联系是什么,甚至在哪里开始解决这个问题。
答案 0 :(得分:1)
事实证明这很简单。
项目中没有自己的名为“幻灯片”的类。一旦重命名,冲突便消失了。