1

I have simple ApiController in the separate project:

<Export("Custom", GetType(ApiController)),
PartCreationPolicy(CreationPolicy.NonShared)>
Public Class CustomController
    Inherits ApiController

    Public Function GetSomething() As HttpResponseMessage
        Dim Result As New Something() With {
             .Code = "Code",
             .SomeField = "Something",
             .SomeField2 = 5
            }
        Return Request.CreateResponse(System.Net.HttpStatusCode.OK, Result)
    End Function

End Class

After googlinng for a while I've managed to get to the point where controller gets resolved using custom implementations of the IHttpControllerSelector and IHttpControllerActivator. But now getting Error: "No action was found on the controller 'Custom' that matches the name 'GetSomething'". Means I have to implement IHttpActionSelector and probably something else afterwards... This sounds very complicated and illogical, as I'm not trying to do any custom handling. Any hints where I'm going wrong?

Algis
  • 612
  • 1
  • 8
  • 23
  • Have you tried to decorate the action with the ActionNameAttribute ?. – Pablo Cibraro May 09 '13 at 14:33
  • Out of curiosity, why did you need to implement your own IHttpControllerSelector ? – Darrel Miller May 09 '13 at 15:11
  • Pablo, I did not try, but I will. – Algis May 09 '13 at 15:59
  • Darrel, because that was the only way I've managed to get my custom picked up by the request. But once I started going this route - I understood, that I'm doing something way too complicated. based on the sample [link](http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/) it supposed to be only matter of loading assemblies, but it does not work. I see assembly loaded, but getting 404 – Algis May 09 '13 at 16:08
  • Adding `` does not make any difference. – Algis May 09 '13 at 17:10
  • Darrel, went IHttpControllerSelector because was desperately following other samples :-) It was not necessary indeed. – Algis May 20 '13 at 09:51

1 Answers1

2

To have WebAPI controller in the external library appeared to be extremely simple. All what you need is to have custom AssembliesResolver registered. Mef registration was loading external dll's already.

Trick is that you can't use DefaultAssembliesResolver and override function GetAssemblies(). You have to implement yourself IAssembliesResolver.

Here is my code which gave exactly what I needed:

Mef Registration (just a part where Extensions are loaded):

    Dim ExtensionsCatalog As New DirectoryCatalog(Settings.GetValue("ExtensionsFolder"))
    Dim container As New CompositionContainer(ExtensionsCatalog, defaultCatalogEP)

Global.asax

GlobalConfiguration.Configuration.Services.Replace(GetType(IAssembliesResolver), New Models.CustomAssembliesResolver())

CustomAssembliesResolver class

    Public Class CustomAssembliesResolver
    Implements IAssembliesResolver

    Public Function GetAssemblies() As ICollection(Of Assembly) Implements IAssembliesResolver.GetAssemblies
        Dim assemblies As List(Of Assembly) = AppDomain.CurrentDomain.GetAssemblies().ToList()
        Return assemblies
    End Function

End Class

And now I have new controllers "adding themselves automatically" and using MEF to override/extend any base WebAPIs.

Algis
  • 612
  • 1
  • 8
  • 23