【问题标题】:WCF dependency injection without SVC file没有 SVC 文件的 WCF 依赖注入
【发布时间】:2016-06-03 22:17:49
【问题描述】:

我有一个通过 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();

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

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

如何在代码中做到这一点?

【问题讨论】:

  • @Evk 你帮了我很多!那行得通。如果您将评论添加为答案,我会接受。

标签: c# wcf inversion-of-control


【解决方案1】:

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

向您的主机添加行为
host.Description.Behaviors.Add(new DependencyInjectionServiceBehavior()); 

【讨论】:

    猜你喜欢
    • 2011-03-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 2011-09-11
    • 1970-01-01
    相关资源
    最近更新 更多