Search code examples
asp.netasp.net-mvc-4orchardcmsorchard-modulesorchardcms-1.9

Making Orchard Services Available in Web Forms: Disable Disposing Orchard Services IOrchardService?


After Adding a New Module to the Orchard Solution in VS 2013, In the Home Controller's Index method, Iam saving the IOrchardServices in Session as:

public class HomeController : Controller
    {
        public IOrchardServices Services { get; set; }

    public HomeController(IOrchardServices services)
    {
        Services = services;
        T = NullLocalizer.Instance;
    }

    public Localizer T { get; set; }

    public ActionResult Index()
    {
           Session["OrchardCMServices"] = Services;
           return Redirect("/OrchardLocal/modules/HelloWorld/default.aspx");
    }

After storing the Orchard Services in Session,Redirection to WebForms Page: default.aspx is done. This does redirects successfully.

The Problem is that when I access the above Session["OrchardCMServices"] in my ASPX page, the ContentManager is disposed and I cannot query using GET methods. The Error is: Object Disposed

How can I Disable/Prevent the Disposing of the Session Objects while Redirecting from MVC to Asp.NET ? I didn't find any modules settings in web.config of the MVC App.

Is there any way to make the Orchard Services ( ContentManager) available on Web Forms ?

enter image description here


Solution

  • Services in Orchard have different scopes depending on their purpose. IOrchardServices has a lifetime scope of request, so it will be destroyed at the end of the request.

    Request: a dependency instance is created for each new HTTP request and is destroyed once the request has been processed. Use this by deriving your interface from IDependency. The object should be reasonably cheap to create. http://docs.orchardproject.net/Documentation/How-Orchard-works

    And you can see that IOrchardServices is an IDependency.

    public interface IOrchardServices : IDependency {...
    

    So you shouldn't try to store the service in the current session. If you need to access the service in a view you can resolve it using the work context like this:

    var orchardServices = WorkContext.Resolve<IOrchardServices>();
    

    Or if your module has a controller that is rendering the page you can inject the dependency as normal.