用autofac注入wcf通道

时间:2014-10-20 14:27:14

标签: c# wcf dependency-injection autofac

我想在WCF服务(使用Autofac.Integration.Web)中使用autofac来解析对wcf通道的依赖。我正在尝试的是这样的事情

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();
}

使用频道的课程:

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

但我得到了

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

当我尝试使用WCF测试客户端时。

但是,如果我刚刚在方法中新增了频道,则客户端不会抱怨:

所以我的理解是我在绑定中做错了但不确定是什么。

public AuthorizationManager()
{

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

所以,如果我错了,请纠正我,问题应该在绑定中,但我不知道我做错了什么。

由于

更新

ResourceServiceAuthorizationManager以nuget包

的形式出现
public class ResourceServiceAuthorizationManager : ServiceAuthorizationManager
{
    public ResourceServiceAuthorizationManager();

    public IAuthenticationService AuthenticationServiceClient { get; set; }

    public Guid ResourceId { get; set; }

    public override bool CheckAccess(OperationContext operationContext);
}

1 个答案:

答案 0 :(得分:0)

我不确定您是否以正确的方式使用DI来使用您的WCF服务,但初始化可能是

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