Wix Bootstrapper - Best way to update/install Visual FoxPro database - c#

As mentioned here wix-bootstrapper-update-ui-xaml-from-customaction I use a Bootstrapper to install two MSI-packages.
During the installation I want to install/update a Visual FoxPro database (consisting of free tables).
At the moment I achieve this by calling a Visual FoxPro-exe during the ApplyComplete-Event of the BootstrapperApplication. To establish a communication between the BootstrapperApplication and the Visual FoxPro-exe I use MSMQ :
private void OnApplyComplete(object sender, ApplyCompleteEventArgs e)
{
string updateFile = this.installationFolder + "\\updfile.exe";
if (!MessageQueue.Exists(NAMEDPVDATENBANKNACHRICHTENSCHLANGE))
{
this._msmq = MessageQueue.Create(UPDATEMSMQ);
}
else
{
this._msmq = new MessageQueue(UPDATEMSMQ);
}
this._msmq.SetPermissions("Everyone", MessageQueueAccessRights.FullControl);
this._msmq.Purge();
if (System.IO.File.Exists(updateFile))
{
this._dbUpdate = true;
ProcessStartInfo updateProcessInfo = new ProcessStartInfo();
updateProcessInfo.FileName = updateFile;
updateProcessInfo.Arguments = UPDATEMSMQ;
updateProcessInfo.UseShellExecute = true;
updateProcessInfo.WorkingDirectory = this.installationFolder;
updateProcessInfo.CreateNoWindow = true;
Process updateProcess = new Process();
updateProcess.StartInfo = updateProcessInfo;
updateProcess.EnableRaisingEvents = true;
updateProcess.Exited += new EventHandler(this.updateFinished);
updateProcess .Start();
while (this._dbUpdate)
{
Message msg = null;
try
{
nachricht = this._msmq.Receive(new TimeSpan(0, 0, 45));
}
catch (MessageQueueException msgEx)
{
if (nachrichtAusnahme.MessageQueueErrorCode != MessageQueueErrorCode.IOTimeout)
{
this.Engine.Log(LogLevel.Verbose, msgEx);
}
}
if (msg != null)
{
msg.Formatter = new ActiveXMessageFormatter();
this.Engine.Log(LogLevel.Verbose, "VfpUpdate - " + msg.Body.ToString());
}
}
}
this._msmq.Close();
MessageQueue.Delete(UPDATEMSMQ);
}
private void updateFinished(object sender, EventArgs e)
{
this._dbUpdate = false;
this.Engine.Log(LogLevel.Verbose, "Update finished");
}
This way it works like a charm unless there are errors during the the update of the Visual FoxPro database. It should be possible to roll-back the changes made during the installation. For me it would be no problem to create a backup of the Visual FoxPro-files and to restore the files if an error occurs. But how should I do this with the files changed by the actual Bootstrapper?
With a CustomAction I can use ActionResult.Failure or ActionResult.Success. But with a CustomAction I face the following issues:
no access to Application.Current (for reading values from a customized ResourceDictionary with localized strings that I use within the Bootstrapper)
MSMQ-queue is broken (closed?!) after the first message is delivered
display the currently performed task in the MainWindow.
Any advice on how to perform the update of the Visual FoxPro database inside a BootstrapperApplication is really welcome.

I don't know that in the context of an installation bootstrapper. OTOH, either a VFP database and tables or VFP tables (free) are just plain files that are copied/moved from a folder. You don't need to have an exe for that to create them programmatically.
Also having those files as part of the setup might not be a good idea as well. If some files are removed because of version changes then your installer might start to bark about that.
Looking at the bootstrapper code, I have a suspicion that you are creating those free tables in the installation folder or its subfolders which would be a real PITA. Because installation folder is generally under "Program Files (x86)" which is a read only location.
Your bootstrapper code is C#. You might use C# to create those tables as well. One way to do that is to use VFPOLEDB and ExecScript() calls.

Related

CefSharp crashing in SharePoint Timer Job

I am attempting to schedule a CefSharp rendering of pages stored in SharePoint via a SharePoint Timer Job. However, the OWSTIMER.exe service crashes almost immediately after calling Cef.Initialize(), throwing next to no errors.
The Subscription Class is:
internal class Subscriptions: IDisposable
{
internal Subscriptions()
{
db = // CreateConnection();
if (Cef.IsInitialized == false)
{
var settings = new CefSettings
{
CachePath = "cache",
BrowserSubprocessPath = "ProjectBin\\CefSharp.BrowserSubprocess.exe",
LogSeverity = LogSeverity.Default,
};
Cef.Initialize(settings);
}
}
internal void RunTodaysSubscriptions()
{
var subs = db.tt_ScheduledJobs.Where(sj => sj.Date <= DateTime.Today).Select(sj => sj.tt_Subscription).ToList();
foreach (var sub in subs)
{
ExecuteSubscription(sub);
SetNextSchedule(sub);
}
}
private void ExecuteSubscription(tt_Subscription subscription)
{
tt_JobHistory historyEntry = new tt_JobHistory();
historyEntry.SubscriptionKey = subscription.SubscriptionKey;
historyEntry.StartDate = DateTime.Now;
historyEntry.Status = "In Progress";
dfe.tt_JobHistory.Add(historyEntry);
dfe.SaveChanges();
string url = //path to file
using (SubscriptionExecution se = new SubscriptionExecution(
url,
subscription.SubscriptionKey,
subscription.Format))
{
//Completed represents either a response from the dashboard that the export either succeded
//or failed. If there is no response (i.e. a javascript error preventing the dashboard from executing
//an export, then we would hit the timeout of 5 minutes and shutdown this process.
if (SpinWait.SpinUntil(() => se.Completed == true, 120000) == true)
{
//Had a response from dashboard, could be a success or failure
if (se.ServerModel.Success == true) SuccessHistoryEntry(historyEntry);
else ErrorHistoryEntry(se.ServerModel.Message, historyEntry);
}
else
{
ErrorHistoryEntry("Process timed out generating subscription", historyEntry);
}
}
db.SaveChanges();
}
// ...
}
SubscriptionExecution is:
internal class SubscriptionExecution : IDisposable
{
// ...
internal SubscriptionExecution(string url, int subscriptionId, string fileType, string logLocation)
{
LogLocation = logLocation;
SubscriptionId = subscriptionId;
FileType = fileType;
Completed = false;
ServerModel = new ServerModel();
ServerModel.PropertyChanged += new PropertyChangedEventHandler(ServerModel_PropertyChanged);
browser = new ChromiumWebBrowser(url);
browser.RegisterJsObject("serverModel", ServerModel);
browser.LoadingStateChanged += LoadingStateChanged;
if (LogLocation != string.Empty) browser.ConsoleMessage += BrowserConsoleMessage;
}
private void ServerModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Success")
{
Completed = true;
}
}
// ...
}
There's a loadingStateChange function that injects some javascript that modifies the ServerModel.Success field when an event happens.
However, I can't seem to get this far. Except in one specific circumstance, the timerjob reaches the Cef.Initialize(settings) section, and then immediately restarts. Unfortunately, outside the event log, there is no error message to be found.
In the Windows Event Log, there is
which is indicating an error in libcef.dll
I've installed the libcef.dll.pdb symbols for this version of cefsharp (v57).
The CefSharp troubleshooting guide suggests that in instances like this, there should be logfiles or error dumps. There should be a file 'debug.log' in the folder with the executable, and perhaps a mini crashdump file in AppData\Local\CrashDumps.
However, as this is a SharePoint Timer job, these files don't seem to be appearing. I did find a debug.log file inside the 15 hive bin (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\BIN); however this file is empty.
I can't find the CrashDumps folder anywhere; checked under all user accounts on the machine, especially the running account and the SharePoint Timer Job managed account.
The one specific circumstance that I can get the code to run without restarting the timer job is immediately after the server is restarted. Restarting the server has (temporarily) fixed a few other problems around this issue, and so I suspect it might be related. Namely, I have had trouble retracting and redeploying the farm solution. This throws a couple of different errors:
on retracting: <SERVER>: The process cannot access the file 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\BIN...\icudtl.dat' because it is being used by another process.
Error: The removal of this file failed: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\BIN...\icudtl.dat.
on deploying: The requested operation cannot be performed on a file with a user-mapped section open.
I've attempted to find what other process is using icudtl.dat using Process Explorer, but all I could find was chrome, which was accessing a different copy. Killing all chrome processes and trying again also did not solve the problem.

Tfs Check-in using PendAdd: The array must contain at least one element

So I'm having a problem with automating my code to check-in files to TFS, and it's been driving me up the wall! Here is my code:
string location = AppDomain.CurrentDomain.BaseDirectory;
TfsTeamProjectCollection baseUserTpcConnection = new TfsTeamProjectCollection(uriToTeamProjectCollection);
IIdentityManagementService ims = baseUserTpcConnection.GetService<IIdentityManagementService>();
TeamFoundationIdentity identity = ims.ReadIdentity(IdentitySearchFactor.AccountName, #"PROD1\JR", MembershipQuery.None, ReadIdentityOptions.None);
TfsTeamProjectCollection impersonatedTpcConnection = new TfsTeamProjectCollection(uriToTeamProjectCollection, identity.Descriptor);
VersionControlServer sourceControl = impersonatedTpcConnection.GetService<VersionControlServer>();
Workspace workspace = sourceControl.CreateWorkspace("MyTempWorkspace", sourceControl.AuthorizedUser);
String topDir = null;
try
{
Directory.CreateDirectory(location + "TFS");
String localDir = location + "TFS";
workspace.Map("$/Automation/", localDir);
workspace.Get();
destinationFile = Path.Combine(localDir, Name + ".xml");
string SeconddestinationFile = Path.Combine(localDir, Name + ".ial");
bool check = sourceControl.ServerItemExists(destinationFile, ItemType.Any);
PendingChange[] pendingChanges;
File.Move(sourceFile, destinationFile);
File.Copy(destinationFile, sourceFile, true);
File.Move(SecondsourceFile, SeconddestinationFile);
File.Copy(SeconddestinationFile, SecondsourceFile, true);
if (check == false)
{
workspace.PendAdd(localDir,true);
pendingChanges = workspace.GetPendingChanges();
workspace.CheckIn(pendingChanges, Comments);
}
else
{
workspace.PendEdit(destinationFile);
pendingChanges = workspace.GetPendingChanges();
workspace.CheckIn(pendingChanges, Comments);
}
and the problem is that whenever it's NEW files (PendEdit works correctly when the files already exist in TFS) that my code is attempting to check in, and it runs through this code:
if (check == false)
{
workspace.PendAdd(localDir,true);
pendingChanges = workspace.GetPendingChanges();
workspace.CheckIn(pendingChanges, Comments);
}
The files, instead of being in the included changes in pending changes, are instead in the excluded changes like so:
and when the line that actually does the check-in runs, I'll get a "The array must contain at least one element" error, and the only way to fix it is to manually add those detected changes, and promote them to included changes, and I simply can't for the life of me figure out how to do that programatically though C#. If anyone has any guidance on what direction I should take for this, I would really appreciate it! Thank you!
Edit: I've also discovered another way to solve this by reconciling the folder, which also promotes the detected changes, but again the problem is I can't seem to figure out how to program that to do it automatically.
I know that running the visual studio developer command prompt, redirecting to the folder that this mapping is in, and the running "tf reconcile /promote" is one way, but I can only automate that as far as the /promote part, because that brings up a toolbox that a user would have to input into, which defeats the purpose of the automation. I'm at a loss.
Next Edit in response to TToni:
Next Edit in response to TToni:
I'm not entirely sure if I did this CreateWorkspaceParameters correctly (see picture 1), but this time it gave the same error, but the files were not even in the excluded portions. They just didn't show up anywhere in the pending changes (see picture 2).
Check this blog:
The workspace has a method GetPendingChangesWithCandidates, which actually gets all the “Excluded” changes. Code snippet is as below:
private void PendChangesAndCheckIn(string pathToWorkspace)
{
//Get Version Control Server object
VersionControlServer vs = collection.GetService(typeof
(VersionControlServer)) as VersionControlServer;
Workspace ws = vs.TryGetWorkspace(pathToWorkspace);
//Do Delete and Copy Actions to local path
//Create a item spec from the server Path
PendingChange[] candidateChanges = null;
string serverPath = ws.GetServerItemForLocalItem(pathToWorkspace);
List<ItemSpec> its = new List<ItemSpec>();
its.Add(new ItemSpec(serverPath, RecursionType.Full));
//get all candidate changes and promote them to included changes
ws.GetPendingChangesWithCandidates(its.ToArray(), true,
out candidateChanges);
foreach (var change in candidateChanges)
{
if (change.IsAdd)
{
ws.PendAdd(change.LocalItem);
}
else if (change.IsDelete)
{
ws.PendDelete(change.LocalItem);
}
}
//Check In all pending changes
ws.CheckIn(ws.GetPendingChanges(), "This is a comment");
}

WinForm settings not saving on Windows Shut down

I have a Winform program I wrote for a university so they could run test files.
The program is targeted at .NET 4.5 and was developed and run on Windows 7.
To make the running of files easy for the technicians I added the ability to save the paths to the test files. The file path is stored in a settings file (ProgSettings.settings) under the variable "RunNames".
When the save button is pressed I run a validation to ensure the test file at the end of the path exists and is valid before adding it to the RunNames list:
/*
*Function: SaveButton_Click(sender, e)
*Notes: responds to the button click "Save"
* tries to save file (if valid) to settings
*/
private void SaveButton_Click(object sender, EventArgs e)
{
if (ValidateFile(TrackFileName.Text))
{
string displayName = updateLinkNames(TrackFileName.Text).ToUpper();
if (!ListOfTracks.Items.Contains(displayName))
{
ListOfTracks.Items.Add(displayName);
runNames.Add(TrackFileName.Text);
}
updateSettings();
}
}
/*
*Function: ValidateFile(fileName)
*Notes: called from save button click
* checks validity of the file path
*/
private bool ValidateFile(string fileName)
{
fileName = fileName.ToUpper();
FileToRun.FileName = fileName;
validFile = System.IO.File.Exists(fileName);
if(fileName != null && fileName != "" && fileName.EndsWith(".TXT") && validFile)
{
return true;
}
else
{
return false;
}
}
/*
*Function: updateSettings()
*Notes: updates the settings with the valid path
*/
private void updateSettings()
{
string value = String.Join(",", runNames.Select(i => i.ToString()).ToArray());
ProgSettings.Default.RunNames = value;
ProgSettings.Default.Save();
}
(For those of you unfamiliar with .settings they store the basic types available on C#, there are ways to include arrays as well but the simplest solution is to concatenate the different parts of the array in a string with a separator - in my case ",").
Then when I load the program again I populate a list of checkboxes using the a getSavedFiles() function below.
private List<string> getSavedFiles()
{
string localRunNames = ProgSettings.Default.RunNames.ToUpper();
string[] arr = localRunNames.Split(',').Select(s => Convert.ToString(s)).ToArray();
return arr.Cast<String>().ToList();
}
The settings works correctly on both my work computer and my personal laptop I did testing on. However, now the program is in use at the university they are saying that after shutting down the computer the next time the run the program all the saved file paths are gone.
Is this due to how to how the university managers users accounts on the computer? Or is there some other reason for this?
Was there another alternative to storing setting information for cases where .settings files don't work? (The only other solution I can think of is having the program write a config.txt file which I would prefer not to do encase the application is used somewhere it won't having writing permissions)
Below is a link to a question about the best practice to save application settings:
Best practice to save application settings

Set Script Task code dynamically in SSIS 2012

In my application, a script task is created dynamically.
In SQL Server 2008's implementation of SSIS, the following method worked fine.
private void SetSourceCode(ScriptTask scriptTask, string code, string codeName)
{
string fileName = "ScriptMain.vb";
string language = "VisualBasic";
string proj = ".vbproj";
scriptTask.ScriptLanguage = VSTAScriptLanguages.GetDisplayName(language);
scriptTask.ScriptingEngine.InitNewScript(language,
scriptTask.ScriptProjectName, proj);
scriptTask.ScriptingEngine.ShowDesigner(false);
scriptTask.ScriptingEngine.AddCodeFile(fileName, code);
if (!scriptTask.ScriptingEngine.Build())
throw new Exception("Failed to build vb script code: " + codeName);
scriptTask.ScriptingEngine.SaveScriptToStorage();
if (!scriptTask.ScriptingEngine.CloseIDE(false))
{
throw new Exception("Unable to close Scripting engine.");
}
}
How do I migrate this code to SQL Server 2012, because following methods are removed from SQL Server 2012 dll-s (assemblies):
InitNewScript
AddProjectReference
AddCodeFile
SaveScriptToStorage
CloseIDE
Build
ShowDesigner
Generally, how do I dynamically set source code for script task in SQL Server 2012?
As you've noticed, the VSTA helper methods you could use in 2008 were moved/removed in 2012. It is still possible to do, but the code has changed.
The easiest thing to do is load an existing project using VstaHelper.LoadProjectFromFolder().
If you want to dynamically add script files, see the snippet below. There are two main things you need to keep in mind:
The ScriptingEngine and VstaHelper classes represent VSTA itself. This is where you’d create the project, and add new files. You cannot remove or replace an existing file directly here. When you call SaveProjecToStorage(), it's like closing the VSTA window … it saves the project and compiled binary to the ScriptTask.
ScriptTask.ScriptStorage allows you to directly manipulate the source file contents. From here, you can modify the content of a file.
The following code snippet should help you get started.
static void Main(string[] args)
{
// 1. Create new package, and add a script task
var pkg = new Package();
var exec = pkg.Executables.Add("STOCK:ScriptTask");
var th = (TaskHost)exec;
th.Name = "Script Task";
th.Description = "This is a Script Task";
var task = (ScriptTask)th.InnerObject;
// 2. Set the script language - "CSharp" or "VisualBasic"
task.ScriptLanguage = VSTAScriptLanguages.GetDisplayName("CSharp");
// 3. Set any variables used by the script
//task.ReadWriteVariables = "User::Var1, User::Var2";
// 4. Create a new project from the template located in the default path
task.ScriptingEngine.VstaHelper.LoadNewProject(task.ProjectTemplatePath, null, "MyScriptProject");
// 5. Initialize the designer project, add a new code file, and build
//task.ScriptingEngine.VstaHelper.Initalize("", true);
//task.ScriptingEngine.VstaHelper.AddFileToProject("XX.cs", "FileContents");
//task.ScriptingEngine.VstaHelper.Build("");
// 6. Persist the VSTA project + binary to the task
if (!task.ScriptingEngine.SaveProjectToStorage())
{
throw new Exception("Save failed");
}
// 7. Use the following code to replace the ScriptMain contents
var contents = File.ReadAllText("path to file");
var scriptFile =
task.ScriptStorage.ScriptFiles["ScriptMain.cs"] =
new VSTAScriptProjectStorage.VSTAScriptFile(VSTAScriptProjectStorage.Encoding.UTF8, contents);
// 8. Reload the script project, build and save
task.ScriptingEngine.LoadProjectFromStorage();
task.ScriptingEngine.VstaHelper.Build("");
// 9. Persist the VSTA project + binary to the task
if (!task.ScriptingEngine.SaveProjectToStorage())
{
throw new Exception("Save failed");
}
// 10. Cleanup
task.ScriptingEngine.DisposeVstaHelper();
// 11. Save
string xml;
pkg.SaveToXML(out xml, null);
File.WriteAllText(#"c:\temp\package.dtsx", xml);
}

TFS / File Checkout from C#

I don't have a great deal of experience with TFS, other than using it for source control. I am working on a C# application that will need to modify files that are being controlled by TFS. From within my C# application, how can I check out a file that is controlled via TFS?
Thanks - Randy
You can use PendEdit to make your files writables, make your changes to it, then you add it to the pending changes, and finally check it in.
Here is some code where a folder structure is created and then checked in (Very similar to what you will need).
private static void CreateNodes(ItemCollection nodes)
{
using (var tfs = TeamFoundationServerFactory.GetServer("http://tfsserver:8080"))
{
var versionControlServer = tfs.GetService(typeof (VersionControlServer)) as VersionControlServer;
versionControlServer.NonFatalError += OnNonFatalError;
// Create a new workspace for the currently authenticated user.
var workspace = versionControlServer.CreateWorkspace("Temporary Workspace", versionControlServer.AuthenticatedUser);
try
{
// Check if a mapping already exists.
var workingFolder = new WorkingFolder("$/testagile", #"c:\tempFolder");
// Create the mapping (if it exists already, it just overides it, that is fine).
workspace.CreateMapping(workingFolder);
// Go through the folder structure defined and create it locally, then check in the changes.
CreateFolderStructure(workspace, nodes, workingFolder.LocalItem);
// Check in the changes made.
workspace.CheckIn(workspace.GetPendingChanges(), "This is my comment");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// Cleanup the workspace.
workspace.Delete();
// Remove the temp folder used.
Directory.Delete("tempFolder", true);
}
}
}
private static void CreateFolderStructure(Workspace workspace, ItemCollection nodes, string initialPath)
{
foreach (RadTreeViewItem node in nodes)
{
var newFolderPath = initialPath + #"\" + node.Header;
Directory.CreateDirectory(newFolderPath);
workspace.PendAdd(newFolderPath);
if (node.HasItems)
{
CreateFolderStructure(workspace, node.Items, newFolderPath);
}
}
}
Using the other solution gave me permission problems.
Here's an alternative way to checkout your files using tf.exe:
//Checkout file
Process proc = new Process();
proc.StartInfo =
new ProcessStartInfo(
#"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe",
string.Format("checkout \"{0}\"", fileLocation)
);
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
For those looking to use the first solution and resolve the permission issue you can use the following code to use the current credentials, this replaces the "TeamFoundationServerFactory.GetServer" call then use the TfsTeamProjectCollection (tmPrjColl) to get the VersionControlServer:
using Microsoft.TeamFoundation.Client;
using MTVC = Microsoft.TeamFoundation.VersionControl.Client;
using MVSC = Microsoft.VisualStudio.Services.Common;
MVSC.VssCredentials creds = new MVSC.VssCredentials(new MVSC.WindowsCredential(true));
using (TfsTeamProjectCollection tmPrjColl = new TfsTeamProjectCollection(new Uri("<source control URL>"), creds))
{
MTVC.VersionControlServer verCtrlSvr = tmPrjColl.GetService<MTVC.VersionControlServer>();
...
}

Categories