我在单独的项目中有简单的ApiController:
<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
googlinng一段时间之后,我已经设法使用IHttpControllerSelector和IHttpControllerActivator的自定义实现来解决控制器的问题。但现在得到错误:"No action was found on the controller 'Custom' that matches the name 'GetSomething'"
。意味着我必须实现IHttpActionSelector,之后可能还有其他东西......这听起来非常复杂和不合逻辑,因为我不想做任何自定义处理。我有什么不对的提示吗?
答案 0 :(得分:2)
让外部库中的WebAPI控制器看起来非常简单。您需要的只是注册自定义AssembliesResolver。 Mef注册已加载外部dll。
Trick是你不能使用DefaultAssembliesResolver并覆盖函数GetAssemblies()。你必须自己实现IAssembliesResolver。
这是我的代码,它完全符合我的要求:
Mef注册(只是加载扩展的部分):
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类
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
现在我有了新的控制器“自动添加”并使用MEF覆盖/扩展任何基本的WebAPI。