I have a system that uses System.AddIn (MAF) to host addins in separate processes. Inside the addins I would like to consume some code that leverages async and await.
Since MAF uses remoting to communicate across processes and there is very little control we have over this communication, I am trying to determine the best way to consume the async code without running the risk of hitting a deadlock.
For example, right now I have a contract which defines a method like so:
[AddInContract]
public interface IWorker : IContract
{
string DoWork(string workToDo);
}
Inside the AddIn implementation of DoWork
, I want to be able to consume an async API. It would be great if I could come up with a way of converting from string to Task<string>
inside the AddInAdapter and HostAdapter, but I want to be sure that I am not setting myself up for a deadlock.
I would like to define my view contract as:
[System.AddIn.Pipeline.AddInBaseAttribute()]
public interface IWorker
{
Task<string> DoWork(string workToDo);
}
Then is it safe to do the following inside the contract to view adapter:
public virtual string DoWork(string workToDo)
{
var t = _view.DoWork(Contracts.AddInSideAdapters.DoWorkAddInAdapter.ContractToViewAdapter(workToDo)).ConfigureAwait(false);
return t.GetAwaiter().GetResult();
}