[英]Error stringifying an object when calling Custom Action in OData D365 Online
[英]Custom services from IServiceProvider in D365
我們最近開始使用 D365 構建應用程序。 這是從 java 的轉變,我們使用 spring 進行依賴注入。 似乎IServiceProvider
進行了依賴注入,但我還沒有找到任何方法讓它與我們的本土服務一起工作。
我們的代碼如下所示:
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
// ... do stuff ...
}
我想添加以下行:
MyHomeGrownService myHomeGrownService = (MyHomeGrownService )serviceProvider.GetService(typeof(MyHomeGrownService));
但是,我不知道如何注冊MyHomeGrownService
; 我也沒有找到很多關於這個主題的文檔。
如果沒有正確的依賴注入,開發人員不得不在代碼中使用new
,這使得正確的單元測試幾乎是不可能的。
如何注冊定制服務?
現在,我創建了一個委托IServiceProvider
實現,用於在每個插件調用中包裝給定的IServiceProvider
。 該自定義實現知道我們的服務,並會在詢問時創建這些服務。 其他一切,它代表。
我試圖讓委托IServiceProvider
更快,但我還沒有在實時系統中嘗試過,所以我不知道它的性能如何。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.