I tried selft hosting but i was having some problems 1.
HttpContext.Current is null
- I have a custom error handler in the web API and do not work in self-hosted ones
IisExpress solution works very well for me
bat file for deployment in iis
@Echo off set msBuildDir=C:\Program Files (x86)\MSBuild\14.0\Bin ::compile web api project call "%msBuildDir%\msbuild.exe" solutionFilePath.sln /t:projectName /p:Configuration=Debug;TargetFrameworkVersion=v4.5 /l:FileLogger,Microsoft.Build.Engine;logfile=Manual_MSBuild_ReleaseVersion_LOG.log /p:Platform="Any CPU" /p:BuildProjectReferences=false call "C:\Program Files (x86)\IIS Express\iisexpress.exe" /path:"pathToRootOfApiProject" /port:8888 /trace:error
I work with Nunit frameWork
[SetUpFixture] public class SetUpTest { private Process process = null; private Process IisProcess = null; private System.IO.StreamWriter sw = null; string programsFilePath = Environment.GetEnvironmentVariable(@"PROGRAMFILES(X86)"); [OneTimeSetUp] public void Initialize() { //compile web api project List<string> commands = new List<string>(); commands.Add( $@ "CD {programsFilePath}\MSBuild\14.0\Bin\"); commands.Add( $@ "msbuild ""pathToYourSolution.sln"" /t:ProjrctName /p:Configuration=Debug;TargetFrameworkVersion=v4.5 /p:Platform=""Any CPU"" /p:BuildProjectReferences=false /p:VSToolsPath=""{programsFilePath}\MSBuild\Microsoft\VisualStudio\v14.0"""); RunCommands(commands); //deploy to iis express RunIis(); } [OneTimeTearDown] public void OneTimeTearDown() { if (IisProcess.HasExited == false) { IisProcess.Kill(); } } void RunCommands(List<string> cmds, string workingDirectory = "") { if (process == null) { InitializeCmd(workingDirectory); sw = process.StandardInput; } foreach (var cmd in cmds) { sw.WriteLine(cmd); } } void InitializeCmd(string workingDirectory = "") { process = new Process(); var psi = new ProcessStartInfo(); psi.FileName = "cmd.exe"; psi.RedirectStandardInput = true; psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; psi.UseShellExecute = false; psi.WorkingDirectory = workingDirectory; process.StartInfo = psi; process.Start(); process.OutputDataReceived += (sender, e) => { Debug.WriteLine($"cmd output: {e.Data}"); }; process.ErrorDataReceived += (sender, e) => { Debug.WriteLine($"cmd output: {e.Data}"); throw new Exception(e.Data); }; process.BeginOutputReadLine(); process.BeginErrorReadLine(); } void RunIis() { string _port = System.Configuration.ConfigurationManager.AppSettings["requiredPort"]; if (_port == 0) { throw new Exception("no value by config setting for 'requiredPort'"); } IisProcess = new Process(); var psi = new ProcessStartInfo(); psi.FileName = $@ "{programsFilePath}\IIS Express\iisexpress.exe"; psi.Arguments = $@ "/path:""pathToRootOfApiProject"" /port:{_port} /trace:error"; psi.RedirectStandardInput = true; psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; psi.UseShellExecute = false; IisProcess.StartInfo = psi; IisProcess.Start(); IisProcess.OutputDataReceived += (sender, e) => { Debug.WriteLine($"cmd output: {e.Data}"); }; IisProcess.ErrorDataReceived += (sender, e) => { Debug.WriteLine($"cmd output: {e.Data}"); if (e.Data != null) { throw new Exception(e.Data); } }; IisProcess.BeginOutputReadLine(); IisProcess.BeginErrorReadLine(); } }
attach to iisexpress
Then run a debug test, then go to "Debug"> "Attach to Process"> in the application to select
click "OK",
search iisexpress and click attach
Yitzhak weinberg
source share