我創建簡單的WCF休息服務:Ninject在WCF休息服務託管在Windows服務
[ServiceContract]
public interface IReadData
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "GetHealthStatus")]
string GetHealthStatus();
}
和:
public class ReadData : IReadData
{
private IFacadeToRepo repository;
public ReadData(IFacadeToRepo connectorToRepo)
{
this.repository = connectorToRepo;
}
public string GetHealthStatus()
{
return "ok";
}
}
接下來,我創建了 「映射」:
class NinjectBindings: NinjectModule
{
public override void Load()
{
Bind<IReadData>().To<ReadData>();
Bind<IFacadeToRepo>().To<RepositoryApi>();
}
}
而且最後我創建了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;
}
}
}
所以我認爲現在ninject應該解析IReadData並創建新的ReadData witch默認IFacadeToRepo。但它不起作用。通過installutil安裝windows服務並運行processes - >我收到「服務正在啓動,服務無法啓動,服務沒有報告錯誤。」
你能幫我將這些web服務與ninjects一起運行嗎?
編輯。它看起來像它具有獨特的有問題的:
kernel.Get<IReadData>()
後更換到:
的typeof(READDATA)
並添加默認情況下,空的構造函數READDATA - 它的工作原理
我做一些測試通過逐行添加行。
我一直在尋找一種解決方案,沒有圍繞託管在IIS中,我可以託管與TopShelf使用Windows服務,這解決了我的問題 - 希望我可以upvote不止一次 – JohnChris