dependency injection - Using Autofac for DI into WCF service hosted in ASP.NET application -
I'm having trouble rendering service dependencies to my WCF service using Autofac 1.4.5. I have read and followed, but my debugging shows me that my WCF service is created by I have set the My WCF service class Now I have used wiring in System.ServiceModel.Dispatcher.InstanceBehavior.GetInstance ()
method and not by AutofacWebServiceHostFactory . What am i doing wrong
ajax.svc
file to be used as an example to use with WebHttpBinding
: & lt;% @ ServiceHost language = "c #" debug = "true" service = "generic.frontend.Web.Ajax, Generic.Frontend.Web" factory = "Autofac.Integration.Wcf .AutofacWebServiceHostFactory, Autofac.Integration.Wcf "%>
Ajax
is defined as:
name space Generic.Frontend.Web {[ServiceContract ] [AspNetCompatibilityRequirements (RequirementsMod = ASPNnetSecurity Plan Mode.Allowed)] Public Square Ajax {Public MapWebService MapWebService {get; Set;} Public Ajax () {// This constructor is called PublicWorld (MapWebService mapWebService)) {// This constructor should be called MapWebService = mapWebService; } [WebGet (ResponseFormat = WebMessageFormat.Json)] [OperationContract (name = "mapchange")] Public MapChangeResult ProcessMapChange (string args) {// Injection service here results = MapWebService.ProcessMapChange (args); Return result; }}}
Global.asax.cs
as shown in the wiki given above: < Code> var Builder = New Containerbuilder (); the creator. Registrar module (new AutofacModuleWebservice ()); Var Container = Builder Build (); AutoFax Service Host Feature Container = Container;
with
class AutofacModuleWebservice: module {protected override zero load (ContainerBuilder Builder) {builder.Register & lt; Ajax & gt; (); Builder.Register & lt; MapWebService & gt; () ContainerScoped () .; }}
In my web.config I have
& lt; Services & gt; & Lt; Service name = "generic.frant.web.jax" & gt; & Lt; Endpoint address = "http://mysite.com/ajax.svc/" binding = "webHTPbinding" contract = "generic.frontend.web.jax" /> & Lt; / Services & gt; & Lt; / Services & gt;
.
This service is already working fine but I can not get autofac bits (reading: construction / injection) to work. Any ideas
edit: Removing default creators unfortunately leads to the following exception:
System.InvalidOperationException: provided Could not be loaded as a service that could not be loaded as a service because there is no default (parameter-less) constructor in it. To fix the problem, add a default constructor for the type or an example of the type of host Pass to .
Cheers, Oliver
Is your service setup with InstanceContextMode there is one? If so, wcf will serve your service using the default constructor. To bring this change around your instance context mode and let AutoFac manage the life of your service.
Comments
Post a Comment