简体   繁体   English

用autofac注入wcf通道

[英]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. 我想在WCF服务(使用Autofac.Integration.Web)中使用autofac来解决对wcf通道的依赖性。 What I'm trying is something like this 我正在尝试的是这样的事情

Global.asax 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. 当我尝试使用WCF测试客户端时。

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 ResourceServiceAuthorizationManager以nuget包的形式出现

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 我不确定您是否以正确的方式使用DI来使用WCF服务,但是初始化可能是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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