3

There's an assembly I need to reference in my project, that needs to have the "Embed Interop Types" set to false in order to successfully compile. If I don't put this option, I get the compile time error:

A reference was created to embedded interop assembly 'Interop.MSTSCLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because of an indirect reference to that assembly created by assembly 'AxInterop.MSTSCLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Consider changing the 'Embed Interop Types' property on either assembly.

If I were to simply have a project, I would set the option to false and never worry about it, but now that I'm making a nuget package with it, I'm trying to find a way to set the option when the package is added.

My nuspec file currently has this (ommiting metadata):

<files>
  <file src="Interop.MSTSCLib.dll" target="lib\net40" />
  <file src="AxInterop.MSTSCLib.dll" target="lib\net40" />
</files>

I've looked at the nuget documentation but couldn't find how to do this. Is it even possible?

Philippe Paré
  • 4,279
  • 5
  • 36
  • 56

1 Answers1

6

You'll need an install.ps1 in the tools directory like

param($installPath, $toolsPath, $package, $project)

$project.Object.References | Where-Object { $_.Name -eq "Interop.MSTSCLib" } |  ForEach-Object { $_.EmbedInteropTypes = $false }
$project.Object.References | Where-Object { $_.Name -eq "AxInterop.MSTSCLib" } |  ForEach-Object { $_.EmbedInteropTypes = $false }

Based on the question Can NuGet distribute a COM dll? and it's answer. This difference is you don't need the call to regsvr32 as you're not trying to distribute and register a com dll.

mcdon
  • 4,931
  • 3
  • 38
  • 36
  • 1
    Amazing, although it seems the `install.ps1` is no longer supported. I instead used `init.ps1`, and put it in `tools` and it worked perfectly! – Philippe Paré Sep 22 '17 at 19:43
  • According to this [issue](https://github.com/NuGet/Home/issues/4942), the future of support for install.ps1 seems uncertain. If your project uses the PackageReference node, only init.ps1 will be supported. Support of the PackageReference node is currently [limited](https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files) to a few project types (.Net Core, .Net Standard, and UWP in Visual Studio 2017). – mcdon Sep 25 '17 at 15:39
  • 2
    The recommended way to do this is now to use Nuget's support for MSBuild targets files. See: [Nuget.Samples.Interop](https://github.com/NuGet/Samples/blob/master/NuGet.Samples.Interop/README.md) – CyberMonk Jun 20 '18 at 21:55