0

下面的应用程序在我们的开发/QA 团队机器上成功运行:

using Autofac;
using Word = Microsoft.Office.Interop.Word;

namespace ComObjectIssue
{
    public interface IDateWriter
    {
        void WriteDate();
    }

    public class TodayWriter : IDateWriter
    {
        private readonly Word.Application wordApp;
        public TodayWriter(Word.Application wordApp)
        {
            this.wordApp = wordApp;
        }

        public void WriteDate()
        {
            //...
        }
    }

    public class Program
    {
        private static IContainer Container { get; set; }

        static void Main(string[] args)
        {
            var builder = new ContainerBuilder();
            var wordApp = new Word.Application();
            builder.RegisterInstance(wordApp).SingleInstance();
            builder.RegisterType<TodayWriter>().As<IDateWriter>();
            
            Container = builder.Build(); // throws "System.ArgumentException: The type 'System.__ComObject' is not assignable to service 'Microsoft.Office.Interop.Word.Application'."

            using (var scope = Container.BeginLifetimeScope())
            {
                var writer = scope.Resolve<IDateWriter>();
                writer.WriteDate();
            }

            wordApp.Quit();
        }
    }
}

但不适用于我们的一位客户——Autofac 在创建容器时抛出以下异常:

System.ArgumentException: The type 'System.__ComObject' is not assignable to service 'Microsoft.Office.Interop.Word.Application'.
   at Autofac.Builder.RegistrationBuilder.CreateRegistration(Guid id, RegistrationData data, IInstanceActivator activator, IResolvePipelineBuilder pipelineBuilder, Service[] services, IComponentRegistration target)
   at Autofac.Builder.RegistrationBuilder.CreateRegistration[TLimit,TActivatorData,TSingleRegistrationStyle](IRegistrationBuilder`3 builder)
   at Autofac.Builder.RegistrationBuilder.RegisterSingleComponent[TLimit,TActivatorData,TSingleRegistrationStyle](IComponentRegistryBuilder cr, IRegistrationBuilder`3 builder)
   at Autofac.ContainerBuilder.Build(IComponentRegistryBuilder componentRegistry, Boolean excludeDefaultModules)
   at Autofac.ContainerBuilder.Build(ContainerBuildOptions options)

重新安装Microsoft Visual Studio 2010 Tools for Office Runtime没有帮助。


  • Autofac版本:6.2.0.0
  • 目标:.NET Framework 4.8

是否知道会发生什么或应该检查什么?


更新

MS Word 版本:16.0

今天我们能够通过以管理员身份运行 MS Word 来解决这个问题。不幸的是,这种解决方案不适用,因为客户端的安全策略不允许使用管理员权限。

我无法在我这边复制它 - 它适用于非管理员帐户。看起来需要一些特定的许可,但是什么

4

0 回答 0