Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8386681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:00:39+00:00 2026-06-09T18:00:39+00:00

I have a MVC web application which shows some information about users in our

  • 0

I have a MVC web application which shows some information about users in our AD. The AD is synchronized with Office 365, so using the UPN I can retrieve the license information from Office 365 using the Windows PowerShell cmdlets for Office 365. Basically this all works fine.

As the initialization cmdlet Connect-MsolService takes some time to finish, I’m using kind of a singleton pattern for my Office365Connector class. In my Global.asax in Application_Start() I initialize the singleton instance, in Application_End() I dispose it. The connector class uses exactly one instance of my PowerShellInvoker class which – as the name implies – encapsulates PowerShell invoking. The PowerShell initialization code inside the PowerShellInvoker constructor looks like this:

public PowerShellInvoker(params string[] modules)
{
    var iss = InitialSessionState.CreateDefault();
    iss.ImportPSModule(modules);
    iss.ThrowOnRunspaceOpenError = true;

    _runspace = RunspaceFactory.CreateRunspace(iss);
    _runspace.Open();
    _invoker = new RunspaceInvoke(_runspace);
}

The Office365Connector class calls this constructor with "MSOnline" as parameter. The MSOnline module contains the cmdlets for Office 365. I keep the _runspace and _invoker fields for command execution at a later time. Both fields will be disposed in the Dispose method of PowerShellInvoker (which is called when the Office365Connector class is being disposed). Script execution is done by this line of code:

_invoker.Invoke(scriptText);

So much for the introduction – now here comes the real problem:

In my application, I have a user list. When I click a user, additional information is loaded using an AJAX request. Within this request, my app uses the singleton instance of the Office365Connector class to retrieve the license information for the user. In most cases, this all works perfectly. But sometimes the AJAX request ends up with a code 500. Debugging my source code, I stumbled upon an Exception being thrown in the PowerShellInvoker on the “Invoke” line above, telling me that the Runspace is not open anymore, and I can’t figure out why. I can’t even really reproduce it. Sometimes, the error occurs when I click the second user. Sometimes, the error occurs on the 10th or 15th user. I already thought about some weird clean-up, timeout or garbage collection techniques used by MVC, but I haven’t come to a conclusion. IMHO, the Runspace closing can’t be time-based because the time between the “user clicks” is just a few seconds.

The Connect-MsolService cmdlet creates a connection to Office 365, but it doesn’t return anything. So re-creating the Runspace if needed is not a work-around because this would be done by the PowerShellInvoker class and the Office365Connector wouldn’t know that it has to reconnect to Office 365. (Also this would not solve the problem.) Combining the two classes isn’t a solution either because the PowerShellInvoker is also used elsewhere.

So can anyone tell me how to prevent the Runspace from closing or why it is closed?

Edit: More code

The full PowerShellInvoker class can be found here.

In the Office365Connector class, there is currently much overhead. Here are some snippets:

Initialization in constructor:

var cred = new PSCredential(adminUpn, adminPassword);

_psi = new PowerShellInvoker("MSOnline");
_psi.ExecuteCommand("Connect-MsolService", new { Credential = cred });

Method to retrieve the license for a UPN:

public IEnumerable<string> GetUserLicenses(string upn)
{
    PSObject[] licenses = _psi.ExecuteScript(string.Format("(Get-MsolUser -UserPrincipalName \"{0}\").Licenses | % {{ $_.AccountSkuId }}", upn)).ToArray();

    // no licenses: a list with one element (which is null) is returned.
    if (licenses.Length == 1 && licenses[0] == null)
    {
        licenses = new PSObject[0];
    }

    return licenses.Select(pso => pso.ToString()).ToList();
}

As you can see, I added some ToLists to the method return values (especially in the PowerShellInvoker). I did this because I wanted to prevent lazy execution of the enumerables because I thought this could be the reason for the closed Runspace.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:00:40+00:00Added an answer on June 9, 2026 at 6:00 pm

    OK, this was a stupid mistake by myself – although I don’t really understand why MVC is doing this:

    In one of my many attempts to solve the problem I moved the disposal code of my Office365Connector to the Dispose method of the application (in Global.asax.cs). After that I apparently fixed the original error but it didn’t work out because of the disposal stuff. Apparently the application instance is disposed more often than it is “ended”. When I moved the code back to Application_End() where it belongs everything worked fine.

    That makes my original question kind of invalid because I wrote that my dispose code already was in Application_End(). :-\

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Clustered Windows Hosting site which currently hosts an MVC Web Application
I am developing an ASP.Net MVC 3 Web application using Razor Views. I have
I have a web application which was using Asp.net MVC2 . I Upgraded it
I am using Spring MVC to build my web application, and I have a
I have an ASP.NET MVC web application which is hosted by an external provider,
I have a web application built on ASP.NET MVC framework which requires a service
We currently have a classic ASP.NET web application which serves as our API. It
I have an MVC 2 web application, which is nearing release. Until now I
I have _Layout.cshtml defined for my mvc application, which is shown below: @inherits System.Web.Mvc.WebViewPage
I have a Spring MVC web application which needs to connect to a database,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.