2011-06-27 31 views
5

我們試圖在CRM2011插件中使用早期綁定類型。要啓用此功能,我們需要添加ProxyTypesBeavior(),或撥打EnableProxyTypes()。但是,這兩個屬性均適用於OrganizationServiceProxy類,並且在IOrganizationService接口上不存在。在CRM2011插件中創建OrganizationServiceProxy使用早期綁定

所以,如果我們使用下面的代碼來獲取組織服務,我們如何獲得代理類來設置上述屬性?

var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
var service = serviceFactory.CreateOrganizationService(context.UserId); 

回答

-2

這樣寫,

IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); 
+0

請做解釋 – johannes

+1

這不是一個答案,它只是一個建議,使用顯式類型而不是推斷類型。這是一種文體偏好,使用var!肯定沒有錯。 –

3

對於那些使用CRM在線,反射的解決方案是行不通的,因爲你停留在沙盒模式。

下面的解決方案使用IProxyTypesAssemblyProvider接口(由Pavel Korsukov建議)爲我工作(source)。

var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 

var proxyTypesProvider = factory as IProxyTypesAssemblyProvider; 
if (proxyTypesProvider != null) 
{ 
    proxyTypesProvider.ProxyTypesAssembly = typeof(Xrm.XrmServiceContext).Assembly; 
} 
// Use the factory to generate the Organization Service. 
var service = factory.CreateOrganizationService(context.UserId); 
相關問題