Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple conflicts with Microsoft.VisualStudio.Interop in VS2022 #136

Open
miksh7 opened this issue Mar 16, 2022 · 23 comments
Open

Multiple conflicts with Microsoft.VisualStudio.Interop in VS2022 #136

miksh7 opened this issue Mar 16, 2022 · 23 comments

Comments

@miksh7
Copy link

miksh7 commented Mar 16, 2022

In Visual Studio 2022 there are many conflicts with classes defined in Microsoft.VisualStudio.Interop

Just a few of them:
Error Compiling transformation: The type 'CodeTypeRef' exists in both 'EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'Microsoft.VisualStudio.Interop, Version=17.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

Error Compiling transformation: The type 'CodeClass2' exists in both 'EnvDTE80, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'Microsoft.VisualStudio.Interop, Version=17.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

Error Compiling transformation: The type 'SVsQueryEditQuerySave' exists in both 'Microsoft.VisualStudio.Interop, Version=17.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

@davidebbo
Copy link
Contributor

Maybe they changed something. Can you try to figure out the root cause and propose solution?

@miksh7
Copy link
Author

miksh7 commented Mar 16, 2022

Looks like, so far a straightforward fix may work, e.g. instead of SVsQueryEditQuerySave use Microsoft.VisualStudio.Shell.Interop.SVsQueryEditQuerySave

@davidebbo
Copy link
Contributor

I see, and I assume same for CodeTypeRef and CodeClass2? I haven't touched T4MVC in a long time, and right now I'm not really set up. But if that's the fix, we can get it in at some point.

In the meantime, people will hopefully find this issue and work around!

@DanielRieck
Copy link

Looks like, so far a straightforward fix may work, e.g. instead of SVsQueryEditQuerySave use Microsoft.VisualStudio.Shell.Interop.SVsQueryEditQuerySave

I tried that, but still get the error. Was there something else you did?

@adamjford
Copy link

I fixed this by removing the assembly imports of EnvDTE, EnvDTE80, and Microsoft.VisualStudio.Shell.Interop. (The namespace imports are still necessary.)

I should note I did this in T4MVC v3.17.4, so your mileage may vary with newer versions.

@aborja79
Copy link

I resolved it in addition to removing the assembly imports of EnvDTE, EnvDTE80, and Microsoft.VisualStudio.Shell.Interop what adamjford says, adding the Microsoft.VisualStudio.Sdk nuget package to the project, but too many unnecessary dependencies are created and I do not like the solution.
Any better solution?

@miksh7
Copy link
Author

miksh7 commented Apr 19, 2022

The issue disappeared in Visual Studio 2022 ver. 17.1.4. No template code change is required.

@cblaze22
Copy link

I am getting this issue and on 17.1.4, can't generate code with 2022 update.

@aborja79
Copy link

I have just updated to 17.1.4 and I am getting same error

@cblaze22
Copy link

I removed these in the T4MVC.tt and it seemed to work.

<#@ assembly name="Microsoft.VisualStudio.Shell.Interop" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="EnvDTE80" #>

@aborja79
Copy link

Yes, If I remove that lines it's working in Visual Studio 2022 but no in VS 2019.
Is there any conditional lines to load that assemblies in previous versions?

@miksh7
Copy link
Author

miksh7 commented Apr 21, 2022

Interesting... not sure what did the trick for me but everything works fine. Recently, I have installed ".NET desktop development" feature, maybe this one helped?

@oceania-npc
Copy link

Interesting... not sure what did the trick for me but everything works fine. Recently, I have installed ".NET desktop development" feature, maybe this one helped?

Installing this .Net desktop feature (check the c# templates box) along with this stackoverflow post worked for me:
https://stackoverflow.com/questions/71575357/vs2022-17-2-0-preview-2-0-t4-template-serialization-exception-when-accessing-pr

<#@ assembly name="Microsoft.VisualStudio.Interop" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="EnvDTE80" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
and then swap out the IServiceProvider's GetService for GetCOMService
//var dte = hostServiceProvider.GetService(typeof(DTE)) as DTE2;
var dte = hostServiceProvider.GetCOMService(typeof(DTE)) as DTE2;

@miksh7
Copy link
Author

miksh7 commented Jul 4, 2022

After another VS2022 update I got the issue back and simple replacement "GetService(typeof(DTE))" -> "GetCOMService(typeof(DTE))" worked for me.

@AkosLukacs
Copy link
Contributor

With VS2022 v.17.2.4 and T4MVC 4.0.0, I got this error:

Error		Running transformation: System.Runtime.Serialization.SerializationException: Type 'Microsoft.VisualStudio.CommonIDE.Solutions.Dte.DteProjects' in Assembly 'Microsoft.VisualStudio.CommonIDE, Version=17.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

Server stack trace: 
   at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
   at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.SerializeMessageParts(ArrayList argsToSerialize)
   at System.Runtime.Remoting.Messaging.SmuggledMethodReturnMessage..ctor(IMethodReturnMessage mrm)
   at System.Runtime.Remoting.Messaging.SmuggledMethodReturnMessage.SmuggleIfPossible(IMessage msg)
   at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoDispatch(Byte[] reqStmBuff, SmuggledMethodCallMessage smuggledMcm, SmuggledMethodReturnMessage& smuggledMrm)
   at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoTransitionDispatchCallback(Object[] args)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at EnvDTE._Solution.get_Projects()

Tried the solutions mentioned here, updated VS to 17.2.6, and T4MVC to 4.2.4, but still the same exception. Any new ideas?

@zturchan
Copy link

With VS2022 v.17.2.4 and T4MVC 4.0.0, I got this error:

We are also observing similar problems. Will update if we get anything to work.

@zturchan
Copy link

The other solution mentioned in that issue appears to work for me.

"GetService(typeof(DTE))" -> "GetCOMService(typeof(DTE))"

@aborja79
Copy link

aborja79 commented Oct 6, 2022

I am having the same problem with version 17.3.5.
If I remove the 3 lines it works for me in VS2022 but not in VS2019.
The change from GetService to GetCOMService does not affect anything since if I do not remove the 3 lines it does not work for me.
Something help, please?

@ghost
Copy link

ghost commented Oct 11, 2022

I removed following lines from T4MVC.tt and it works in vs2022.

<#@ assembly name="Microsoft.VisualStudio.Shell.Interop" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="EnvDTE80" #>

Thanks @adamjford And @cblaze22

@aborja79
Copy link

I removed following lines from T4MVC.tt and it works in vs2022.

<#@ assembly name="Microsoft.VisualStudio.Shell.Interop" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="EnvDTE80" #>

Thanks @adamjford And @cblaze22

Sorry, If I remove that lines is not working in VS2019

@Niels-V
Copy link

Niels-V commented Mar 7, 2023

So this comes down to edit the .tt to either work in VS2019 or work in VS2022. I would love to hear if there is another way to conditionally include imports based on VS version.
I might end up hacking the project file to do this. Or consider dropping VS2019 for this solution.

@NaserParhizkar
Copy link

I got this error even with Visual Studio 17.7 preview3.0 for
(https://learn.microsoft.com/en-us/visualstudio/modeling/design-time-code-generation-by-using-t4-text-templates?view=vs-2022&tabs=csharp#getting-data-from-visual-studio)
sample code
I think those changes are related to global references which Visual Studio new versions behind scene add them for all projects

@NaserParhizkar
Copy link

NaserParhizkar commented Jul 17, 2023

In order to figure out how many assemblies are added behind scene I wrote this piece of code in TextTemplate file (aka TT)

<#@ template hostspecific="true" debuge="true" language="C#" #>
<#@ output extension=".txt" #>

<#
  var assemblyReferences = this.Host.StandardAssemblyReferences;
  foreach(string assemblyRef in assemblyReferences)
  {
     WriteLine(assemblyRef);
  }

#>

and I got

System
Microsoft.VisualStudio.Interop
C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll
C:\ProgramFiles\MicrosoftVisualStudio\2022\Preview\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.TextTemplating.VSHost.dll

which when I checked (Microsoft.VisualStudio.Interop). it contains a EnvDTE.DTE type causes conflict with

<#@ assembly name="EnvDTE" #>

code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests