對OrganizationServiceProxy對象調用.Dispose()會產生什麼樣的影響?爲什麼要調用OrganizationServiceProxy.Dispose()?
有時,在測試期間,代碼崩潰之前,可以丟棄對象;這是否意味着一個服務渠道是永恆的?
我有關於OrganizationServiceContext的相同問題,我一直沒有處理,直到今天讀this。
/* Synchronizes with CRM * */
public class CRMSync
{
[ThreadStatic] // ThreadStatic ensures that each thread gets a copy of these fields
private static OrganizationServiceProxy service;
[ThreadStatic]
private static Context linq;
/* Tries to connect to CRM and return false if failure - credentials arguments */
private bool Connect(string username = @"username", string password = "password", string uri = @"orgUrl/XRMServices/2011/Organization.svc")
{
try
{
var cred = new ClientCredentials();
cred.UserName.UserName = username;
cred.UserName.Password = password;
service = new OrganizationServiceProxy(new Uri(uri), null, cred, null);
service.EnableProxyTypes(); // this has to happen to allow LINQ early bound queries
linq = new Context(service);
var who = new Microsoft.Crm.Sdk.Messages.WhoAmIRequest(); // used to test the connection
var whoResponse = (Microsoft.Crm.Sdk.Messages.WhoAmIResponse)service.Execute(who); // this fails if not connected
}
catch (Exception e)
{
Log(e.Message); // Write to Event Log
return false;
}
return true;
}
}
是否有另一種方式,使用多種方法相同OrganizationServiceContext和OrganizationServiceProxy?
我打算使用此析構函數處置OrganizationServiceProxy和OrganizationServiceContext:
~CRMSync()
{
if (service != null)
service.Dispose();
if(linq!=null)
linq.Dispose();
}
編輯
這是ONSTART
/* Called by CRMAUX.OnStart when it is time to start the service */
public async void Start()
{
this.ProcessCSVFiles(); // Creates a ThreadPool thread that processes some CSV files
this.ProcessCases(); // Imports cases into CRM from a db (on this thread)
var freq = 0;
ConfigurationManager.RefreshSection("appSettings");
var parse = int.TryParse(ConfigurationManager.AppSettings["Frequency"], out freq);
await System.Threading.Tasks.Task.Delay((parse) ? freq * 1000 * 60 : 15000 * 60); // 15 minutes default or user defined
Start(); // Start again after the wait above
}
由服務調用的方法這是Windows服務
public partial class CRMAUX : ServiceBase
{
private CRMSync crmSync;
public CRMAUX()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
ConfigurationManager.RefreshSection("userSettings"); // Get the current config file so that the cached one is not useds
if (TestConfigurationFile())
{
crmSync = new CRMSync();
Thread main = new Thread(crmSync.Start);
main.IsBackground = true;
main.Start();
}
else //The configuration file is bad
{
Stop(); // inherited form ServiceBase
return;
}
}
protected override void OnStop()
{
}
/* Checks the configuration file for the necessary keys */
private bool TestConfigurationFile()...
}
Windows服務應用程序創建CRMSync對象並調用CRMSync.Start()。 Start()調用一個方法創建一個新的線程,該線程從csv中提取數據; Start()也調用一個將數據庫中的案例推送到CRM中的方法。我會添加更多的代碼;你認爲有沒有更好的方法來做到這一點?我正在考慮擺脫私有成員,只使用包裝在使用中的本地OrganizationServiceContext和OrganizationServiceProxy變量。 – Bvrce
我會用後者去使用非靜態實例。由於身份驗證,代理上的昂貴操作正在旋轉初始連接。我首先要測量它是否是性能瓶頸。如果你想增加導入的吞吐量,你可以看看使用任務並行庫(.NET 4.5中的async/await)。 –