Include referenced libraries as CLR assembly in Dacpac (afterwards)#915
Include referenced libraries as CLR assembly in Dacpac (afterwards)#915jvdslikke wants to merge 39 commits intorr-wfm:masterfrom
Conversation
|
@jvdslikke First of all, thanks for taking a stab at this. While we don't use SQLCLR ourselves I do realise there are many folks out there that do, so having a solution for this in the box would definitely be nice. But to be honest, looking at this it feels a bit hacky and I'm wondering if it is worth maintaining in its current form. We are making a lot of assumptions here about the inner workings of DacFx that could be changed at any time and then this would break. Now we are already making some assumptions by calling internal API's from DacFx through reflection and that got me thinking if there might actually be some internal API already that adds a SQLCLR assembly to the model. Apparently sqlpackage is capable of deploying one, so I wouldn't be surprised if DacFx has a method somewhere to add it to the model. I would much rather call an internal API that does the heavy-lifiting than trying to do the heavy-lifting ourselves. That might also mean we don't have to work around the warnings as well. Aside from that I do think we should treat these references as a separate thing, rather than trying to shoehorn them in the existing references and then checking whether it is a |
|
Hi @jmezach thanks for looking into my PR. I agree that the current solution is hacky. I tried adding the assembly with passing a Although the solution is hacky, it only impacts those actually using SQL CLR. Another option might be to rewrite the DacpacTool to .netstandard/.NET Framework so the build can be executed with .NET Framework. But I assume that is not something you want to consider 😊. I am willing to explore the option of calling an internal DacFX API. It there documentation somewhere about what internal methods DacFX offers which can be called through reflection?
I do not have much knowledge about MSBuild, but I will give it a try. |
|
So what you're saying is that even though you have a .NET Framework assembly and you put that into a If that is the case I'm wondering if we could add a new .NET Framework CLI tool that would add the assemblies to an existing |
Yes that is what I understood from microsoft/DacFx#523, and have now proven with a small test project (attached)😀. The command When you change the TargetFramework in DacpacWithAssemblyCreationTest.csproj to "net48" the command above succesfully produces a Dacpac with the assembly included.
That is a wonderful idea, better than hacking into the Dacpac ZIP or rewriting the whole DacpacTool to .NET Framework. Then still we need to ignore "unresolved reference to Assembly" in the Dacpactool because the assembly is added afterwards. I will update my PR to this idea. |
|
@jvdslikke What if we'd run the .NET Framework tool first to create a |
|
@jmezach I think it will give the same error when the DacpacTool opens and saves the |
|
I vibe-coded a solution with a "DacpacToolFramework" which first makes a Dacpac with the assemblies and then passes it to the DacpacTool. But this gives the "This assembly is corrupt or not valid" in the validation as expected because it validates with the .NET Core tool. I kept a backup in a branch (https://github.com/MontaServices/MSBuild.Sdk.SqlProj/tree/feature/assembly-ref-framework-before) and will work further on a solution to add the assembly afterwards to the Dacpac. |
|
I implemented (vibe-coded) the solution to add the assembly afterwards to the dacpac with the DacpacToolFramework. It also adds objects referencing the assembly afterwards. This produces a dacpac without errors. But I observed that the relationship from objects referencing the afterwards-added objects are lost in the Dacpac. See comment #915 (comment) for details. I will work further on finding a solution for this. Maybe we should also re-add all objects referencing objects referencing the assembly. |
Just out of curiosity, at what point does the |
|
@jmezach I can't produce a stack trace at the moment. The error throws first at This PR now has the solution to add the assembly and all objects that reference it afterwards with a DacpacToolFramework. It also includes a test and passes the files with a separate build target. Are you willing to accept this? |
|
@jvdslikke What if you pass SQL70557 to |
|
@jmezach I created a version to test your suggestion but it doesn't work. Error with stack trace: |
|
@jmezach I updated the PR with:
|
Hi, I was looking for a way to include another project with SQL CLR code as Assembly in the Dacpac. Because I didn't like to have it in Postdeployment scripts and also didn't want to keep an old SSDT project. I managed to get it working by adding support for DLL references.
I read all the information in microsoft/DacFx#523. To work around the "SQL70557: This assembly is corrupt or not valid" I add the assembly directly to the Dacpac ZIP afterwards and fix the references. Build errors "unresolved reference to Assembly" are ignored (because the assembly is added afterwards, it is missing during build).
The referenced project must have .NET Framework as TargetFramework. Because SQL Server only supports .NET Framework assemblies. (This is not validated during build.)
This draft PR is a working proof-of-concept. Are you willing to accept this functionality? Then I can make it into a full PR by improving the code, adding tests, etc.