Reputation: 47921
I'm getting a "could not load type MyApp.Api.App from assembly MyApp.Api" runtime error in my c# mvc project that is referencing an f# web api library. The c# project MyApp.Web has a project reference to the F# project MyApp.Api and has no compilation errors. What could be the issue?
App.fs in the project MyApp.Api
namespace MyApp.Api
open System
open System.Web
open System.Web.Mvc
open System.Web.Routing
open System.Web.Http
open System.Data.Entity
open System.Net.Http.Headers
open System.Net.Http.Headers
type Route = { controller : string; action : string; id : UrlParameter }
type ApiRoute = { id : RouteParameter }
type App() =
inherit System.Web.HttpApplication()
static member RegisterGlobalFilters (filters:GlobalFilterCollection) =
filters.Add(new HandleErrorAttribute())
static member RegisterRoutes(routes:RouteCollection) =
routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" )
routes.MapHttpRoute( "DefaultApi", "api/{controller}/{id}",
{ id = RouteParameter.Optional } ) |> ignore
routes.MapRoute("Default", "{controller}/{action}/{id}",
{ controller = "Home"; action = "Index"; id = UrlParameter.Optional } ) |> ignore
member this.Start() =
AreaRegistration.RegisterAllAreas()
App.RegisterRoutes RouteTable.Routes
App.RegisterGlobalFilters GlobalFilters.Filters
And my global.asax.cs in MyApp.Web
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using MyApp.Api;
namespace MyApp.Web
{
public class WebApiApplication : MyApp.Api.App// System.Web.HttpApplication
{
protected void Application_Start()
{
base.Start();
}
}
}
Upvotes: 3
Views: 1161
Reputation: 267
You are registering your api route incorrectly. While the APIs look similar, they are not. You need to register your Web API route using the HttpConfiguration
instance:
GlobalConfiguration.Configuration.Routes.MapHttpRoute("", "", ...)
You are trying to map a Web API route into the MVC RouteTable
. I'm actually surprised you don't get a compilation error.
So the above appears to not be the case. I must not have included an appropriate namespace when I tried before without pulling in Dan Mohl's project template.
You've subclassed your MyApp.Api.App
type in Global.asax.cs
. Dan's template doesn't include this. Instead, his template modifies the markup in Global.asax
as follows:
<%@ Application Inherits="MyApp.Api.App" Language="C#" %>
<script Language="C#" RunAt="server">
protected void Application_Start(Object sender, EventArgs e) {
base.Start();
}
</script>
This seems to work just fine. I also got the following to work:
<%@ Application Inherits="MyApp.Web.WebApiApplication" Language="C#" %>
<!-- The following seems to be optional; just an extra, duplicate event handler.
I was able to run the app with this script and without. -->
<script Language="C#" RunAt="server">
protected void Application_Start(Object sender, EventArgs e) {
base.Start();
}
</script>
Note that you need the full namespace, not just the type name. If that works correctly, then I think more of the code is necessary, as I can't find anything else that is wrong.
Upvotes: 2