将参数传递给WCF的ServiceHost类型与Ninject 2 [英] Passing parameters to a WCF ServiceHost type with Ninject 2
问题描述
我想用一个Ninject.Wcf扩展来创建一个参数化的服务主机实例。
I want to use a Ninject.Wcf extension to create a parametrized service host instance.
例如我有一个类 MyWCFHandler
,唯一的下面的构造:
For example I have a class MyWCFHandler
with the only following constructor:
public MyWCFHandler(UserManager manager)
{
_manager = manager;
}
但是,当我写 VAR myServiceHost =新的ServiceHost( typeof运算(MyWCFHandler));
我没有办法来传递的依赖对象的构造
But when I write var myServiceHost = new ServiceHost(typeof(MyWCFHandler));
I have no way to pass the dependency object to a constructor.
我不想惹。定制的ServiceHost像
的我如何通过值来构造我的WCF服务?
I don't want to mess with the custom ServiceHost like offered in How do I pass values to the constructor on my wcf service?
我决定去与Ninject方式,但不能完全了解如何法案我的处境。
I decided to go with the Ninject way, but not able to fully understand how to act in my situation.
下面是我理解WCF扩展在Ninject作品:
Here is what I understand WCF Extension in Ninject works:
- 参考Ninject和Ninject.Extensions.WCF在我的项目
-
创建一个继承Ninject模块类,并写类似:
- Reference Ninject and Ninject.Extensions.WCF in my project.
Create a class that inherits Ninject module and write something like:
内部类ServiceModule:NinjectModule
{
公共覆盖无效的load()
{
绑定< IUserManager>()为<的UserManager>()。 WithConstructorParameters(myUserManagerIwantToUseInWCFHandler);
}
}
添加)与新ServiceModule(初始化内核到KernelContainer。
Add a Kernel initialized with new ServiceModule() to a KernelContainer.
使用的NinjectServiceHost是这样的:
Use the NinjectServiceHost like this:
VAR的服务= KernelContainer.Kernel。获取< IMyWCFHandler>();
_host =新NinjectServiceHost(服务);
和有我应该有我的主人准备好打开。
And there I should have my host ready to be opened.
的问题是:
我应该如何通过我的构造函数的参数为一个NinjectModule?当我准备一个参数绑定到它应创建一个NinjectModule权的实例?我如何通过他们获取的方法?
How should I pass my constructor parameters into a NinjectModule? Should I create an instance of a NinjectModule right when I am ready to bind a parameter to it? How do I pass them to Get method?
不幸的是,没有一个例子身边简单的表演参数化ServiceHost的开始。我甚至不关心,如果它是Ninject我使用。无论解决方案有一个很好的例子 - 我与它的罚款,因为我只是决定什么容器来使用。
Unfortunately there is not one example around to simple show the parametrized ServiceHost start. I don't even care if it is Ninject I use. Whichever solution has a good example - I am fine with it, since I am just deciding what IoC container to use.
推荐答案
关于ninject。答案是这取决于你是否要单独服务还是每个请求一个新的实例。随着单服务,您可以执行以下操作:
Regarding ninject. The answer is it depends whether you want a singleton service or a new instance per request. With a singleton service you can do the following:
public class TimeServiceModule : NinjectModule
{
/// <summary>
/// Loads the module into the kernel.
/// </summary>
public override void Load()
{
this.Bind<ITimeService>().To<TimeService>();
this.Bind<ServiceHost>().ToMethod(ctx => ctx.Kernel.Get<NinjectServiceHost>(new ConstructorArgument("singletonInstance", c => c.Kernel.Get<ITimeService>())));
}
}
internal static class Program
{
private static void Main()
{
var kernel = new StandardKernel(new TimeServiceModule());
var serviceHost = kernel.Get<ServiceHost>();
serviceHost.AddServiceEndpoint(typeof(ITimeService), new NetTcpBinding(), "net.tcp://localhost/TimeService");
try
{
serviceHost.Open();
}
finally
{
serviceHost.Close();
}
}
}
每请求的方式:
Per request approach:
public interface IServiceTypeProvider
{
/// <summary>
/// Gets the service types.
/// </summary>
/// <value>The service types.</value>
IEnumerable<Type> Types { get; }
}
Func<Type, ServiceHost> serviceHostFactory
foreach (Type serviceType in this.ServiceTypeProvider.Types)
{
// I do some magic here to query base contracts because all our service implement a marker interface. But you don't need this. But then you might need to extend the type provider interface.
IEnumerable<Type> contracts = QueryBaseContracts(serviceType );
var host = this.CreateHost(serviceType);
foreach (Type contract in contracts)
{
Binding binding = this.CreateBinding();
string address = this.CreateEndpointAddress(contract);
this.AddServiceEndpoint(host, contract, binding, address);
}
host.Description.Behaviors.Add(new ServiceFacadeBehavior());
this.OpenHost(host);
this.serviceHosts.Add(host);
}
protected virtual ServiceHost CreateHost(Type serviceType )
{
return this.serviceHostFactory(serviceType );
}
public class YourWcfModule : NinjectModule
{
/// <summary>
/// Loads the module into the kernel.
/// </summary>
public override void Load()
{
this.Bind<Func<Type, ServiceHost>>().ToMethod(
ctx =>
(serviceType) =>
ctx.Kernel.Get<NinjectServiceHost>(new ConstructorArgument("serviceType", serviceType), new ConstructorArgument("baseAddresses", new Uri[] { })));
}
}
有乐趣
Have fun
这篇关于将参数传递给WCF的ServiceHost类型与Ninject 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!