我有一个WCF网络服务,我想利用我想要依赖的我的存放和服务,向我的WCF网络服务注入,然而,Ninject WCF推广范例相当多,它有一位导师,正在对每个依赖性进行即时调查,我不想这样做,我想说的是纯粹依赖性注射。
谷歌在利用Ninject和WCF方面取得了任何成功,似乎对我所研究的专题来说,没有什么相关结果。
我有一个WCF网络服务,我想利用我想要依赖的我的存放和服务,向我的WCF网络服务注入,然而,Ninject WCF推广范例相当多,它有一位导师,正在对每个依赖性进行即时调查,我不想这样做,我想说的是纯粹依赖性注射。
谷歌在利用Ninject和WCF方面取得了任何成功,似乎对我所研究的专题来说,没有什么相关结果。
<%@ ServiceHost Language="C#" Debug="true" Service="WcfTimeService.TimeService" CodeBehind="TimeService.svc.cs" **Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory"** %>
我目前使用Ninject(v3)和我的WCF的方法是以Ninject WCF延伸和。
简言之,这里是我做的事情:
1) Via NuGet, I ve supplemented a reference to Ninject. 延期。 WCF项目。 由此产生了NinjectWebCommon.cs的“Start”双倍,后者负责启动Ninject。
2) 典型的情况是,你在NinjectWebCommon.cs的创建Kernel方法中建立了你的Ninject绘图。 然而,由于我有一个MVC3站点,处于同一解决方案之中,并且希望该场址有相同的Ninject绘图,我的创建者认为:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
InfrastructureSetup.RegisterServices(kernel);
return kernel;
}
3) 基础设施。 登记册服务,我有我的Ninject绘图:
public static class InfrastructureSetup
{
public static void RegisterServices(IKernel kernel)
{
kernel.Bind<IRepositoryContext>().To<MyEntityFrameworkContext>().InRequestScope();
kernel.Bind<IFooRepository>().To<FooRepository>().InRequestScope();
kernel.Bind<IBarRepository>().To<BarRepository>().InRequestScope();
// ... and so on. I want InRequestScope() for the EF context, since
// otherwise my repositories (which take IRepositoryContext in their
// constructors) end up getting different EF contexts, messing things up
}
}
4) 我也要向我的工联建筑商注入 st(IFooservice等),因此,我编辑了WCF项目的会议,听取Pieter De Rycke的建议:
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<!-- Add the Ninject behavior to the WCF service. This is needed to support dependency injection to the WCF constructors -->
<ninject />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<!-- Add the Ninject behavior extension -->
<add name="ninject"
type="MyWCFProject.Infrastructure.NinjectBehaviorExtensionElement, MyWCFProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
5) 在MyWCF项目中。 基础设施名称空间,我有3个基本上由Pieter复制的档案:
NinjectBehaviorAttribute.cs:
using System;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using Ninject.Web.Common;
namespace MyWCFProject.Infrastructure
{
public class NinjectBehaviorAttribute : Attribute, IServiceBehavior
{
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
Type serviceType = serviceDescription.ServiceType;
// Set up Ninject to support injecting to WCF constructors
var kernel = new Bootstrapper().Kernel;
IInstanceProvider instanceProvider = new NinjectInstanceProvider(kernel, serviceType);
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher endpointDispatcher in dispatcher.Endpoints)
{
DispatchRuntime dispatchRuntime = endpointDispatcher.DispatchRuntime;
dispatchRuntime.InstanceProvider = instanceProvider;
}
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
}
NinjectBehavior ExtensionElement.cs:
using System;
using System.ServiceModel.Configuration;
namespace MyWCFProject.Infrastructure
{
public class NinjectBehaviorExtensionElement : BehaviorExtensionElement
{
public override Type BehaviorType
{
get { return typeof(NinjectBehaviorAttribute); }
}
protected override object CreateBehavior()
{
return new NinjectBehaviorAttribute();
}
}
}
NinjectInstanceProvider.cs:
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using Ninject;
namespace MyWCFProject.Infrastructure
{
public class NinjectInstanceProvider : IInstanceProvider
{
private Type serviceType;
private IKernel kernel;
public NinjectInstanceProvider(IKernel kernel, Type serviceType)
{
this.kernel = kernel;
this.serviceType = serviceType;
}
public object GetInstance(InstanceContext instanceContext)
{
return this.GetInstance(instanceContext, null);
}
public object GetInstance(InstanceContext instanceContext, Message message)
{
return kernel.Get(this.serviceType);
}
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
}
}
}
目前,这一解决办法似乎运作良好;依赖注射正在南欧论坛和MVC3站点工作,我可以要求向南欧论坛建筑商提供附属条件,并在申请期间保持欧阵环境。
Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don t feel like I m going down the right route. My issue is that most of the entities ...
Is there any way programmatically or any tool by which i can generate SOAP messages from WCF services ?
I have a .NET website with a WCF service. How do I access the current operations context of my service? One possible work around is to just make a call to the service within the app...but that seems ...
So I got into work early today and got the latest from source control. When I try to launch our ASP.NET application, I get this exception: "The binding at system.serviceModel/bindings/wsHttpBinding ...
I am facing strange issue with our WCF service. The same code was working fine until recently we added more OperationContracts(Web Methods). We have common 3 tier architecture. DAL (WCF) BLL Web ...
I ve finished writing a WCF service that uses TCP. It is meant to run on a Windows 2003 Server, which doesn t have WAS available, so I ve written a Windows service to host my WCF service. It works ...
I am trying to write my first WCF project. I have created a project of type WCF library. In the video I am working from, when F5 is pressed (start debugger) the WCF test client starts up. What do I ...
[Update] - I attach also full config files, for service, and for client (outside of here, not to flood the topic) I m having situation pretty much identical to the one outlined in this question, ...