Dynamics crm CRM服务和上下文可以是静态的吗?

Dynamics crm CRM服务和上下文可以是静态的吗?,dynamics-crm,microsoft-dynamics,Dynamics Crm,Microsoft Dynamics,这是设计问题/查询,所以我希望可以发布 您看到的几乎每个CRM OrgService和上下文示例都不是静态的。通常使用使用块。这当然是可行的,但这是一个静态类,而不是通过到处使用块一次又一次地创建/破坏它吗?这样做有什么问题吗 谢谢你的建议:-) 编辑:下面是代码。您认为这有什么问题吗?.. CrmServiceContext由早期绑定实体生成器创建 static public class CRMOrgService { static private IOrganizationServi

这是设计问题/查询,所以我希望可以发布

您看到的几乎每个CRM OrgService和上下文示例都不是静态的。通常使用
使用
块。这当然是可行的,但这是一个静态类,而不是通过到处使用块一次又一次地创建/破坏它吗?这样做有什么问题吗

谢谢你的建议:-)

编辑:下面是代码。您认为这有什么问题吗?..

CrmServiceContext
由早期绑定实体生成器创建

static public class CRMOrgService
{
    static private IOrganizationService _orgService = null;
    static private CrmServiceContext _context = null;

    static public IOrganizationService OrgService {
        get
        {
            if (_orgService == null)
            {
                var reader = new AppSettingsReader();
                Uri crmUri = new Uri(reader.GetValue("CRMOrgSvc", typeof(string)).ToString());
                string crmUser = reader.GetValue("CRMUser", typeof(string)).ToString();
                string crmPass = reader.GetValue("CRMPass", typeof(string)).ToString();
                // Your client credentials   
                ClientCredentials clientCredentials = new ClientCredentials();
                clientCredentials.UserName.UserName = crmUser;
                clientCredentials.UserName.Password = crmPass;
                // Create your Organization Service Proxy  
                var proxy = new OrganizationServiceProxy(crmUri, null, clientCredentials, null);
                proxy.EnableProxyTypes();
                _orgService = (IOrganizationService)proxy;
            }

            return _orgService;
        }
    }

    static public CrmServiceContext Context
    {
        get
        {
            if (_context == null)
            {
                _context = new CrmServiceContext(OrgService);
            }
            return _context;
        }
    }

    static public void CloseCleanUp() 
    {
        _context.ClearChanges();
        _context.Dispose();
        _context = null;
        _orgService = null;
    }

} // end class

是的,它可以是静态的。但是,您必须记住,这个实例不是线程安全的。这意味着连接实例不能由多个线程同时使用。

是的,它可以是静态的。但是,您必须记住,这个实例不是线程安全的。这意味着,连接实例不能由多个线程同时使用。

那么这在WindowsService应用程序中是否正常?那么在WindowsService应用程序中是否正常?