Question

I have an Web-Api project with controllers and all the necessary stuff, which I want to host as Windows Service. I've created new project, and added there a WindowsService and ServiceInstaller items, so my solution looks like this:

Solution structure

My configuration is:

private HttpSelfHostServer _server;
    private readonly HttpSelfHostConfiguration _config;
    public const string ServiceAddress = "http://localhost:333";

    public WebApiService()
    {
        InitializeComponent();

        _config = new HttpSelfHostConfiguration(ServiceAddress);

        // Set our own assembly resolver where we add the assemblies we need
        CustomAssembliesResolver assemblyResolver = new CustomAssembliesResolver();
        _config.Services.Replace(typeof(IAssembliesResolver), assemblyResolver);

        _config.Routes.MapHttpRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = RouteParameter.Optional });
    }

public class CustomAssembliesResolver : DefaultAssembliesResolver
    {
        public override ICollection<Assembly> GetAssemblies()
        {
            ICollection<Assembly> baseAssemblies = base.GetAssemblies();

            List<Assembly> assemblies = new List<Assembly>(baseAssemblies);

            var controllersAssembly = Assembly.LoadFrom(@"D:\Regula\WebApiService\WebApiService\bin\WebApiService.dll");

            baseAssemblies.Add(controllersAssembly);

            return assemblies;
        }
    }

I've tried to follow this thread suggestions, but it did not help me - I'm still getting:

No type was found that matches the controller named 'Home'.

Basically, I' trying to call HomeController located in WebApiService project from WebApiHost project.

Thanks in advance!

Was it helpful?

Solution

MapHttpRoute is for controllers, derived from ApiController only. HomeController is derived from Controller, so it can't be mapped in this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top