SharePoint: PortalContext – gives “Value cannot be null. Parameter name: site”

I came across the following problem in PortalContext API.

 

Say I have created a portal with port number 9091. I am using the following code snippet to retrieve the portal context.

 

[working though we changed the port number]

TopologyManager tm = new TopologyManager();

PortalSite ps = tm.PortalSites[ new Uri[https://karthickmain:9091)];

PortalContext ctx = PortalApplication.GetContext(ps);

 

The code works fine no problem. For some reason, I have changed the port number to 9092. Now I have modified the code by changing the port number.

 

[not working]

TopologyManager tm = new TopologyManager();

PortalSite ps = tm.PortalSites[ new Uri[https://karthickmain:9092)];

PortalContext ctx = PortalApplication.GetContext(ps);

 

The code doesn’t work. Its giving the following error. “Value cannot be null. Parameter name: site”

 

But if I use the same port number 9091 and execute the earlier code, its working fine. Because of this issue, getting the portal context by hardcoding the portal url is not advisable.