Open Excel from a WebAPI service hosted in local IIS - c#

I'm facing a strange problem with the IIS Local. I've developed a webapi Controller that, at some point, opens Excel, makes some processing with macros and then closes. Here's the piece of code that opens Excel with several parameters:
var procStartInfo = new ProcessStartInfo(
this.Parameters[ConsoleExecutionParameters.FileToExecute],
this.ParametersProcessed)
{
CreateNoWindow = false,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
WindowStyle = ProcessWindowStyle.Maximized,
WorkingDirectory = workingDirectory
};
using (var proc = new Process { StartInfo = procStartInfo })
{
try
{
proc.EnableRaisingEvents = true;
proc.Start();
proc.WaitForExit(60000);
proc.StandardInput.Flush();
if (!proc.HasExited)
....
Gotta say that this piece of code WORKS (and opens the excel correctly) if the WebAPI is run under IIS Express. However, when running it under a local IIS, an Excel process is opened (at least it is shown in the task manager) but no Excel window appears, the application seems to hang when "proc.WaitForExit" instruction is called, and EXCEL.EXE process stays like in a "zombie" state. After some time (60 seconds) my application times out and the Excel.EXE process is still hung.
Does someone have any idea about why it works on IIS express and doesn't in Local IIS?
Thanks and kind regards.

Related

Getting the output of .exe program in IIS under windows server 2012

We have our own C++ compiled OCR as .exe file that takes the image location as a parameter and return a string, and we place it in folder within our web-API 2 application folder, now we start the OCR as a process from the web-api, get the output and return it back.
everything works good in a local machine, when we deploy the API in the server, the output cannot be retrieved unless we replace the Application pool identity with the Admin in the application pool. At this stage we need to use the Application pool identity (or any other user but the admin) and still be able to retrieve the output from the process here is our code:
ProcessStartInfo info = new ProcessStartInfo
{
WorkingDirectory = enginepath,
CreateNoWindow = true,
UseShellExecute = false,
FileName = enginepath+"//"+"OCR.exe",
RedirectStandardOutput = true,
Arguments = " "+imageFilepath
};
using (Process process = Process.Start(info))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
return result;
}
}
we tried all the popular ways from giving the right permissions,loading the user profile=true, and still can't be able to retrieve the output.
*we need to be able to get the output within the web server application.

How to execute Git commands using cmd.exe being called from another application?

I have an application that generates some files. Once the files are generated, i want to then perform some Git commands to start a local repository. I have spent a few days on multiple solutions and endless googling but i can't get this to work as expected.
If i manually kick off the .exe, locally or on a server, it works perfectly. However, when the application(MVC) calls the .exe it doesn't work locally or on a server. I will provide the code that calls the .exe itself and then what the .exe code is doing.
MVC call to run .exe
//Call git exe
var processInfo = new ProcessStartInfo()
{
Arguments = Arg1 + " " + Arg2,
UseShellExecute = false,
FileName = #"C:\Foo.exe"
};
var process = new Process()
{
StartInfo = processInfo
};
process.Start();
process.WaitForExit();
This .exe gets launched without issue so this part is working i think. I checked it via task manager.
Git command execution
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false,
WorkingDirectory = Properties.Settings.Default.OutPutDirectory
},
};
process.Start();
using (var writer = process.StandardInput)
{
writer.WriteLine("git init");
writer.WriteLine("git add --all");
writer.WriteLine("git commit --author=\"" + ParseUserNameFromEmail() + " <" + _userEmailAddress + ">\" -m \"Initial Commit\"");
}
If i execute this part manually from the cmd prompt it works perfect on both local and server. But as soon as this .exe is called from an MVC app, it runs but the git repo isn't created as expected. I am at a total lost. I have tried running the process with my creds and a service account. I have also tried capturing the output in hope to shed light on what the issue could be but it's just empty on error and output redirects :(.

Printing a PDF from ASP .Net (c#) using Acrobat Reader

I am trying to print a PDF using ASP (C#) through Adobe Reader -the problem is it does work on my local machine but not on server. On my local it starts Adob eReader in minimized state and I can see the file present inside Printer's "See what's printing" window. But on the server I can see the process has started from Task Manager but there's no UI visible as well no file in printer's list.
I though it may be a permission issue but after trying the following steps - it still does not work.
What I have tried.
As by default it runs under DefaultAppPool user - so I created a new App pool under the admin user, it now starts the process under admin but still I can't see the UI and no output on printer.
I added permission "Allow service to interact with desktop" to IIS Admin Service following this article https://support.microsoft.com/en-us/help/555134 - but no difference.
My current code
string args = string.Format("/s /o /h /t \"{0}\" \"{1}\"", filepath, printerName);
var startInfo = new ProcessStartInfo {
FileName = Properties.Settings.Default.AdobeReaderPath,
Arguments = args,
CreateNoWindow = true,
ErrorDialog = false,
UseShellExecute = false,
Verb = "print",
WindowStyle = ProcessWindowStyle.Minimized,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
var process = Process.Start(startInfo);
In the Application Pool Advanced settings, make sure you set the option
"Load User Profile" to True on the server.

Working a process in background

I am using C# to run another .exe applicaiton.
however I want to run the .exe in background
I tried Start info hide bu.t it didn't help
How can I do that?
currently this is the code I am using
_p = new System.Diagnostics.Process();
_startInfro = new System.Diagnostics.ProcessStartInfo();
_startInfro.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
_startInfro.FileName = tmp[0];
_startInfro.Arguments = deleteRangeCommand;
_p.StartInfo = _startInfro;
_p.Start();
Since I you don't provide information about which .exe you want to start I can't be sure about this answer. But the CreateNoWindow property looks like what you need.
You must also set the UseShellExecute property to false as is pointed out in the documentation. Otherwise the value of CreateNoWindow is ignored.

Executing batch file in WCF

I am trying to execute a batch file server-side in IIS to add a printer using the printuientry call.
The problem I am facing is that I am using the Copy To Output Directory - Copy Always and the following code:
var path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
var processInfo = new ProcessStartInfo(Path.Combine(path, "AddPrinter.bat"))
{
CreateNoWindow = true,
UseShellExecute = false,
WorkingDirectory = path,
Arguments = ipAddress,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
var process = Process.Start(processInfo);
process.WaitForExit(10000);
process.Close();
Now when deugging, I have checked the values of path and its set to
file:\C:\_Projects\PrinterServerV2\bin
and I have checked to see if the file and directory exist which they do.
But I get the exception:
System.ComponentModel.Win32Exception (0x80004005): The directory name is invalid
Any ideas please??
Check if the user you had set in the iis configuration does have all privileges to run, access, write and read what you wanna do with your batch file.
Also try to change your ProcessStartInfo like the process will be cmd.exe and your batch file the argument.
I had a similar issue How to execute multiples .BAT files in C#
try AppDomain.CurrentDomain.BaseDirectory as path.

Categories

Resources