将参数传递给WCF的ServiceHost类型与Ninject 2

编程入门 行业动态 更新时间:2024-10-11 13:25:22
本文介绍了将参数传递给WCF的ServiceHost类型与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

    本文发布于:2023-11-13 04:44:48,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1583467.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:参数   类型   ServiceHost   WCF   Ninject

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!