没有 SVC 文件的 WCF 依赖项注入



>我有一个通过net.pipe公开的WCF服务。此服务的托管方式如下:

var hosturi = new Uri("net.pipe://localhost/somewhere");
var host = new ServiceHost(typeof(Service), hosturi);
var serviceBinding = new NetNamedPipeBinding
{
    MaxReceivedMessageSize = int.MaxValue, // and maybe something else
};
serviceBinding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas
{
    MaxArrayLength = int.MaxValue, // and maybe something else
};
host.AddServiceEndpoint(typeof(T), serviceBinding, string.Empty);
host.Description.Behaviors.Add(
    new ServiceThrottlingBehavior
    {
        MaxConcurrentCalls = 5000,
        MaxConcurrentInstances = 5000,
        MaxConcurrentSessions = 5000
    }
);
host.Open();

我想为我的服务实例指定 DI 工厂,以获取注入的任何构造函数依赖项。

虽然有很多关于这个主题的文章(例如:https://msdn.microsoft.com/en-us/library/hh323725(v=vs.100).aspx),但绝对所有这些都建议使用 .svc 文件指定工厂。

如何在代码中执行此操作?

你自己创建服务主机,所以你不需要ServiceHostFactory(这是一个创建ServceHost的工厂)。因此,只需跳过本文中的步骤 4-6,并使用

host.Description.Behaviors.Add(new DependencyInjectionServiceBehavior()); 

最新更新