Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

ThreadPool + ServiceContext

$
0
0

Hi @ all,

I am trying to use several Threads at the same time to consume the crm webservice.

This is the scenario:

1) Create OrganizationServiceProxy

2) Get all active Accounts

 

public IList<Account> GetAccounts(XRM context)
    {
      return (from a in context.AccountSet where a.StatusCode == new OptionSetValue(1) select a).ToList();
    }

 

3) Because I used the using-command my OrganizationServiceProxy and Servicecontext should be disposed

4) ForEach - Loop through all Accounts

5) For every Account use a Thread to execute a Method called Test-Method

 

 ThreadPool.QueueUserWorkItem((o) => TestMethod(account1));

 

6) In the TestMethod - Create new OrganzationServiceProxy / ServiceContext (again with using command)

7)  acc.property = 55; //Set a generic property

Thread.Sleep(2000); //Just using to simulate a longer execution time

serviceContext.UpdateObject(acc); //Update the generic property

serviceContext.SaveChanges();

 

When I am running the code - I get the following error (happens randomly):

Unhandled Exception: System.InvalidOperationException: Metadata contains a reference that cannot be resolved: 'http://crm2011rc.dev.local/Test/XrmServices/2011/Organization.svc?wsdl=wsdl0'. ---> System.Net.WebException: The operation has timed out
  at System.Net.HttpWebRequest.GetResponse()
  at System.ServiceModel.Description.MetadataExchangeClient.MetadataLocationRetriever.DownloadMetadata(TimeoutHelper timeoutHelper)
  at System.ServiceModel.Description.MetadataExchangeClient.MetadataRetriever.Retrieve(TimeoutHelper timeoutHelper)
  --- End of inner exception stack trace ---
  at System.ServiceModel.Description.MetadataExchangeClient.MetadataRetriever.Retrieve(TimeoutHelper timeoutHelper)
  at System.ServiceModel.Description.MetadataExchangeClient.ResolveNext(ResolveCallState resolveCallState)
  at System.ServiceModel.Description.MetadataExchangeClient.ResolveNext(ResolveCallState resolveCallState)
  at System.ServiceModel.Description.MetadataExchangeClient.GetMetadata(MetadataRetriever retriever)
  at System.ServiceModel.Description.MetadataExchangeClient.GetMetadata(Uri address, MetadataExchangeClientMode mode)
  at Microsoft.Xrm.Sdk.Client.ServiceMetadataUtility.RetrieveServiceEndpoints(Type contractType, Uri serviceUri)
  at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1..ctor(Uri serviceUri)
  at Microsoft.Xrm.Sdk.Client.OrganizationServiceConfiguration..ctor(Uri serviceUri)
  at Microsoft.Xrm.Sdk.Client.ServiceConfigurationFactory.CreateConfiguration[TService](Uri serviceUri)
  at Microsoft.Xrm.Sdk.Client.ServiceProxy`1..ctor(Uri uri, Uri homeRealmUri, ClientCredentials clientCredentials, ClientCredentials deviceCredentials)
  at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy..ctor(Uri uri, Uri homeRealmUri, ClientCredentials clientCredentials, ClientCredentials deviceCredentials)
  at ThreadHelper.Program.GetOrgService() in C:\Users\cott\Documents\Visual Studio 2010\Projects\ThreadHelper\ThreadHelper\Program.cs:line 159
  at ThreadHelper.Program.TestMethod(Object account1) in C:\Users\cott\Documents\Visual Studio 2010\Projects\ThreadHelper\ThreadHelper\Program.cs:line 77
  at ThreadHelper.Program.<>c__DisplayClass1.<Run>b__0(Object o) in C:\Users\cott\Documents\Visual Studio 2010\Projects\ThreadHelper\ThreadHelper\Program.cs:line 49
  at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
  at System.Threading.ExecutionContext.runTryCode(Object userData)
  at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
  at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
  at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
  at System.Threading.ThreadPoolWorkQueue.Dispatch()
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
Exception occures: System.ServiceModel.Security.SecurityNegotiationException: Secure channel cannot be opened because security negotiation with the remote endpoint has failed. This may be due to absent or incorrectly specified EndpointIdentity in the EndpointAddress used to create the channel. Pleas
e verify the EndpointIdentity specified or implied by the EndpointAddress correctly identifies the remote endpoint. ---> System.ServiceModel.FaultException: The request for security token has invalid or malformed elements.
  at System.ServiceModel.Security.SecurityUtils.ThrowIfNegotiationFault(Message message, EndpointAddress target)
  at System.ServiceModel.Security.SspiNegotiationTokenProvider.GetNextOutgoingMessageBody(Message incomingMessage, SspiNegotiationTokenProviderState sspiState)
  --- End of inner exception stack trace ---

It looks like I am running out of connections - but how is this possible ? Does anyone already have experience with the new OrganizationServiceProxy in MSCRM 2011 & Threading ? 

cheers,

Uter ;)

 


Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>