c# - How to register two WCF service contracts with autofac -
i have wcf service implements 2 service contracts...
public class myservice : iservice1, iservice2
and self-hosting service...
host = new servicehost(typeof(myservice));
everything working fine when service implemented 1 service contract, when attempt set autofac register both this:
host.adddependencyinjectionbehavior<iservice1>(_container); host.adddependencyinjectionbehavior<iservice2>(_container);
... throws exception on second one, reporting:
the value not added collection, collection contains item of same type: 'autofac.integration.wcf.autofacdependencyinjectionservicebehavior'. collection supports 1 instance of each type.
at first glance thought saying 2 contracts somehow being seen same type on second reading believe saying autofacdependencyinjectionservicebehavior type in question, i.e. cannot use twice!
and yet, found this post explicitly showed using multiple times in different form:
foreach (var endpoint in host.description.endpoints) { var contract = endpoint.contract; type t = contract.contracttype; host.adddependencyinjectionbehavior(t, container); }
unfortunately, gave same error message.
is possible register more 1 service contract on 1 service and, if so, how?
in fact can register multiple endpoint single host autofac.
that true cannot add multiple autofacdependencyinjectionservicebehavior
behaviour iterates through endpoints , registers them in applydispatchbehavior
method: source
in order make work need register service asself()
builder.registertype<myservice>();
then can configure endpoint normally:
host = new servicehost(typeof(myservice)); host.addserviceendpoint(typeof(iservice1), binding, string.empty); host.addserviceendpoint(typeof(iservice2), binding, string.empty);
and need call adddependencyinjectionbehavior
sevicehost type itself:
host.adddependencyinjectionbehavior<myservice>(container);
here small sample project (based on documentation) demonstrates behavior.
Comments
Post a Comment