I'm trying to test how my t4 template generates outside the context of my addon. I've looked at https://msdn.microsoft.com/en-us/library/68shb4dw.aspx and https://msdn.microsoft.com/en-us/library/bb166401.aspx to get the DTE and the IServicePRovider. However there's a strange behavior while on the context of a UnitTest the following happens:
var dte = Marshal.GetActiveObject("VisualStudio.DTE.15.0") as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
System.IServiceProvider serviceProvider = new Microsoft.VisualStudio.Shell.ServiceProvider(dte);
// This returns a valid System.__ComObject (not null)
object serviceInstance = serviceProvider.GetService(typeof(Microsoft.VisualStudio.TextTemplating.VSHost.STextTemplating));
// Throws an exception
var textTemplating = (Microsoft.VisualStudio.TextTemplating.VSHost.STextTemplating)serviceInstance;
The exception text:
System.InvalidCastException: 'Unable to cast COM of type 'System.__ComObject' to interface type 'Microsoft.VisualStudio.TextTemplating.VSHost.STextTemplating'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{508349B6-6B84-4DF5-91F0-309BEEBAD82D}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x800040002 (E_NOINTERFACE)). (Microsoft.VisualStudio.OLE.Interop)
I've tried querying the IServiceProvider with the specific STextTemplate GUID and it returns HRESULT = 0, so this seems quite odd.
Thank you!