how to hook into orchard services

Jun 9, 2013 at 4:58 PM
Hi guys.
Like the scripting engine.
Can i call orchard services within my c# script ?
Any examples ?
I tried calling on Orchard.WorkContext.CurrentSite.SiteName; but it failed.
Thanks
Yiannis




using System;
using Orchard;

public class DotNetScripting
{
public static dynamic Main()
{
    return Greeting();
}

public static string Greeting()
{
    return Orchard.WorkContext.CurrentSite.SiteName;

}
}
Coordinator
Jun 9, 2013 at 5:53 PM
Hi Yiannis!

These services are not yet available, but it's on the list, coming soon! :)
Coordinator
Jun 9, 2013 at 6:07 PM
But since the scripting engine exposes events, you can write a small event handler and inject the WorkContext yourself: you could use BeforeExecution to add a variable containing the WorkContext to the scope.
Jun 9, 2013 at 6:29 PM
Thanks guys.
I'll check out Piedones tip , see how it goes.
Yiannis.
Jun 9, 2013 at 6:48 PM
I added this line in the DefaultAssemblyLoader class just after the loading of assemblies
public void BeforeExecution(BeforeExecutionContext context)
        {
            var scope = context.Scope;
            scope.LoadAssembly(typeof(string).Assembly); // mscorlib.dll
            scope.LoadAssembly(typeof(Uri).Assembly); // System.dll
            scope.LoadAssembly(typeof(HttpContext).Assembly); // System.Web.dll
            scope.LoadAssembly(typeof(AjaxHelper).Assembly); // System.Web.Mvc.dll
            scope.LoadAssembly(typeof(Shapes).Assembly); // Orchard Core
            scope.LoadAssembly(typeof(WorkContext).Assembly); // Orchard Framework
       
           //yiannis code
            scope.SetVariable("OrchardServices", _orchardServices);
        
         
              
        }
I then tried calling the service from the script like this :
public static string Greeting()
{
    return OrchardServices.WorkContext.CurrentSite.SiteName;

}
but it did'nt work.

Am I missing something ?