Visual Studio serialization error when T4 uses DTE to open generated file
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
We have a C# T4 file named GenerateProxies.tt which calls several command-line codegen utilities. Using the System.Diagnostics Process class, we redirect the standard output to the T4 output text file (GenerateProxies.txt) so that we can review the command-line output for errors.
I added the following simple code to the end of the T4 so that Visual Studio will open the generated text file as the last step in the process (the workingDirectory
variable is declared and populated earlier in the template). This does work but it throws a serialization error. Can this error be avoided?
<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>
<#
IServiceProvider vssp = (IServiceProvider)this.Host;
DTE dte = vssp.GetService(typeof(DTE)) as DTE;
dte.ItemOperations.OpenFile(
string.Format(@"{0}GenerateProxies.txt", workingDirectory),
Constants.vsViewKindTextView
);
#>
Again, this does work, it opens the text file, but it generates this error:
Running transformation: System.Runtime.Serialization.SerializationException:
Type 'Microsoft.VisualStudio.Platform.WindowManagement.DTE.WindowBase' in
Assembly 'Microsoft.VisualStudio.Platform.WindowManagement'
is not marked as serializable.
c# visual-studio-2015 t4 envdte
add a comment |
We have a C# T4 file named GenerateProxies.tt which calls several command-line codegen utilities. Using the System.Diagnostics Process class, we redirect the standard output to the T4 output text file (GenerateProxies.txt) so that we can review the command-line output for errors.
I added the following simple code to the end of the T4 so that Visual Studio will open the generated text file as the last step in the process (the workingDirectory
variable is declared and populated earlier in the template). This does work but it throws a serialization error. Can this error be avoided?
<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>
<#
IServiceProvider vssp = (IServiceProvider)this.Host;
DTE dte = vssp.GetService(typeof(DTE)) as DTE;
dte.ItemOperations.OpenFile(
string.Format(@"{0}GenerateProxies.txt", workingDirectory),
Constants.vsViewKindTextView
);
#>
Again, this does work, it opens the text file, but it generates this error:
Running transformation: System.Runtime.Serialization.SerializationException:
Type 'Microsoft.VisualStudio.Platform.WindowManagement.DTE.WindowBase' in
Assembly 'Microsoft.VisualStudio.Platform.WindowManagement'
is not marked as serializable.
c# visual-studio-2015 t4 envdte
The call stack might help.
– Will
Dec 6 '16 at 19:38
Yeah, but unfortunately it's from a development VM that has no external access at all (which is why I didn't bother to retype all the assembly info, the key GUID and so on). The call stack is huge but it looks like some kind of PInvoke marshalling problem. Apparently DTE is COM. I sort of vaguely suspect it's a threading problem.
– McGuireV10
Dec 7 '16 at 12:15
Smells to me like something is being accidentally pulled across an AppDomain border. The call stack could identify the source, and you could investigate, at the bottom of your code in the stack, who has a reference to an instance of that type.
– Will
Dec 7 '16 at 14:01
I have a similar issue see my answer, comment does not allow that many chars
– Tom Deloford
Jan 28 '17 at 15:04
add a comment |
We have a C# T4 file named GenerateProxies.tt which calls several command-line codegen utilities. Using the System.Diagnostics Process class, we redirect the standard output to the T4 output text file (GenerateProxies.txt) so that we can review the command-line output for errors.
I added the following simple code to the end of the T4 so that Visual Studio will open the generated text file as the last step in the process (the workingDirectory
variable is declared and populated earlier in the template). This does work but it throws a serialization error. Can this error be avoided?
<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>
<#
IServiceProvider vssp = (IServiceProvider)this.Host;
DTE dte = vssp.GetService(typeof(DTE)) as DTE;
dte.ItemOperations.OpenFile(
string.Format(@"{0}GenerateProxies.txt", workingDirectory),
Constants.vsViewKindTextView
);
#>
Again, this does work, it opens the text file, but it generates this error:
Running transformation: System.Runtime.Serialization.SerializationException:
Type 'Microsoft.VisualStudio.Platform.WindowManagement.DTE.WindowBase' in
Assembly 'Microsoft.VisualStudio.Platform.WindowManagement'
is not marked as serializable.
c# visual-studio-2015 t4 envdte
We have a C# T4 file named GenerateProxies.tt which calls several command-line codegen utilities. Using the System.Diagnostics Process class, we redirect the standard output to the T4 output text file (GenerateProxies.txt) so that we can review the command-line output for errors.
I added the following simple code to the end of the T4 so that Visual Studio will open the generated text file as the last step in the process (the workingDirectory
variable is declared and populated earlier in the template). This does work but it throws a serialization error. Can this error be avoided?
<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>
<#
IServiceProvider vssp = (IServiceProvider)this.Host;
DTE dte = vssp.GetService(typeof(DTE)) as DTE;
dte.ItemOperations.OpenFile(
string.Format(@"{0}GenerateProxies.txt", workingDirectory),
Constants.vsViewKindTextView
);
#>
Again, this does work, it opens the text file, but it generates this error:
Running transformation: System.Runtime.Serialization.SerializationException:
Type 'Microsoft.VisualStudio.Platform.WindowManagement.DTE.WindowBase' in
Assembly 'Microsoft.VisualStudio.Platform.WindowManagement'
is not marked as serializable.
c# visual-studio-2015 t4 envdte
c# visual-studio-2015 t4 envdte
asked Dec 6 '16 at 16:44
McGuireV10McGuireV10
5,58243040
5,58243040
The call stack might help.
– Will
Dec 6 '16 at 19:38
Yeah, but unfortunately it's from a development VM that has no external access at all (which is why I didn't bother to retype all the assembly info, the key GUID and so on). The call stack is huge but it looks like some kind of PInvoke marshalling problem. Apparently DTE is COM. I sort of vaguely suspect it's a threading problem.
– McGuireV10
Dec 7 '16 at 12:15
Smells to me like something is being accidentally pulled across an AppDomain border. The call stack could identify the source, and you could investigate, at the bottom of your code in the stack, who has a reference to an instance of that type.
– Will
Dec 7 '16 at 14:01
I have a similar issue see my answer, comment does not allow that many chars
– Tom Deloford
Jan 28 '17 at 15:04
add a comment |
The call stack might help.
– Will
Dec 6 '16 at 19:38
Yeah, but unfortunately it's from a development VM that has no external access at all (which is why I didn't bother to retype all the assembly info, the key GUID and so on). The call stack is huge but it looks like some kind of PInvoke marshalling problem. Apparently DTE is COM. I sort of vaguely suspect it's a threading problem.
– McGuireV10
Dec 7 '16 at 12:15
Smells to me like something is being accidentally pulled across an AppDomain border. The call stack could identify the source, and you could investigate, at the bottom of your code in the stack, who has a reference to an instance of that type.
– Will
Dec 7 '16 at 14:01
I have a similar issue see my answer, comment does not allow that many chars
– Tom Deloford
Jan 28 '17 at 15:04
The call stack might help.
– Will
Dec 6 '16 at 19:38
The call stack might help.
– Will
Dec 6 '16 at 19:38
Yeah, but unfortunately it's from a development VM that has no external access at all (which is why I didn't bother to retype all the assembly info, the key GUID and so on). The call stack is huge but it looks like some kind of PInvoke marshalling problem. Apparently DTE is COM. I sort of vaguely suspect it's a threading problem.
– McGuireV10
Dec 7 '16 at 12:15
Yeah, but unfortunately it's from a development VM that has no external access at all (which is why I didn't bother to retype all the assembly info, the key GUID and so on). The call stack is huge but it looks like some kind of PInvoke marshalling problem. Apparently DTE is COM. I sort of vaguely suspect it's a threading problem.
– McGuireV10
Dec 7 '16 at 12:15
Smells to me like something is being accidentally pulled across an AppDomain border. The call stack could identify the source, and you could investigate, at the bottom of your code in the stack, who has a reference to an instance of that type.
– Will
Dec 7 '16 at 14:01
Smells to me like something is being accidentally pulled across an AppDomain border. The call stack could identify the source, and you could investigate, at the bottom of your code in the stack, who has a reference to an instance of that type.
– Will
Dec 7 '16 at 14:01
I have a similar issue see my answer, comment does not allow that many chars
– Tom Deloford
Jan 28 '17 at 15:04
I have a similar issue see my answer, comment does not allow that many chars
– Tom Deloford
Jan 28 '17 at 15:04
add a comment |
2 Answers
2
active
oldest
votes
The EnvDTE assemblies are COM interop assemblies. Your error can be avoided by creating a Runtime Callable Wrapper, which marshals calls to the COM object based off information in the interop-assembly. Microsoft has provided an extension method within the
Microsoft.VisualStudio.TextTemplating
namespace:
<#@ template hostspecific="true" language="C#" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#
IServiceProvider serviceProvider = (IServiceProvider)this.Host;
EnvDTE.DTE dte = (EnvDTE.DTE) serviceProvider.GetCOMService(typeof(EnvDTE.DTE));
#>
The GetCOMService(this IServiceProvider, Type type)
extension method is the correct way to obtain the DTE COM object.
T4 templates run in a separate AppDomain, and I believe that is the reason your code is working despite the exception. IServiceProvider.GetService(typeof(DTE))
returns a transparent Proxy Object. This exception is because the proxy requires objects crossing an app domain be decorated with the Serializable
attribute. You can confirm the DTE object in your code is a "transparent proxy" like this:
bool isProxy = RemotingServices.IsTransparentProxy(dte);
1
Worked for me. up vote
– Roberto
Dec 6 '18 at 22:31
1
Started getting the errorRunning transformation: System.Runtime.Serialization.SerializationException: Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProjectItem' in Assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable
in Visual Studio 2019 for a T4 template that worked just fine in VS 2017. Updating it to use the GetCOMService extension method as mentioned here fixed it!
– Duncan Smart
Mar 12 at 22:53
add a comment |
This is not an answer however the OP could not provide the stack trace as requested in the comments.
I have a similar exception being thrown when I try to execute a function in my tt file to write to the Output window (ST is too long for comment)
private void WriteToOutput(string output)
{
if (_host == null)
throw new Exception("Host property returned unexpected value (null)");
EnvDTE.DTE dte = (EnvDTE.DTE)_host.GetService(typeof(EnvDTE.DTE));
if (dte == null)
throw new Exception("Unable to retrieve DTE");
Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
window.Activate();
var outputWindow = (EnvDTE.OutputWindow) window.Object;
outputWindow.ActivePane.Activate();
outputWindow.ActivePane.OutputString(output);
outputWindow.ActivePane.OutputString("n");
}
Severity Code Description Project File Line Suppression State
Error Running transformation:
System.Runtime.Serialization.SerializationException: Type
'Microsoft.VisualStudio.Platform.WindowManagement.DTE.Windows' in
Assembly 'Microsoft.VisualStudio.Platform.WindowManagement,
Version=15.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.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._DTE.get_Windows() at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TypeMapper.WriteToOutput(String
output) in C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line 581
at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TypeMapper.GetItemsToGenerate[T](IEnumerable`1
itemCollection) in
C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line 566 at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TransformText()
in C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line
33 C:ViewModelsBaseGridViewModelsBaseViewModels.tt 581
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41000524%2fvisual-studio-serialization-error-when-t4-uses-dte-to-open-generated-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The EnvDTE assemblies are COM interop assemblies. Your error can be avoided by creating a Runtime Callable Wrapper, which marshals calls to the COM object based off information in the interop-assembly. Microsoft has provided an extension method within the
Microsoft.VisualStudio.TextTemplating
namespace:
<#@ template hostspecific="true" language="C#" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#
IServiceProvider serviceProvider = (IServiceProvider)this.Host;
EnvDTE.DTE dte = (EnvDTE.DTE) serviceProvider.GetCOMService(typeof(EnvDTE.DTE));
#>
The GetCOMService(this IServiceProvider, Type type)
extension method is the correct way to obtain the DTE COM object.
T4 templates run in a separate AppDomain, and I believe that is the reason your code is working despite the exception. IServiceProvider.GetService(typeof(DTE))
returns a transparent Proxy Object. This exception is because the proxy requires objects crossing an app domain be decorated with the Serializable
attribute. You can confirm the DTE object in your code is a "transparent proxy" like this:
bool isProxy = RemotingServices.IsTransparentProxy(dte);
1
Worked for me. up vote
– Roberto
Dec 6 '18 at 22:31
1
Started getting the errorRunning transformation: System.Runtime.Serialization.SerializationException: Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProjectItem' in Assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable
in Visual Studio 2019 for a T4 template that worked just fine in VS 2017. Updating it to use the GetCOMService extension method as mentioned here fixed it!
– Duncan Smart
Mar 12 at 22:53
add a comment |
The EnvDTE assemblies are COM interop assemblies. Your error can be avoided by creating a Runtime Callable Wrapper, which marshals calls to the COM object based off information in the interop-assembly. Microsoft has provided an extension method within the
Microsoft.VisualStudio.TextTemplating
namespace:
<#@ template hostspecific="true" language="C#" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#
IServiceProvider serviceProvider = (IServiceProvider)this.Host;
EnvDTE.DTE dte = (EnvDTE.DTE) serviceProvider.GetCOMService(typeof(EnvDTE.DTE));
#>
The GetCOMService(this IServiceProvider, Type type)
extension method is the correct way to obtain the DTE COM object.
T4 templates run in a separate AppDomain, and I believe that is the reason your code is working despite the exception. IServiceProvider.GetService(typeof(DTE))
returns a transparent Proxy Object. This exception is because the proxy requires objects crossing an app domain be decorated with the Serializable
attribute. You can confirm the DTE object in your code is a "transparent proxy" like this:
bool isProxy = RemotingServices.IsTransparentProxy(dte);
1
Worked for me. up vote
– Roberto
Dec 6 '18 at 22:31
1
Started getting the errorRunning transformation: System.Runtime.Serialization.SerializationException: Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProjectItem' in Assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable
in Visual Studio 2019 for a T4 template that worked just fine in VS 2017. Updating it to use the GetCOMService extension method as mentioned here fixed it!
– Duncan Smart
Mar 12 at 22:53
add a comment |
The EnvDTE assemblies are COM interop assemblies. Your error can be avoided by creating a Runtime Callable Wrapper, which marshals calls to the COM object based off information in the interop-assembly. Microsoft has provided an extension method within the
Microsoft.VisualStudio.TextTemplating
namespace:
<#@ template hostspecific="true" language="C#" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#
IServiceProvider serviceProvider = (IServiceProvider)this.Host;
EnvDTE.DTE dte = (EnvDTE.DTE) serviceProvider.GetCOMService(typeof(EnvDTE.DTE));
#>
The GetCOMService(this IServiceProvider, Type type)
extension method is the correct way to obtain the DTE COM object.
T4 templates run in a separate AppDomain, and I believe that is the reason your code is working despite the exception. IServiceProvider.GetService(typeof(DTE))
returns a transparent Proxy Object. This exception is because the proxy requires objects crossing an app domain be decorated with the Serializable
attribute. You can confirm the DTE object in your code is a "transparent proxy" like this:
bool isProxy = RemotingServices.IsTransparentProxy(dte);
The EnvDTE assemblies are COM interop assemblies. Your error can be avoided by creating a Runtime Callable Wrapper, which marshals calls to the COM object based off information in the interop-assembly. Microsoft has provided an extension method within the
Microsoft.VisualStudio.TextTemplating
namespace:
<#@ template hostspecific="true" language="C#" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#
IServiceProvider serviceProvider = (IServiceProvider)this.Host;
EnvDTE.DTE dte = (EnvDTE.DTE) serviceProvider.GetCOMService(typeof(EnvDTE.DTE));
#>
The GetCOMService(this IServiceProvider, Type type)
extension method is the correct way to obtain the DTE COM object.
T4 templates run in a separate AppDomain, and I believe that is the reason your code is working despite the exception. IServiceProvider.GetService(typeof(DTE))
returns a transparent Proxy Object. This exception is because the proxy requires objects crossing an app domain be decorated with the Serializable
attribute. You can confirm the DTE object in your code is a "transparent proxy" like this:
bool isProxy = RemotingServices.IsTransparentProxy(dte);
edited Mar 22 at 3:07
answered Nov 16 '18 at 23:39
FakeSaintFakeSaint
756
756
1
Worked for me. up vote
– Roberto
Dec 6 '18 at 22:31
1
Started getting the errorRunning transformation: System.Runtime.Serialization.SerializationException: Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProjectItem' in Assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable
in Visual Studio 2019 for a T4 template that worked just fine in VS 2017. Updating it to use the GetCOMService extension method as mentioned here fixed it!
– Duncan Smart
Mar 12 at 22:53
add a comment |
1
Worked for me. up vote
– Roberto
Dec 6 '18 at 22:31
1
Started getting the errorRunning transformation: System.Runtime.Serialization.SerializationException: Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProjectItem' in Assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable
in Visual Studio 2019 for a T4 template that worked just fine in VS 2017. Updating it to use the GetCOMService extension method as mentioned here fixed it!
– Duncan Smart
Mar 12 at 22:53
1
1
Worked for me. up vote
– Roberto
Dec 6 '18 at 22:31
Worked for me. up vote
– Roberto
Dec 6 '18 at 22:31
1
1
Started getting the error
Running transformation: System.Runtime.Serialization.SerializationException: Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProjectItem' in Assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable
in Visual Studio 2019 for a T4 template that worked just fine in VS 2017. Updating it to use the GetCOMService extension method as mentioned here fixed it!– Duncan Smart
Mar 12 at 22:53
Started getting the error
Running transformation: System.Runtime.Serialization.SerializationException: Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProjectItem' in Assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable
in Visual Studio 2019 for a T4 template that worked just fine in VS 2017. Updating it to use the GetCOMService extension method as mentioned here fixed it!– Duncan Smart
Mar 12 at 22:53
add a comment |
This is not an answer however the OP could not provide the stack trace as requested in the comments.
I have a similar exception being thrown when I try to execute a function in my tt file to write to the Output window (ST is too long for comment)
private void WriteToOutput(string output)
{
if (_host == null)
throw new Exception("Host property returned unexpected value (null)");
EnvDTE.DTE dte = (EnvDTE.DTE)_host.GetService(typeof(EnvDTE.DTE));
if (dte == null)
throw new Exception("Unable to retrieve DTE");
Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
window.Activate();
var outputWindow = (EnvDTE.OutputWindow) window.Object;
outputWindow.ActivePane.Activate();
outputWindow.ActivePane.OutputString(output);
outputWindow.ActivePane.OutputString("n");
}
Severity Code Description Project File Line Suppression State
Error Running transformation:
System.Runtime.Serialization.SerializationException: Type
'Microsoft.VisualStudio.Platform.WindowManagement.DTE.Windows' in
Assembly 'Microsoft.VisualStudio.Platform.WindowManagement,
Version=15.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.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._DTE.get_Windows() at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TypeMapper.WriteToOutput(String
output) in C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line 581
at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TypeMapper.GetItemsToGenerate[T](IEnumerable`1
itemCollection) in
C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line 566 at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TransformText()
in C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line
33 C:ViewModelsBaseGridViewModelsBaseViewModels.tt 581
add a comment |
This is not an answer however the OP could not provide the stack trace as requested in the comments.
I have a similar exception being thrown when I try to execute a function in my tt file to write to the Output window (ST is too long for comment)
private void WriteToOutput(string output)
{
if (_host == null)
throw new Exception("Host property returned unexpected value (null)");
EnvDTE.DTE dte = (EnvDTE.DTE)_host.GetService(typeof(EnvDTE.DTE));
if (dte == null)
throw new Exception("Unable to retrieve DTE");
Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
window.Activate();
var outputWindow = (EnvDTE.OutputWindow) window.Object;
outputWindow.ActivePane.Activate();
outputWindow.ActivePane.OutputString(output);
outputWindow.ActivePane.OutputString("n");
}
Severity Code Description Project File Line Suppression State
Error Running transformation:
System.Runtime.Serialization.SerializationException: Type
'Microsoft.VisualStudio.Platform.WindowManagement.DTE.Windows' in
Assembly 'Microsoft.VisualStudio.Platform.WindowManagement,
Version=15.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.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._DTE.get_Windows() at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TypeMapper.WriteToOutput(String
output) in C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line 581
at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TypeMapper.GetItemsToGenerate[T](IEnumerable`1
itemCollection) in
C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line 566 at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TransformText()
in C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line
33 C:ViewModelsBaseGridViewModelsBaseViewModels.tt 581
add a comment |
This is not an answer however the OP could not provide the stack trace as requested in the comments.
I have a similar exception being thrown when I try to execute a function in my tt file to write to the Output window (ST is too long for comment)
private void WriteToOutput(string output)
{
if (_host == null)
throw new Exception("Host property returned unexpected value (null)");
EnvDTE.DTE dte = (EnvDTE.DTE)_host.GetService(typeof(EnvDTE.DTE));
if (dte == null)
throw new Exception("Unable to retrieve DTE");
Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
window.Activate();
var outputWindow = (EnvDTE.OutputWindow) window.Object;
outputWindow.ActivePane.Activate();
outputWindow.ActivePane.OutputString(output);
outputWindow.ActivePane.OutputString("n");
}
Severity Code Description Project File Line Suppression State
Error Running transformation:
System.Runtime.Serialization.SerializationException: Type
'Microsoft.VisualStudio.Platform.WindowManagement.DTE.Windows' in
Assembly 'Microsoft.VisualStudio.Platform.WindowManagement,
Version=15.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.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._DTE.get_Windows() at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TypeMapper.WriteToOutput(String
output) in C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line 581
at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TypeMapper.GetItemsToGenerate[T](IEnumerable`1
itemCollection) in
C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line 566 at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TransformText()
in C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line
33 C:ViewModelsBaseGridViewModelsBaseViewModels.tt 581
This is not an answer however the OP could not provide the stack trace as requested in the comments.
I have a similar exception being thrown when I try to execute a function in my tt file to write to the Output window (ST is too long for comment)
private void WriteToOutput(string output)
{
if (_host == null)
throw new Exception("Host property returned unexpected value (null)");
EnvDTE.DTE dte = (EnvDTE.DTE)_host.GetService(typeof(EnvDTE.DTE));
if (dte == null)
throw new Exception("Unable to retrieve DTE");
Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
window.Activate();
var outputWindow = (EnvDTE.OutputWindow) window.Object;
outputWindow.ActivePane.Activate();
outputWindow.ActivePane.OutputString(output);
outputWindow.ActivePane.OutputString("n");
}
Severity Code Description Project File Line Suppression State
Error Running transformation:
System.Runtime.Serialization.SerializationException: Type
'Microsoft.VisualStudio.Platform.WindowManagement.DTE.Windows' in
Assembly 'Microsoft.VisualStudio.Platform.WindowManagement,
Version=15.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.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._DTE.get_Windows() at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TypeMapper.WriteToOutput(String
output) in C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line 581
at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TypeMapper.GetItemsToGenerate[T](IEnumerable`1
itemCollection) in
C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line 566 at
Microsoft.VisualStudio.TextTemplating8CBB5A87F4A34D52835396F51C533E1D8E9F22BC6977A9510B46C012D01E08C8AD263AC5BA030600D92BC0F39E7F1C3B6AA67D8CE545627E10A7F993E06C0D02.GeneratedTextTransformation.TransformText()
in C:ViewModelsBaseGridViewModelsBaseViewModels.tt:line
33 C:ViewModelsBaseGridViewModelsBaseViewModels.tt 581
answered Jan 28 '17 at 15:10
Tom DelofordTom Deloford
1,10611423
1,10611423
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41000524%2fvisual-studio-serialization-error-when-t4-uses-dte-to-open-generated-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The call stack might help.
– Will
Dec 6 '16 at 19:38
Yeah, but unfortunately it's from a development VM that has no external access at all (which is why I didn't bother to retype all the assembly info, the key GUID and so on). The call stack is huge but it looks like some kind of PInvoke marshalling problem. Apparently DTE is COM. I sort of vaguely suspect it's a threading problem.
– McGuireV10
Dec 7 '16 at 12:15
Smells to me like something is being accidentally pulled across an AppDomain border. The call stack could identify the source, and you could investigate, at the bottom of your code in the stack, who has a reference to an instance of that type.
– Will
Dec 7 '16 at 14:01
I have a similar issue see my answer, comment does not allow that many chars
– Tom Deloford
Jan 28 '17 at 15:04