对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)。 –