When you’ve created your ExternalDataExchange interface and class, remember, when you have a CallExternalMethod activity, to invoke the external method in a separate thread.
So if you have InvokeSendPreview that you want to invoke from a WF, then ensure that you encapsulate the call to the event inside a new thread, for instance, via the ThreadPool, as follows.
[Serializable]
public class CommunicationService : ICommunicationService
{
#region ICommunicationService Members
public event EventHandler<ExternalDataEventArgs> SendPreview;
public void InvokeSendPreview()
{
Guid instanceId = WorkflowEnvironment.WorkflowInstanceId;
ThreadPool.QueueUserWorkItem(delegate(object state)
{
SendPreview(null, new ExternalDataEventArgs(instanceId));
});
}
#endregion
}
The error at the beginning of this post was caused by
public void InvokeSendPreview()
{
Guid instanceId = WorkflowEnvironment.WorkflowInstanceId;
SendPreview(null, new ExternalDataEventArgs(instanceId)); // ERROR HERE!
}
This error is because the WF threads are not to be used to execute code outside WF.