This fix is broken: 
- contextFactory requires OperationContextScope to be available.
- scope is not disposed properly in case an exception other than ServiceManagementClientException is thrown. 

Can you not move WriteObject(context, true) out of the using block by declaring context object in outer scope rather than making all these changes?