简体   繁体   中英

Injecting wcf channel with autofac

I would like to use autofac inside a WCF service (using Autofac.Integration.Web) to resolve a dependency on a wcf channel. What I'm trying is something like this

Global.asax

protected void Application_Start(object sender, EventArgs e)
{
    var builder = new ContainerBuilder();
    builder.RegisterType<CheckingService>();
    builder.Register(c => new ChannelFactory<IAuthenticationService>(
                        new WSHttpBinding("wsHttpBinding_Common"),
                            new EndpointAddress(ConfigurationManager.AppSettings["AuthenticationServiceUrl"])).CreateChannel())
                        .SingleInstance();
    AutofacHostFactory.Container = builder.Build();
}

Class using the channel:

public AuthorizationManager(IAuthenticationService authenticationServiceClient)
{
    AuthenticationServiceClient = authenticationServiceClient;
}

But I'm getting

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

When I'm trying to use WCF test client.

However if I just new the channel in the method the client doesn't complain:

so my understanding is that I'm doing something wrong in the binding but not sure what.

public AuthorizationManager()
{

    AuthenticationServiceClient = new ChannelFactory<IAuthenticationService>(
        new WSHttpBinding("wsHttpBinding_Common"),
        new EndpointAddress(ConfigurationManager.AppSettings["AuthenticationServiceUrl"])).CreateChannel();
}

So, correct me if I'm wrong, the problem should be in the binding but I have no idea what I'm doing wrong.

Thanks

Update

the ResourceServiceAuthorizationManager comes in the form of a nuget package

public class ResourceServiceAuthorizationManager : ServiceAuthorizationManager
{
    public ResourceServiceAuthorizationManager();

    public IAuthenticationService AuthenticationServiceClient { get; set; }

    public Guid ResourceId { get; set; }

    public override bool CheckAccess(OperationContext operationContext);
}

I'm not sure you are using the DI in the right way to use your WCF Service but the initialization could be

builder.Register(c => new ChannelFactory<IAuthenticationService>(
    new WSHttpBinding("wsHttpBinding_Common"),
    new EndpointAddress(ConfigurationManager.AppSettings["AuthenticationServiceUrl"])) 
         .CreateChannel()).As<IAuthenticationService>().SingleInstance();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM