locked
Silverlight CRM Context Best Practice RRS feed

  • Question

  • I have a Silverlight application that I use to schedule, update and cancel Service Activities. Every once in awhile I'll get strange behavior from certain users that I can't explain. For instance, every time a user edits an appointment subject they'll get an error saying the Scheduled Start time must be less than or equal to the Scheduled End time, even though it is and they did not adjust that.

    My question is how to set up my Service Agent in Silverlight to connect to CRM.

    Currently I am declaring the context once in my class:

    public string orgUri = Connections.orgUri();
    public string CRMUrl = Connections.crmUrl();
    
    private mySrvc.myContext _myContext; 
    
    public mySrvc.myContext MyContext  
    {
       get
       {
          if (_myContext == null)
          {
             _myContext = new mySrvc.myContext(new Uri(string.Format("{0}/xrmservices/2011/organizationdata.svc/", CRMUrl), UriKind.Absolute));
    
             _myContext.IgnoreMissingProperties = true;
       
             _myContext.UseDefaultCredentials = true;
             _myContext.MergeOption = MergeOption.OverwriteChanges;
             _myContext.IgnoreResourceNotFoundException = true;
          }
          return _myContext;
       }
    }

    And in my update/Createfunctions I have the following to update/create for a retrievedSA that is populated:

    MyContext.UpdateObject(retrievedSA);
    MyContext.BeginSaveChanges(OnUpdatedServiceAppointmentCompleted, retrievedSA);
    private void OnUpdatedServiceAppointmentCompleted(IAsyncResult result)
    {
       try
       {
          MyContext.EndSaveChanges(result);
    
    
       }
    }

    Is this acceptable? It works but should I be creating a new context before every MyContext.UpdateObject / MyContext.AddToServiceAppointmentSet(retrievedSA)? Before I call the MyContext.UpdateObject should I always set _myContext = null?

    For example:

    _myContext = null;
    
    MyContext.AddToServiceAppointmentSet(newSA);
    MyContext.BeginSaveChanges(OnCreatedServiceAppointmentCompleted, newSA);

    Just curious about this approach to see why I might be having the issues that I am having.

    Thanks,

    Tim

    Wednesday, October 2, 2013 1:58 PM