4

IIS でホストされているサービスがあります。Web.config によって構成されます。

検証メソッドにロジックがある場合に機能するカスタム UserNamePassValidator を作成しました。しかし、別のプロジェクトにロジックが必要であり、以下のように DI を使用して注入されます。

public class UserNamePassValidator : System.IdentityModel.Selectors.UserNamePasswordValidator
{
    private readonly ISystemAuthentication _systemAuthentication;

    public UserNamePassValidator(ISystemAuthentication systemAuthentication)
    {
        _systemAuthentication = systemAuthentication;
    }

    public override void Validate(string userName, string password)
    {
        _systemAuthentication.Validate(userName, password))
    }
}

Autofac WCF 統合を使用しています。

var builder = new ContainerBuilder();
builder.RegisterType<AuthenticationService>().As<IAuthenticationService>();
builder.Register(c => new SystemAuthentication()).As<ISystemAuthentication>();
builder.Register(c => new UserNamePassValidator(c.Resolve<ISystemAuthentication>()));
AutofacHostFactory.Container = builder.Build();

サービスを参照すると、次のエラーが表示されます。

[MissingMethodException: No parameterless constructor defined for this object.]

web.config の動作;

 <userNameAuthentication
                 userNamePasswordValidationMode="Custom"
                 customUserNamePasswordValidatorType="MyNamespace.UserNamePassValidator, service" />

次の関連記事を読みましたが、例は自己ホスト型サービスです: How to inject an object into a WCF validator class

編集

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="Namespace.AuthenticationServiceBehaviour" name="Namespace.AuthenticationService" >
        <endpoint address="" binding="wsHttpBinding" contract="Namespace.IAuthenticationService" bindingConfiguration="SafeServiceConf">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Namespace.AuthenticationServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceCredentials>
            <serviceCertificate findValue="AuthenticationService" 
              storeLocation="LocalMachine"
              storeName="My" 
              x509FindType="FindBySubjectName" />
            <userNameAuthentication
              userNamePasswordValidationMode="Custom"
              customUserNamePasswordValidatorType="Namespace.UserNamePassValidator, Service" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding name="SafeServiceConf" maxReceivedMessageSize="65536">
          <readerQuotas maxStringContentLength="65536" maxArrayLength="65536" maxBytesPerRead="65536" />
          <security mode="Message">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

SystemAuthentication クラス

public class SystemAuthentication : ISystemAuthentication
{
   public bool Validate(string userName, string password)
   {
       // removed code for abbreviation
       return true;
   }

WCF 認証サービス

public class AuthenticationService : IAuthenticationService
    { 
        public bool Authenticate(string email, string password)
        {
            // removed for abbreviation
            return true;
        }
    }
4

1 に答える 1

10

この投稿のヘルプからUserNamePasswordValidator: DI とフレームワークが衝突するとき

私が削除したXML構成から:

<userNameAuthentication
   userNamePasswordValidationMode="Custom"
   customUserNamePasswordValidatorType="MyNamespace.UserNamePassValidator, service" />

AutoFacHostFactory サービス ホストに動作を追加しました

   IContainer container = builder.Build();
    AutofacHostFactory.Container = container;
    AutofacHostFactory.HostConfigurationAction = host =>
    {
        var auth = host.Credentials.UserNameAuthentication;
        auth.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
        auth.CustomUserNamePasswordValidator = container.Resolve<UserNamePassValidator>();
    };

これは完全に機能しますが、web.config から実行できればもっとよかったでしょう。誰かがより良い方法を知っている場合は、投稿してください:)

于 2012-09-25T09:03:16.967 に答える