Search code examples
c#proxyappdomainautofacrealproxy

Autofac and Cross-AppDomain Proxies


I am trying to register a cross-AppDomain proxy with Autofac.

The concrete type represented by the proxy implements the interface IServiceHost. Thus, the proxy should also, in order to allow calls to the proxy using that interface to work.

I try to register the proxy with Autofac, by doing:

void Initialize(IServiceHost host)
{
    Host = host;

    var builder = new ContainerBuilder();
    builder.RegisterInstance(host)
        .As<IServiceHost>()
        .SingleInstance();

    Scope = builder.Build();
}

However, on the builder.Build() call, I get:

ArgumentException

The type 'System.MarshalByRefObject' is not assignable to service 'Treadmarks.Core.ServiceBase.IServiceHost'.

However, host is definitely an IServiceHost, since it isn't null and it comes from a strongly typed method argument.

Can anyone explain how I can register the proxy properly?


Solution

  • You should be able to use a lambda with the .Register() method, rather than .RegisterInstance(), to work around this.