Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to pass an object which is a management scope :
ConnectionOptions options = new ConnectionOptions
{
    Username = sv.User,
    Password = sv.Pass
};
ManagementScope scope = new ManagementScope(@"\\" + sv.Name + @"\root\cimv2", options);
scope.Connect();
return scope;

This is the first controller, basically I'm getting inputs from a form and then connecting to the server with these inputs using the scope which will help me later on to execute commands
public IActionResult Proceed(Server serverModel)
{
    if (!ModelState.IsValid) return View("Connect");
    else
    {
        try
        {
            // ---------- This is the function returning the scope ---------- \\
            ManagementScope scope = Connecting.ConnectToServer(serverModel);
            // ---------- This is the function returning the scope ---------- \\
            return RedirectToAction("Menu", "Schema");
        }
        catch (Exception e)
        {
            ViewBag.Message = e.Message.ToString();
            return View("Failed");
        }
    }
}

And this is the second controller , notice that I'm passing the scope as second parameter but that won't work obviously cause scope is unknown
public IActionResult ExportProceed(SchemaExport ex)
 {
     if (!ModelState.IsValid) return View("Export");
     else
     {
         try
         {
             // ---------- This is where i need to pass the scope ---------- \\
             ExportProcess.CreateDirectories(ex, scope);
             // ---------- This is where i need to pass the scope ---------- \\
             return RedirectToAction("Menu", "Schema");
         }
         catch (Exception e)
         {
             ViewBag.Message = e.Message.ToString();
             return View("Failed");
         }
     }
 }


What I have tried:

I tried sending the scope object as TempData and converting it with json but it says that I can't convert ManagementScope Object.
Posted
Updated 10-May-20 7:00am

When your controller action stops running (after a "return") then everything that it has created is gone, all variables, all objects. If you want to keep an object between requests you'll need to store it in the Session, or similar. However rather than trying to re-use the same scope just create a new one any time you need it.
 
Share this answer
 
I agree with F-ES, what you're trying to do is the wrong way to do it.

Don't create a scope for use later in another request. That's going to be impossible to manage.

ALWAYS create the resource you need when you need it as late as possible in your code, use it, then close and dispose of it as early as possible. This minimizes the resource footprint of your web app and minimizes the load on the server.
 
Share this answer
 
The problem is that the scope is intialized with variables entered from a previous form.
I've solved this with creating the scope in as a static class with its getter and then use it for all controllers.
 
Share this answer
 
Comments
Richard Deeming 11-May-20 15:08pm    
A very bad idea - static variables are shared across all requests from all users of your application.

Using a static variable in a web application is almost always the wrong thing to do.
dreevo 16-May-20 12:18pm    
I couldn't find any other solution allowing me to keep the variable throughout all requests.
Richard Deeming 18-May-20 6:17am    
ASP.NET Core's session state is limited to strings, and you can't serialize a ManagementScope to JSON, so you'll need to serialize the data required to create the ManagementScope to JSON and store that in the session.

Unless you've done something odd, there shouldn't be a problem with serializing the serverModel to JSON.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900