Skip to content
This repository has been archived by the owner on Jun 20, 2019. It is now read-only.

Add environment variables for external information #46

Closed
Drawaes opened this issue Nov 25, 2016 · 6 comments
Closed

Add environment variables for external information #46

Drawaes opened this issue Nov 25, 2016 · 6 comments

Comments

@Drawaes
Copy link

Drawaes commented Nov 25, 2016

Is it possible to add environment variables to allow the hosted application to understand the external address that IIS is exposing for the hosted application. This information is available from the HttpContext.Request but only once you have a request. An example of how you can do this in a "traditional asp.net" application

ServerManager serverMgr = new ServerManager();
Site site = serverMgr.Sites["YourSiteName"];
List<string[]> urls = new List<string[]>();
foreach (Binding binding in site.Bindings)
{
string bindingInfo = binding.BindingInformation;
string subString = bindingInfo.Substring(2, bindingInfo.Length - 2);
string[] adrs = subString.Split(':');
adrs[0] = "localhost:" + adrs[0];
urls.Add(adrs);
}

@guardrex
Copy link
Contributor

Adding here for workaround reference with PS, a prototype ...

using System.Diagonistics;
...
var siteName = "testselfcontained";
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "powershell.exe";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.Arguments = $"$webBinding = Get-WebBinding -Name '{siteName}';$port = $webBinding.bindinginformation.split(':')[1];Write-Host $port";
Process p  = Process.Start(psi);
var port = p.StandardOutput.ReadToEnd();
p.WaitForExit();

Setting the execution policy didn't work, so I had to elevate the AppPool's user identity to Local System to make it work.

PowerShell bits for Core is still alpha (as of 11/27/2016), and I ended up with dep conflicts in my 1.1/MSBuild test app (1.0.0-preview3-004056) when I tried to implement a test, but this is the direction I want to go in the future ...

NuGet.config:
<add key="powershell.myget.org" value="https://powershell.myget.org/F/powershell-core" />

csproj:
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.0.0-alpha13" />

string port;
using (PowerShell ps = PowerShell.Create())
{
    ps.AddScript($"$webBinding = Get-WebBinding -Name '{siteName}';$port = $webBinding.bindinginformation.split(':')[1];Write-Host $port");
    var result = ps.Invoke()[0];
    port = result.toString();
}

@shirhatti
Copy link
Contributor

@Drawaes Looks like you are just using Microsoft.Web.Administration get your site bindings. This is still possible in .NET Core. See https://www.nuget.org/packages/Microsoft.Web.Administration/10.0.0-rc1

If you can provide some more detailed information about what you are doing, we may be able to help you.

@guardrex
Copy link
Contributor

sooooooo much nicer ...

ServerManager serverManager = new ServerManager();
ViewBag.PORT = serverManager.Sites["testselfcontained"].Bindings[0].EndPoint.Port.ToString();

Just had to run it under LocalSystem to get out of permissions trouble.

@Drawaes
Copy link
Author

Drawaes commented Nov 30, 2016

I will give that a crack

@shirhatti
Copy link
Contributor

Feel free to re-open if that doesn't work out for you

@Drawaes
Copy link
Author

Drawaes commented Dec 8, 2016

That's good enough for me. Thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants