I am working on a C# project where I should deploy few .dacpac files so I have used the Microsoft.SqlServer.DacFx library (github from DacFx here).
I have used also in another part the SqlPackage.exe executable to deploy another .dacpac files (no relation between them), so I'm using a publish profile (.xml / .pubxml file) to configure the deployment options. The cmd command looks like this:
SqlPackage /Profile:"Database.publish.xml"
I would need to deserialize this .xml file into a DacDeployOptions class in order to use the same pulish profile when publishing the dacpacs, but as much as I have been able to search I can't find a way to do it. I would appreciate some help with this.
Thanks 😃
After a long search I found the solution. Here the needed Code:
DacProfile profile = DacProfile.Load(#"C:\temp\publish.xml");
PublishOptions options = new PublishOptions();
options.GenerateDeploymentReport = true;
options.GenerateDeploymentScript = true;
options.DeployOptions = profile.DeployOptions;
Related
I am trying to create a custom MSBuild script in C#, using the newer Microsoft.Build.Evaluation API. The problem I have is that this newer API does not support .sln files. The older deprecated Microsoft.Build.Engine API does support .sln files, but I'd like to use the newer one because 1) it's not deprecated and 2) there seems to be more online documentation and usage to reference. I've seen that MSBuild can create a .metaproj file when is successfully compiles a solution, when this assignment is made in CMD: set MSBuildEmitSolution=1. I need the .metaproj file to be able to compile the solution in the first place. Is there anything in the API for converting .sln to .metaproj? Is there any library out there for parsing .sln files?
I figured it out after more searching. Finding good examples online is a little difficult because of the two different versions of the MSBuild API, and the popularity of just running MSBuild from the command line.
Here is the code that is now working for me, using the newer MSBuild API:
var pc = new ProjectCollection();
var parameters = new BuildParameters(pc)
{
Loggers = new[] { _logger } //Instance of ILogger instantiated earlier
};
var request = new BuildRequestData(
projectFullPath: pathToMySlnFile, //Solution file path
globalProperties: myPropertyDictionary,
toolsVersion: null,
targetsToBuild: myTargetsArray,
hostServices: null,
flags: BuildRequestDataFlags.ProvideProjectStateAfterBuild);
var buildResult = BuildManager.DefaultBuildManager.Build(parameters, request);
I am trying to analyse a solution with Roslyn, with MSBuildWorkspace.
The solution is a new solution, with 2 class library projects in them, one referencing the other.
They are created in Visual Studio 2017, .Net 4.6.2.
When I open the solution, I receive two generic errors in workspace.Diagnostics, both are :
Msbuild failed when processing the file 'PathToProject'
There is nothing more in the diagnostics or output window, to indicate WHY it failed to process the project file.
The code for opening the solution:
namespace RoslynAnalyse
{
class Program
{
static void Main(string[] args)
{
LocalAnalysis();
}
private static void LocalAnalysis()
{
var workspace = MSBuildWorkspace.Create();
var solution = workspace.OpenSolutionAsync(#"D:\Code\Roslyn\RoslynAnalyse\SolutionToAnalyse\SolutionToAnalyse.sln").Result;
var workspaceDiagnostics = workspace.Diagnostics;
}
}
}
The version of Microsoft.CodeAnalysis is 2.0.0.0.
Does anybody have any idea why MSBuild failed, how I can get more information ?
When MSBuildWorkspace fails to open a project or solution this way, it is almost always because the application using MSBuildWorkspace does not include the same binding redirects that msbuild.exe.config has in it.
MSBuild uses binding redirects to allow tasks (typically already compiled C# code using possibly different versions of msbuild API libraries) to all use the current msbuild API's. Otherwise, msbuild gets runtime load failures.
The solution is to add an app.config file to your project and copy the binding redirects (the assemblyBinding section of the msbuild.exe.config file) into your file.
I want to use "EntityFramework Reverse POCO Code First Generator" but programmatically not from VS.
EntityFramework Reverse POCO Code Github
In fact I want to Run T4 for this purpose from C# code
I downloaded simple-t4-engine for this purpose
Simple T4 Engine
I wrote some code like this :
Engine engine = new Engine();
TemplatingHost host = new TemplatingHost(System.Text.Encoding.UTF32);
host.IncludeFileSearchPaths.Add(#"D:\IncludeFiles");
string templateFileName = "some template";
// NOTE: Doesn't actually seem to care about the name of the template file? True, but does use the path to search for stuff.
host.TemplateFile = templateFileName;
string input = File.ReadAllText(#"D:\IncludeFiles\T4Files\Database.tt");
string output = engine.ProcessTemplate(input, host);
File.WriteAllText(#"D:\IncludeFiles\T4Files\Output.txt", output);
StringBuilder sb = new StringBuilder();
foreach (CompilerError error in host.Errors)
{
sb.AppendLine(error.ToString());
}
File.WriteAllText(#"D:\IncludeFiles\T4Files\ErrorLog.txt", sb.ToString());
But I got some errors (Output.txt is Empty)
ErrorLog.txt :
error : Running transformation: System.InvalidCastException: Unable to cast transparent proxy to type 'System.IServiceProvider'.
at Microsoft.VisualStudio.TextTemplating31A64EBEAB614B57E81A1789EC7637709A091834D5CA991E8A2195B15E2A0DFF588B0C98DCEDA8AD6902329A28B09556BDE2A9BEDFA48812CCC12CA1E68AA1C9.GeneratedTextTransformation.GetDTE()
at Microsoft.VisualStudio.TextTemplating31A64EBEAB614B57E81A1789EC7637709A091834D5CA991E8A2195B15E2A0DFF588B0C98DCEDA8AD6902329A28B09556BDE2A9BEDFA48812CCC12CA1E68AA1C9.GeneratedTextTransformation.GetCurrentProject()
at Microsoft.VisualStudio.TextTemplating31A64EBEAB614B57E81A1789EC7637709A091834D5CA991E8A2195B15E2A0DFF588B0C98DCEDA8AD6902329A28B09556BDE2A9BEDFA48812CCC12CA1E68AA1C9.GeneratedTextTransformation.GetConfigPaths()
at Microsoft.VisualStudio.TextTemplating31A64EBEAB614B57E81A1789EC7637709A091834D5CA991E8A2195B15E2A0DFF588B0C98DCEDA8AD6902329A28B09556BDE2A9BEDFA48812CCC12CA1E68AA1C9.GeneratedTextTransformation.GetConnectionString(String& connectionStringName, String& providerName, String& configFilePath)
at Microsoft.VisualStudio.TextTemplating31A64EBEAB614B57E81A1789EC7637709A091834D5CA991E8A2195B15E2A0DFF588B0C98DCEDA8AD6902329A28B09556BDE2A9BEDFA48812CCC12CA1E68AA1C9.GeneratedTextTransformation.InitConnectionString()
at Microsoft.VisualStudio.TextTemplating31A64EBEAB614B57E81A1789EC7637709A091834D5CA991E8A2195B15E2A0DFF588B0C98DCEDA8AD6902329A28B09556BDE2A9BEDFA48812CCC12CA1E68AA1C9.GeneratedTextTransformation.GetDbProviderFactory()
at Microsoft.VisualStudio.TextTemplating31A64EBEAB614B57E81A1789EC7637709A091834D5CA991E8A2195B15E2A0DFF588B0C98DCEDA8AD6902329A28B09556BDE2A9BEDFA48812CCC12CA1E68AA1C9.GeneratedTextTransformation.TransformText()
Can anyone help me for solve this problem ?
or introduce a better way for run Database.tt in runtime an execute in C# programmatically.
Database.tt
EF.Reverse.POCO.Core.ttinclude
EF.Reverse.POCO.ttinclude
EF.Utility.CS.ttinclude
EF6.Utility.CS.ttinclude
GenerateTSQL.Utility.ttinclude
I am the author of the Entity Framework Reverse POCO Generator.
Unfortunately, you can't run this outside of Visual Studio because the code depends on it. The EnvDTE provides the ability for the reverse generator to add/remove generated files from the Visual Studio project. Without this, it will not be able to do it's job.
Others have asked if they can run it on the build server. However the build server may not have access to the database it needs to reverse engineer. So always generate the code by saving the tt settings file, and commit the generated code to source control, and from there on to your build server.
By the way, just to be clear: I don't want this project to be automated outside of Visual Studio, or to be included in another product of any kind. This is stipulated in the license.
You can use TextTransform Utility to transform T4 files outside Visual Studio. Normally you can find util in folder:
\Program Files\Common Files\Microsoft Shared\TextTemplating\
I think this is a start for your problem please research more about this tool and check if fit your issue.
I have a C# Web API Elastic Beanstalk app which needs a folder outside the deployment directory that the IUSER and IIS_USERS users can write to. I've created a .config file and put this in the top level .ebextensions folder in my project. The contents are below:
commands:
0_mkdir:
command: mkdir C:\\AppFolder\\
1_set_iuser_permissions:
command: cacls C:\\AppFolder\\ /t /e /g IUser:f IIS_Users:f
However while the folder is created successfully the permissions aren't set. If anyone has any idea what I am doing wrong I would be hugely grateful. Big thanks in advance.
In the end I switched to using Json instead of YAML as, despite my YAML being validated by several online YAML testers, AWS still wouldn't accept it. It always had issues with the parameters passed to icacls. I also changed to a folder within the application App_Data folder as setting permissions on any directory external to the application didn't appear to work. So, my final configuration file is as follows:
{
"container_commands": {
"01": {
"command": "icacls \"C:/inetpub/wwwroot/AppName_deploy/App_Data/AppFolder\" /grant DefaultAppPool:(OI)(CI)F"
}
}
}
Hope this helps someone else out.
It looks like you are using invalid .net accounts (unless these are custom accounts you created). That is part of the reason why your permissions are not being set. They should be IUSR or IIS_IUSRS
Furthermore, container_commands executes after your app/server environment has been setup, but before your deployment has started. There is no other way to set permissions on files/folders within your deployment directory other than using a wpp.targets file within visual studio.
The following SO post is a good read using wpp.targets to solve your issue.
Can Web Deploy's setAcl provider be used on a sub-directory?
Place a file 01_fix_permissions.config inside .ebextensions folder.
contents:
files:
"/opt/elasticbeanstalk/hooks/appdeploy/pre/49_change_permissions.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
sudo chown -R ec2-user:ec2-user tmp/
From C# code I'm trying to retrieve all the namespaces from powershell... (Later more complex things, like creating namespaces)
PowerShell ps = PowerShell.Create();
ps.AddCommand("Import-Module").AddArgument("ServiceBus").Invoke();
var result = ps.AddCommand("Get-SBNamespace").Invoke();
Above code gives the following exception:
The 'Get-SBNamespace' command was found in the module 'ServiceBus',
but the module could not be loaded. For more information, run
'Import-Module ServiceBus'.
Does anyone know how to solve this error?
CURRENT STATUS: after some debugging I've found that no modules are loaded by default in the PowerShell object. Using the code:
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[]{#"serviceBus"});
PowerShell ps = PowerShell.Create(iss);
doesn't work to load the service bus module. Also the code:
ps.AddCommand("Import-Module").AddParameter("-Name", "serviceBus").Invoke();
doesn't work to import the service bus module. Running Visual Studio in administrator mode also doesn't make a difference
Thanks in advance
You didn't say which version of Visual Studio you're using. If it's VS 2012, when you tried the x64 platform target did you make sure that "Prefer 32-bit" was not checked? Even if it was not checked try checking it, saving the project configuration, clearing it and saving again - this worked for me on another project.
UPDATE
It's been suggested elsewhere that there's a bug in VS2012 that shows "Prefer 32-bit" as greyed-out and unchecked when it's actually active. I'm running Update 2 and I don't see that. But it sounds like you might be. I suggest you edit the .csproj file directly.
Whilst "Platform Target" is set at "Any CPU", in Solution Explorer, right-click on the Project name (or, with go to the PROJECT menu) and select "Unload Project". Project files will close and Solution Explorer will display project name (unavailable) > The project file was unloaded:
Right-click on the Project name again and select "Edit project name.csproj". The file is XML and mostly comprises PropertyGroup and ItemGroup elements. In a console project, the first PropertyGroup usually contains a Platform element which should read AnyCPU if you followed my instructions above. The next two PropertyGroups are normally for Debug and Release configurations. If you've added another configuration, it will have its own PropertyGroup. In each of these, look for an element which reads:
<Prefer32Bit>true</Prefer32Bit>
What you should have is an element which reads:
<Prefer32Bit>false</Prefer32Bit>
Either change it or insert it (in each configuration ProjectGroup), save the file and close it. Back in Solution Explorer, right-click the project and select "Reload Project". Let me know if that solves it. You can confirm your PowerShell is now running 64-bit by get the result of
[System.IntPtr]::Size
e.g.
ps.AddScript("[System.IntPtr]::Size");
which will be 4 in an x86 process and 8 in an x64 process.
Which my project set up like this, I was able to load ServiceBus using:
ps.AddCommand("Import-Module").AddArgument("ServiceBus");
Hopefully, you will, too.
I don't have ServiceBus installed so I can't verify exactly what you've tried but
ps.AddCommand("Import-Module").AddArgument("ActiveDirectory").Invoke();
worked for me, so your original syntax looks good.
Just to test for failure, I tried:
ps.AddCommand("Import-Module").AddArgument("CheeseDirectory");
ps.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
var importResult = ps.Invoke();
foreach (PSObject result in importResult)
{
Console.WriteLine(result);
}
and got
The specified module 'CheeseDirectory' was not loaded because no valid
module file was found in any module directory.
Have you tried similar?
Do you take care of your Assembly target in your C# program (x86 versus X64). The module may exist in one target, not in the other. PowerShell exists in both.
Seems you're trying to import some modules and execute the cmdlet or function inside the module, right?
So I think you could try the following code:
PowerShell ps = PowerShell.Create();
Assembly ass = Assembly.LoadFile(#"yourServiceBus.dll");
ps.AddCommand("Import-Module").AddParameter("Assembly", ass).Invoke();
var result = ps.AddCommand("Get-SBNamespace").Invoke();
Hope this could help.