Autofac throws exception for Microsoft.Office.Interop.Word.Application

AdamShakhabov 1 Reputation point
2021-11-11T18:41:46.213+00:00

The following code in VSTO Add-in for Word works on my machine when I run it as Administrator or User:

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

namespace ComObjectIssue
{
    //...
    var builder = new ContainerBuilder();
    var wordApp = Globals.ThisAddIn.Application;
    builder.RegisterInstance(Globals.ThisAddIn.Application).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'." when customer runs it as User

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

    //...

    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()
        {
            //...
        }
    }
}

But for one of our customers, it works only if he runs it as Administrator — if MS Word is running as User, Autofac's container build method throws an exception:

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](IRegistrationBuilder3 builder) at Autofac.Builder.RegistrationBuilder.RegisterSingleComponent[TLimit,TActivatorData,TSingleRegistrationStyle](IComponentRegistryBuilder cr, IRegistrationBuilder3 builder)
at Autofac.ContainerBuilder.Build(IComponentRegistryBuilder componentRegistry, Boolean excludeDefaultModules)
at Autofac.ContainerBuilder.Build(ContainerBuildOptions options)

As I mentioned above, on my computer it works for both Administrator and User. What kind of security policy on customer machines can be the cause of this issue?


I posted a similar question on StackOverflow — Autofac cannot resolve Microsoft.Office.Interop.Word.Application, made some experiments with GAC and Microsoft Visual Studio 2010 Tools for Office Runtime. It did not help.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,295 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,262 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.