WCF using Ninject Dispose not triggering in requestscope - wcf

Here is my module
internal class WebServiceConfiguration : NinjectModule
{
public override void Load()
{
Bind<IWebService>().To<WebService>().InRequestScope();
}
}
Here is the global.asax
public class Global : NinjectHttpApplication
{
protected override IKernel CreateKernel()
{
return new StandardKernel(new WebServiceConfiguration());
}
}
I also tried InScope(c => OperationContext.Current)
Here is my service with IDisposable in IWebService
[ServiceBehavior(InstanceContextMode = InstanceContextModeDefinition.Mode)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WebService : IWebService
{
private readonly ISomeService _someService;
public WebService(ISomeService someService)
{
_someService = someService;
}
public void Dispose()
{
_someService.Dispose();
}
Here is the ServiceHostFactory in the service markup
<%# ServiceHost Language="C#" Debug="true" Factory="Ninject.Extensions.Wcf.NinjectDataServiceHostFactory" Service="WCFTest.Services.WebService" CodeBehind="WebService.svc.cs" %>
The injection of dependencies works. My only concern is that the dispose method is not being triggered when the Client closes the service call.
I tried to remove the Factory="Ninject.Extensions.Wcf.NinjectDataServiceHostFactory" just to test if the Dipose will be called, and it did call but of course i won't have auto injection. So there might be something i'm doing wrong in the setup? or there is a bug on ninject not calling Dispose?
Note: I grab the sample setup in ninject wcf extension and just added some DI.
Your help will be appreciated.
BTW: I'm using Ninject 3.0.0.15, Ninject.Extensions.Wcf 3.0.0.5, Ninject.Web.Common 3.0.0.7

Use
Bind<IWebService, WebService>().To<WebService>().InRequestScope();

Related

How to write an extension method that allows you to set options without creating the options instance

I really like the pattern where I can configure a service through an option class without having to create it, but I can't find an example of how to write an extension method that allows me to use that same pattern such as the one below that exists for registering a DbContext.
services.AddDbContext<MyDbContext>(options => options.EnableDetailedErrors());
I can see the method signature uses an action method, but I can't seem to find the extension class in GitHub for ASP.NET Core that shows me how to write an extension method using that type of option builder pattern.
For example, take the following service code. How would I write the extension method so that I could configure the options during service registration.
public void ConfigureServices(IServiceCollection services)
{
services.AddMyService(options => options.SomeSetting = true);
}
public interface IMyService
{
void DoSomething();
}
public class MyService : IMyService
{
private readonly MyServiceOptions _options;
public MyService(IOptions<MyServiceOptions> options)
{
_options = options.Value;
}
public void DoSomething()
{
Console.WriteLine(_options.SomeSetting);
}
}
public static class MyServiceExtensions
{
// How would I write this extension method so that I could configure it with options overload
public static IServiceCollection AddMyService(this IServiceCollection services, Action<MyServiceOptions> configure)
{
services.AddSingleton<IMyService, MyService>();
return services;
}
}
ASP.NET Core provides this mechanism with the IConfigureOptions
interface. You implement this interface in a configuration class and
use it to configure the IOptions object in any way you need.
It's as easy as:
public class MyServiceConfiguration : IConfigureOptions<MyServiceOptions>
{
private MyServiceOptions _options;
public MyServiceConfiguration(IOptions<MyServiceOptions> options)
{
_options = options.Value;
}
public void Configure(MyServiceOptions options)
{
options.SomeSetting = _options.SomeSetting;
options.SomeOtherSetting = _options.SomeOtherSetting;
}
}
All that remains is to register this implementation in the DI container.:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<MyServiceOptions>(options => options.SomeOtherSetting = true);
services.AddSingleton<IMyService, MyService>();
}
With this configuration, when IOptions is injected into your service, the MyServiceOptions object will be configured by the ConfigureMyServiceOptions class.
Be careful! The ConfigureMyServiceOptions object is registered as a singleton,
so it will capture any injected services of scoped or transient lifetimes.

Can't get Ninject Interception to work with WCF

we're switching from UNITY to Ninject as our default service locator for WCF services. There's a beautiful NuGet package available for doing this and getting the standard resolution up-and-running is a breeze.
However - I want to intercept the calls to my WCF service. Here's what I've got:
My svc file:
<%# ServiceHost Language="C#" Debug="true" Service="NinjectWcfApp.Service1" Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory" %>
Here's my service:
public class Service1 : IService1
{
[Inject]
public ISomeManager Manager { get; set; }
public string GetData(int value)
{
if(this.Manager != null)
this.Manager.DoStuff();
return string.Format("You entered: {0}", value);
}
}
Kernel is built up like this:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ISomeManager>().To<SomeManager>();
kernel.Bind<IService1>().To<Service1>().Intercept().With<MyInterceptor>();
}
If I setup the kernel like this, the manager instance is injected, but there's no interception taking place. My interceptor that logs something before execution and after execution is never invoked.
Other Stackoverflow threads suggest using:
kernel.Bind<Service1>().ToSelf().Intercept().With<MyInterceptor>();
If I do that, the manager is not being injected. If I then go ahead and create a constructor which takes in the manager, it works, but again: no interception.
kernel.Bind<Service1>().ToSelf().WithConstructorArgument("manager", kernel.Get<ISomeManager>()).Intercept().With<MyInterceptor>();
What am I doing wrong here?
Any methods which are intercepted must be virtual:
public virtual string GetData(int value)

NinjectServiceHost in WCF service does not call Dispose()

I've been trying to get the Dispose method on my IDisposable WCF service called whilst using Ninject's NinjectServiceHost without any luck. I've then downloaded the Ninject.extensions.WCF example code and tried to get the IDisposable TimeService's Dispose() method to be called, but it does not get called either.
The service is instantiated correctly, just the Dispose() doesn't get called.
Is this a bug or something that myself and the example code are missing?
I've created a stripped down service and testing host that reproduces the issue. The code is below.
I'm using Ninject 3.0.1.10, Ninject.extensions.WCF 3.0.0.5, .net 4.5
ServiceModule.cs code (for setting up bindings)
using Ninject.Modules;
namespace TestNinjectWcf
{
public class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<Service1>().ToSelf();
// I've also tried Bind<IService1>().To<Service1>()
// and also tried various scopes such as InParent() and InRequestScope()
}
}
}
Console Test Program to start the service.
using System;
using Ninject.Extensions.Wcf;
using Ninject;
using TestNinjectWcf;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
var kernel = new StandardKernel(new ServiceModule());
var service = kernel.Get<NinjectServiceHost<Service1>>();
service.Open();
Console.WriteLine("Service Started");
Console.ReadKey();
service.Close();
}
}
}
Service Implementation
using System;
using System.Diagnostics;
using System.ServiceModel;
namespace TestNinjectWcf
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1, IDisposable
{
public Service1()
{
Debug.WriteLine("Constructor");
}
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public void Dispose()
{
Debug.WriteLine("Dispose"); // This line never gets called!
}
}
}
Maybe it is that you have created singleton service ? (InstanceContextMode.Single)
Only one InstanceContext object is used for all incoming calls and is not recycled subsequent to the calls. If a service object does not exist, one is created

WCF service with ninject extension

i got problem with my wcf service with ninject extension. When my service doesnt have parameterless constructor when im trying to call it i got error that im missing it, when i got parameterless constructor:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class ContractService: IContractService
{
public ContractService()
: this(new AddressService(), new TelephoneService())
{
}
[Inject]
public ContractWCFService(IAddressService addressService, ITelephoneService telephoneService)
{
_addressService = addressService;
_telephoneService = telephoneService;
}...
}
then, parameterless one is called and i think the second one with attribute [Inject] should be called.
Edit : NinjectWebCommon:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ServiceHost>().To<NinjectServiceHost>();
kernel.Bind<IAddressService>().To<AddressService>().InHttpRequestScope();
kernel.Bind<TelephoneService>().To<TelephoneService>().InHttpRequestScope();
kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InHttpRequestScope();
kernel.Bind(typeof(EFModel)).ToSelf().InHttpRequestScope();
IoC.Initialize((StandardKernel)kernel);
}
Any ideas?
For passing parameters / managing your instance with ninject, you should combine Instance Provider
and / or specific ninject extenstion.
This post can be also helpful: Passing parameters to a WCF ServiceHost type with Ninject 2

Ninject with WCF and Interception (for AOP)

I've been trying to get the ninject working in wcf, using the wcf extension and the interception with dynamicproxy2 extension. I've basically created a Time attribute and have it all working in a basic scenario. Where I get trouble is when in ninject module I create my service binding with a constructor argument:
Bind<IMyDependency>().To<MyDependency>();
Bind<IService1>().To<Service1>().WithConstructorArgument("dependency", Kernel.Get<IMyDependency>());
Everything works fine, but the Time attribute wont fire on anything in my Service1 or MyDependency.
The time attribute is the standard one floating all over the internet. The only other piece of code really is the CreateKernel method is the global.asax, which looks like this:
protected override IKernel CreateKernel() {
IKernel kernel = new StandardKernel(
new NinjectSettings() { LoadExtensions = false },
new WcfNinjectModule(),
new DynamicProxy2Module()
);
return kernel;
}
Thanks for any help!
Matt
EDIT 12/12/2011: As requested, I've added some more detail below:
The entire wcf ninject module:
public class WcfNinjectModule : NinjectModule
{
public override void Load()
{
Bind<IMyDependency>().To<MyDependency>();
Bind<IService1>().To<Service1>();
}
}
The create kernel method in the global.asax is above, and the global.asax inherits from NinjectWcfApplication.
Service method looks like this:
public class Service1 : IService1
{
private IMyDependency _dependency;
public Service1()
{
}
public Service1(IMyDependency dependency)
{
_dependency = dependency;
}
[Time]
public virtual string GetData(string value)
{
return string.Format(_dependency.GetMyString(), value);
}
}
public interface IMyDependency
{
string GetMyString();
}
public class MyDependency : IMyDependency
{
[Time]
public virtual string GetMyString()
{
return "Hello {0}";
}
}
Does this help?
Since removing the 'WithConstructor' argument, the time intercept attribute will fire on GetMyString but not on GetData.
Matt
After a little more work (and writing that last post edit), it turns out that just removing the WithConstructorArgument method did resolve my problem and everything now seems to be working fine.
Matt