Http.EnumRouteContraint must implement System.Web.Routing.IRouteConstraint - asp.net-web-api

I'm using AttributeRouting in my Web API project. I've installed the AttributeRouting for Web API. I want to define an Enum route constraint so I setup my AttributeRoutingHttpConfig config as follows:
using System.Reflection;
using System.Web.Http;
using AttributeRouting.Web.Http.Constraints;
using AttributeRouting.Web.Http.WebHost;
using MyProject.Data.Models;
[assembly: WebActivator.PreApplicationStartMethod(typeof(PhantasyTour.AttributeRoutingHttpConfig), "Start")]
namespace MyProject
{
public static class AttributeRoutingHttpConfig
{
public static void RegisterRoutes(HttpRouteCollection routes)
{
routes.MapHttpAttributeRoutes(
config =>
{
config.AddRoutesFromAssembly(Assembly.GetExecutingAssembly());
config.InlineRouteConstraints.Add("ListType", typeof(EnumRouteConstraint<ListType>));
});
}
public static void Start()
{
RegisterRoutes(GlobalConfiguration.Configuration.Routes);
}
}
}
When I fire up my application I immediately receive the following error:
The constraint "AttributeRouting.Web.Http.Constraints.EnumRouteConstraint`1[[MyProject.Data.Models.ListType, MyProject.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" must implement System.Web.Routing.IRouteConstraint
I've looked at the source code for the AttributeRouting.Web.Http.Constraints.EnumRouteConstraint and confirmed that it implements IHttpRouteConstraint which presumably is the WebAPI equivalent of IRouteConstraint in the MVC namespace.
Does anyone know what I'm doing wrong and how I can get this working?
UPDATE:
I attempted to create a completely blank Web Application and add only WebAPI and AttributeRouting for WebAPI references. Despite having absolutely no references to MVC assemblies, I still receive the same error message. I did discover however that there is another EnumRouteConstraint found in the AttributeRouting.Web.Constraints namespace which works perfectly. It doesn't appear to be MVC specific since it is located in the Core AttributeRouting assembly. I would love to know why there are two different EnumRouteConstraint classes when only one of them works. But that is a question for another time.

It is interesting that the exception you get refers to the MVC interface from the namespace System.Web.Routing.
I would take it as a clue and look at all the references in your project, any place in the config where MVC Routes and Http Routes could have been mixed up.
If possible and if you have any at all, try removing all references to MVC (or System.Web.Routing for a start), and MVC flavour of attribute routing (if it's a separate dll).

Related

Use SignalR along with ASP.Net WebAPI

I read a lot about SignalR and wondering about how to use it with ASP.NET WebAPI. It seems that the WebAPI route config made the SignalR connection not able to connect to Hub and I don't know how to set up correctly to make this 2 things work together.
The two should not conflict if you have not configured the webapi framework to override some of SignalR default routes like ~/signalr/hubs
[assembly: OwinStartup(typeof(SignalRConfig))]
namespace MyApp.App_Start
{
public static class SignalRConfig
{
public static void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
Thats what you need for signalr to hook up, and then include the client side scripts
#Scripts.Render("~/signalr/hubs") and #Scripts.Render("~/Scripts/jquery.signalR-{version}.js")
Here is an example were I use them together
https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/tree/master/SignalR.EventAggregatorProxy.Demo.MVC4

Web API Self hosting from a test assembly

I'm currently evaluating WebAPI and NancyFx for a new project about to start. I've managed to get Nancy to self host from a test assembly (by itself it uses asp.net hosting).
Is there any way to do the same with Web API? I would like to keep the web api project hosted on IIS, but i would like to spin it up from my test assembly, so i can run tests against it.
I have found some blogposts on how to use Autofac to scan controllers from another assembly (seems a little backwards only to get hosting from another assembly to work, but if it can be done, i guess that would be an option), but i would like to keep using Structuremap ioc for this project.
Managed to get it working with help from Mark Jones link. This is what i ended up with in my test assembly.
private static HttpSelfHostServer _server;
[BeforeTestRun]
public static void Setup()
{
var config = new HttpSelfHostConfiguration(Settings.TestUri);
WebApiConfig.Register(config); //map routes
IocConfig.Bootstrap(config); //configure dependency injection
_server = new HttpSelfHostServer(config);
_server.OpenAsync().Wait();
}
[AfterTestRun]
public static void TearDown()
{
_server.CloseAsync().Wait();
}

Automapping mapping lost when using it

I have this a web application and a dll which I use in that application. Both applications have their own automapper configurations.
I call both both configurations in global.asax but somehow the mapping in the dll is lost when I actually go to use it. I get an exception that mapping is not defined.
What do you think could be the reason?
In my MVC3 application, I have this AutoMapperConfiguration class.
class AutoMapperConfiguration {
Mapper.CreateMap<ClassA, classB>();
}
I have a separate dll named sep.dll which also has
class AutoMapperConfiguration {
Mapper.CreateMap<classC, classD>();
}
In my global.asax, I have this code in Application_start:
AutoMapperConfiguration.Configure();
sep.AutoMapperConfiguration.Configure();
Somewhere in my web application, I do
Mapper.Map(objC, new ObjOfClassD()); // mapping not found exception,
if I do this, it works
sep.AutoMapperConfiguration.Configure();
Mapper.Map(objC, new objD());

AutoFac for Window Phone 7 - IContainter.Resolve extension method is not found

I'm building a fun application for Windows Phone 7. I'm using MVVM pattern and AutoFac for resolving the dependencies. I have got a classes AutoFacConfiguration holding a property as below
public static IContainer Container { get; private set; }
I try to resolve one of the registered types in some other part of the application like below
AutoFacConfiguration.Container.Resolve<IExpenseRepository>()
But this is not compiling. The compiler says Container does not have Resolve method. I know that IContainer derives from IComponentContext and an extension method with following signature does existing in the AutoFac assembly
public TService Resolve<TService>(this IComponentContext context)
Am I missing something here? I have just referenced the AutoFac.dll in my porject. This is downloaded from autofac site.
Have you added the relevant namespace as a using directive? For example, if the extension method is in class Foo.Bar.Baz, you should have:
using Foo.Bar;
in the source file that's trying to use the extension method.

Testing If a class is being activated using WebActivator and if add a IModelBinder to ModelBinderProviders.BinderProviders.

Dear fellows from Stack Exchange.
I'm trying to test if my Custom Model Binder is being added to the ModelBinderProviders.BinderProviders collection.
I decided to activate this through WebActivator, to avoid messing global.asax,
Everything works fine, but the Test:
I tried using the WebActivator.ActivationManager.Run() method, but my things weren't loaded.
I've something like this in my test:
[TestMethod]
public void TemplateModelBinderProvider_Should_Be_Registered_In_BinderProviders()
{
WebActivator.ActivationManager.Run();
IModelBinderProvider templateModelBinderProvider = ModelBinderProviders.BinderProviders.
Where(x => x is TemplateModelBinderProvider).
FirstOrDefault();
Assert.IsNotNull(templateModelBinderProvider);
}
And this is my app_Start class:
[assembly: WebActivator.PreApplicationStartMethod(typeof(MVC.App_Start.MVCBindings), "Start")]
namespace MVC.App_Start
{
public static class MVCBindings
{
public static void Start()
{
ModelBinderProviders.BinderProviders.Add(new TemplateModelBinderProvider());
}
}
}
Sorry you have problems with the piece of code I wrote.
I don't have access to the source code right now but will take a look in the evening (UK time).
Do you think you could send me your solution so I could replicate it locally? My email is jkonecki at gmail.com
UPDATE
I have received your source code but unfortunately it contains references to libraries I cannot obtain so I cannot compile it.
I have created a separate solution (emailed to you) with MVC3 web app and unit test projects that uses your custom model binder provide. There are two tests that prove that WebActivatorManager.Run method properly registers a custom provider.
Try debugging your unit test to make sure that Run method calls your static Start method.
WebActivator source code is here - you might want to get it and step through.

Resources