The code blow works, it creates a task, but the AsyncCallback in BeginExcecute throws an exception that I can't catch?
(the method appendDescription adds given information to a textfield in CRM)
private void createTask(string summary) { OrganizationRequest request = new OrganizationRequest() { RequestName = "Create" }; Entity entity = new Entity(); entity.LogicalName = "task"; entity.Attributes = new AttributeCollection(); entity.Attributes.Add(new SoapForSilverlightSample.CrmSdk.KeyValuePair<string, object>() { Key = "subject", Value = txtSummery.Text.Trim() }); entity.Attributes.Add(new SoapForSilverlightSample.CrmSdk.KeyValuePair<string, object>() { Key = "regardingobjectid", Value = new EntityReference() { LogicalName = "incident", Id = getIncidentID() } }); //request properties need to be explicitly named as strings request["Target"] = entity; appendDescription("Start", "[createTask]"); try { IOrganizationService service = SoapForSilverlightSample.SilverlightUtility.GetSoapService(); //depending on how you do things your calls will most likely be asynchronous service.BeginExecute(request, new AsyncCallback(CrmCreate_Callback), service); appendDescription("Saved!", "[createTask]"); } catch(Exception ex) { appendDescription(ex.Message, "[Excption => createTask]"); } } private void CrmCreate_Callback(IAsyncResult result) { OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result); Guid results = (Guid)response["id"]; Guid CreatedEntity = results; appendDescription("utan EndExecute", "[CrmCreate_Callback]"); }
The exception
Unhandled Error in Silverlight Application An AsyncCallback threw an exception. at System.ServiceModel.AsyncResult.Complete(Boolean completedSynchronously)
at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStreamAsyncResult.OnGetRequestStream(IAsyncResult result)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass14.<InvokeGetRequestStreamCallback>b__12(Object state2)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()