Can I use ServiceStack on two different MVC.NET sites running in a common Sitecore environment?

We deliver an ASP.NET MVC application built with Sitecore to a client who deploys an application to install on multiple Sitecore sites / tenants.

We are actively using ServiceStack, as in most of our projects, but have encountered a problem now. Another application in Sitecore setup also uses ServiceStack. This causes an error:

[InvalidDataException: AppHostBase.Instance has already been set]

This is really great, because all the files for all the applications in the Sitecore installation are in the same physical folder on the disk. This means that we share the DLL and that’s it.

So, this other project is initialized before ours, and when we try to register our services, we get the above error.

Is there any way around this? Can I somehow register my services on an existing one AppHostBase.Instance?

Two things worth noting:

  • We do not affect the setting of the Sitecore environment.

  • We can work together with another team using ServiceStack if something needs to be done in both projects.

+4
source share
1 answer

Preferably, you should tell ServiceStack all the service assemblies that you want to register with your AppHost constructor , for example:

public class AppHost : AppHostBase
{
    //Tell ServiceStack the name of your app and which assemblies to scan for services
    public AppHost() : base("Hello ServiceStack!", 
       typeof(ServicesFromDll1).Assembly,
       typeof(ServicesFromDll2).Assembly
       /*, etc */) {}

    public override void Configure(Container container) {}
}

But you can dynamically register services outside of ServiceStack with:

HostContext.ServiceController.RegisterService(typeof(MyService));

:

HostContext.ServiceController.
    RegisterServicesInAssembly(typeof(MyService).Assembly);
+2

All Articles