Reputation: 1519
I created simple WCF Rest service:
[ServiceContract]
public interface IReadData
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "GetHealthStatus")]
string GetHealthStatus();
}
And:
public class ReadData : IReadData
{
private IFacadeToRepo repository;
public ReadData(IFacadeToRepo connectorToRepo)
{
this.repository = connectorToRepo;
}
public string GetHealthStatus()
{
return "ok";
}
}
Next I created "mapper":
class NinjectBindings: NinjectModule
{
public override void Load()
{
Bind<IReadData>().To<ReadData>();
Bind<IFacadeToRepo>().To<RepositoryApi>();
}
}
And finally I created ServiceBase:
public partial class MyRestWCFRestWinSer : ServiceBase
{
ServiceHost readDataServiceHost = null;
private StandardKernel kernel;
public MyRestWCFRestWinSer()
{
kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
InitializeComponent();
}
public void OnDebug()
{
OnStart(null);
}
protected override void OnStart(string[] args)
{
readDataServiceHost = new ServiceHost(kernel.Get<IReadData>());
readDataServiceHost.Open();
}
protected override void OnStop()
{
if (readDataServiceHost != null)
{
readDataServiceHost.Close();
readDataServiceHost = null;
}
}
}
So I thought that now ninject should resolve IReadData and create new ReadData witch default IFacadeToRepo. But it doesn't work. After install windows service via installutil and running proces -> I receive "The service is starting. The service could not be started. The service did not report an error."
Could you help me with running these web services together with ninjects?
edit. It look like it have problem exclusive with:
kernel.Get<IReadData>()
after replace it to:
typeof(ReadData)
and adding default, empty constructor to ReadData - it works
I made some testing by adding line after line.
Upvotes: 1
Views: 291
Reputation: 1519
It appear to be very simple problem.
Firstly: When we are loading kernel into ninject, it should start method Load which was overwritten by
public override void Load().
But it doesn't work. Solution? Problem regarding to availability of this class:
class NinjectBindings: NinjectModule
I changed it into:
public class NinjectBindings: NinjectModule
In result it is work.
Second thing: I needed to change service behaviour by adding ServiceBehaviour:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
public class ReadData : IReadData
That's it.
Upvotes: 1
Reputation: 908
I have done that with another DI Container "Castle Windsor" and WCF rest service. The trick is to add a ServiceRoute to the RoutTable on application start event as follows.
RouteTable.Routes.Add(new ServiceRoute("servicename", new WindsorServiceHostFactory<RestServiceModel>(), typeof(IReadData)));
1- Look for class similar to WindsorServiceHostFactory in Ninject.
2- Call the service via http://xx.com/servicename/GetHealthStatus (for hosting under IIS, same must apply to windows service hosting but different address i assume). It should work without parameter-less constructor.
Upvotes: 0