UrlHelper / webapi

113 views
Skip to first unread message

arom...@gmail.com

unread,
Feb 18, 2015, 2:17:49 PM2/18/15
to aut...@googlegroups.com
I can't get the UrlHelper to work in the webapi (owin).

In Startup, I'm calling: AutofacConfig.RegisterDependencies(app);

public static void RegisterDependencies(IAppBuilder app)
{
var builder = new ContainerBuilder();
var svcAssembly = typeof(HomeService).Assembly;
var repoAssembly = typeof(RepositoryBase).Assembly;
var apiAssembly = Assembly.GetExecutingAssembly();
var config = GlobalConfiguration.Configuration;
builder.RegisterHttpRequestMessage(config);

builder.RegisterType(typeof(ReportToMeDataContext))
.AsSelf();

builder.Register(c => new LinkFactory(c.Resolve<HttpRequestMessage>())).InstancePerRequest();

builder
.RegisterType(typeof(ReportToMeDataContext))
.AsSelf()
.InstancePerLifetimeScope();

builder
.RegisterGeneric(typeof(ReportToMe.Data.Repository<>))
.As(typeof(IRepository<>));

builder
.RegisterType(typeof(UnitOfWork))
.As(typeof(IUnitOfWork));

builder.RegisterAssemblyTypes(svcAssembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces();

builder.RegisterAssemblyTypes(repoAssembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces();





builder.RegisterApiControllers(apiAssembly);

builder.RegisterWebApiFilterProvider(config);



var container = builder.Build();

GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

app.UseAutofacMiddleware(container);
app.UseWebApi(config);


I've tried a veriety of options with this in the controller, and in separate classes. The controller is basically:

[RoutePrefix("values")]
public class ValuesController : ApiController
{
//LinkFactory _lf;
//public ValuesController(LinkFactory lf)
//{
// this._lf = lf;
//}

[Route("", Name="valueslist")]
public IEnumerable<string> Get()
{
//UrlHelper h = Request.GetUrlHelper();
//string s2 = h.Link("DefaultApi", new { });
//UrlHelper h = new UrlHelper(Request);
//string s3 = h.Link("DefaultApi", new { });
//Link s4 = _lf.GetUrl("valueslist", new { });
//var helper = ActionContext.Request.GetUrlHelper();
//string s5 = helper.Link("value", new { id = 1});

UrlHelper helper = Request.GetUrlHelper();
string s6 = helper.Link("value", new { id = 1 });

return new string[] { "value1", "value2" };
}

[Route("{id}", Name="value")]
public string Get(int id)
{
return "value";
}


An exception of type 'System.NotImplementedException' occurred in System.Web.dll but was not handled in user code

Additional information: The method or operation is not implemented.


If I take out autofac, it starts working fine. Thanks.
A

Travis Illig

unread,
Feb 18, 2015, 2:29:28 PM2/18/15
to aut...@googlegroups.com, arom...@gmail.com
If you're working in OWIN, you shouldn't have any references to GlobalConfiguration.Configuration. Docs on that here: http://autofac.readthedocs.org/en/latest/integration/webapi.html#owin-integration

You also appear to be missing the registration for Autofac Web API integration.
app.UseAutofacWebApi(config);

Once you've updated to working on a strictly OWIN stack given the docs linked above, give it a shot.

If it's still not working, please post the full stack trace and everything for the exception so we can better assist you.

Good luck!
-T

arom...@gmail.com

unread,
Feb 18, 2015, 2:47:37 PM2/18/15
to aut...@googlegroups.com, arom...@gmail.com
Thanks for that ... fixed... you're a genius.
Reply all
Reply to author
Forward
0 new messages