簡體   English   中英

用autofac注入wcf通道

[英]Injecting wcf channel with 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);
}

我不確定您是否以正確的方式使用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