WCF - 如何在初始化服务主机时传递服务 class 构造函数参数 (TypeOf(Service-Class))

WCF - How to pass service class constructor parameter when initializing Service Host(TypeOf(Service-Class))

我有 WCF 服务 class 'MusicServiceManager',其构造函数采用 IAppAmbientState 参数。我需要在初始化服务主机时将参数值传递给 MusicServiceManager 构造函数。我不想要 IAppAmbientState 的单例实现。

服务合同

[ServiceContract]
public interface IAlbumService
{
    [OperationContract]
    string TestMessage();

    [OperationContract]
    IEnumerable<AlbumData> GetAlbums();

    [OperationContract(Name = "GetAlbumByID")]
    AlbumData GetAlbum(Guid ID);

    [OperationContract(Name = "GetAlbumByName")]
    AlbumData GetAlbum(string name);
}

AppAmbientState

public class AppAmbientState : IAppAmbientState
{
    public IContainer ServiceContainer { get; }

    public AppAmbientState(
        IContainer container  // autofac DIC container
        )
    {
        ServiceContainer = container;
    }
}

音乐服务管理器

public class MusicServicesManager : IAlbumService
{
    private readonly IAppAmbientState _appAmbientState;
   
    public MusicServicesManager(IAppAmbientState appAmbientState)
    {
        this._appAmbientState= appAmbientState;         
    }

    public AlbumData GetAlbum(Guid ID)
    {
        throw new NotImplementedException();
    }

服务主机配置Class

public class ServiceHostConfiguration : IServiceHostConfiguration
{
    private readonly ServiceHost hostMusicServiceManager;

    public ServiceHostConfiguration()
    {
        var container = ContainerConfiguration.Configure();

        AppAmbientState AppAmbientStateInstance = new AppAmbientState(container);
        // need help here... MusicServiceManager(AppAmbientStateInstance)???? 
        // ideally I don't want to define my base address in class

        this.hostMusicServiceManager = new ServiceHost(typeof(MusicServicesManager));
    }

    public void InitializeService()
    {
        try
        {
            this.hostMusicServiceManager.Open();
            Console.WriteLine("App Web Services Started. Press [Enter] To Exit  ");
        }
        catch(Exception exp)
        {
            Console.WriteLine("App Web Services Could Not Start::  ", exp);
        }
        
    }

    public void CloseService()
    {
        this.hostMusicServiceManager.Close();
    }
}

Autofac DI 容器

public static class ContainerConfiguration
{
    public static IContainer Configure()
    {
        var builder = new ContainerBuilder();
        
        //Application Configuration
        builder.RegisterType<Application>().As<IApplication>();
        builder.RegisterType<ServiceHostConfiguration>().As<IServiceHostConfiguration>();
        builder.RegisterType<AppAmbientState>().As<IAppAmbientState>();
        builder.RegisterType<MusicServicesManager>().As<IAlbumService>();

这是我的演示:

这是项目目录:

Program.cs:

using Autofac;
using Autofac.Integration.Wcf;
using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace AutofacTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterType<Logger>().As<ILogger>();
            builder.RegisterType<Service>();

            using (IContainer container = builder.Build())
            {
                
                ServiceHost host = new ServiceHost(typeof(Service));
                host.AddDependencyInjectionBehavior<Service>(container);
                host.Open();
                Console.WriteLine("The host has been opened.");
                Console.ReadLine();
                host.Close();
              
            }
        }
    }
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string Test();
    }

    public interface ILogger
    {
    }

    public class Service : IService
    {
        private readonly ILogger logger;

        
        public Service(ILogger logger)
        {
            this.logger = logger;
        }

        public string Test()
        {
            return "TEst1";
        }
    }

    public class Logger : ILogger
    {

    }
}

App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
    <system.serviceModel>
        <services>
            <service name="AutofacTest.Service">
                <endpoint address="" binding="basicHttpBinding" contract="AutofacTest.IService" name="TestServiceEndPoint" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" name="TestServiceMexEndPoint" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost/TestService" />
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior>
                    <serviceDebug includeExceptionDetailInFaults="true" />
                    <serviceMetadata httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.0.0" newVersion="5.2.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

如果问题仍然存在,请随时告诉我。