4

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?

David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
  • So `host` does not implement `IServiceHost`? In that case it is not a Autofac problem. – Steven Mar 06 '12 at 14:04
  • @Steven I specifically said that the proxied type *does* implement `IServiceHost`, and so thus the proxy must implement it also. – David Pfeffer Mar 06 '12 at 14:12
  • Autofac checks whether `host` is a `IServiceHost` and because it is not, Autofac throws an exception. – Steven Mar 06 '12 at 14:20
  • @Steven It definitely is an `IServiceHost` -- the variable is a method argument and is passed in as `IServiceHost host`. – David Pfeffer Mar 06 '12 at 15:59
  • @Steven I edited the question; does this convince you that it is definitely an IServiceHost and the issue is in Autofac? – David Pfeffer Mar 06 '12 at 16:03
  • 1
    Note, 'SingleInstance' doesn't make sense with 'RegisterInstance'. After all, there will always only be that one instance. Don't think it has anything to do with your error though. – Peter Lillevold Mar 06 '12 at 19:05

1 Answers1

2

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

Roman
  • 19,581
  • 6
  • 68
  • 84
Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101