Autofac.Webapi not support webapi 2?

3,353 views
Skip to first unread message

fuhong...@gmail.com

unread,
Aug 27, 2013, 9:40:35 PM8/27/13
to aut...@googlegroups.com
when i use autofac to webapi 2,get a error message "Could not load file or assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies", the reason is System.Web.Http version is 5.0.0.0 now. how to fix this problem? thanks

jb...@terryberry.com

unread,
Aug 28, 2013, 4:45:43 PM8/28/13
to aut...@googlegroups.com, fuhong...@gmail.com
On Tuesday, August 27, 2013 9:40:35 PM UTC-4, fuhong...@gmail.com wrote:
> when i use autofac to webapi 2,get a error message "Could not load file or assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies", the reason is System.Web.Http version is 5.0.0.0 now. how to fix this problem? thanks

I believe that you just need to add this to your web.config:

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31BF3856AD364E35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>

Cecil Phillip

unread,
Sep 1, 2013, 6:44:10 PM9/1/13
to aut...@googlegroups.com, fuhong...@gmail.com, jb...@terryberry.com
This binding redirect works, but when I try to register a type:

 builder.RegisterType<InMemoryRepository>().As<IArkeRepository>().InstancePerApiRequest();

I get the following TypeLoadExeption:

Inheritance security rules violated while overriding member: 'Autofac.Integration.WebApi.AutofacWebApiDependencyResolver.BeginScope()'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.

I'm currently used Microsoft.AspNet.WebApi.Core 5.0.0-rc1 with Autofac.WebApi 3.1.0

Alex Meyer-Gleaves

unread,
Sep 2, 2013, 9:22:52 PM9/2/13
to aut...@googlegroups.com
I have pushed a prerelease package for RC1 that sorts out this issue.



--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To unsubscribe from this group and stop receiving emails from it, send an email to autofac+u...@googlegroups.com.
To post to this group, send email to aut...@googlegroups.com.
Visit this group at http://groups.google.com/group/autofac.
For more options, visit https://groups.google.com/groups/opt_out.

Cecil Phillip

unread,
Sep 3, 2013, 12:03:19 AM9/3/13
to aut...@googlegroups.com
Thanks Alex. This resolved my issues. I'll report any issues I find going forward.
BTW... why the change in package naming?

Alex Meyer-Gleaves

unread,
Sep 3, 2013, 12:38:02 AM9/3/13
to aut...@googlegroups.com

Great news. Thank Cecil.

With the MVC integration package the first release was just called Autofac.Mvc, and with each new release we created a new package name. There are two main reasons for changing the package name with each major release, with the same reasoning being applied to MVC and Web API.

The first is because creating a project with a previous version of MVC/Web API would require the user to get a specific version of the integration package; the last version that worked with that earlier version. With the integration package version numbers being tied to the core Autofac version (currently 3.0 series) determining exactly what version that is would be difficult, and even if you could figure it out would be a PITA. In the case of a project upgrade to a new MVC/Web API version uninstalling one package and grabbing the latest of another is easy enough and a one-time only exercise.

The second is if a bug is found in the integration for a previous version of MVC/Web API we need to be able to push out an updated package to correct the issue. If the latest package is targeting a different major release of MVC/Web API that would be difficult. It is also common for MVC to target a new version of the framework with each release making things even more confusing.

I wasn't sure if the second release of Web API would be 2.0 or if they would do the EF trick and jump straight to 5.0 to match the MVC release. It looks like they decided to jump the version number straight to 5.0 so that is what the NuGet package is called.

Hopefully that makes sense. It seems to have worked well in the past so I have kept going with that approach.

Cheers,

Alex.

nguyenth...@gmail.com

unread,
Jun 20, 2014, 5:48:18 AM6/20/14
to aut...@googlegroups.com
Vào 11:38:02 UTC+7 Thứ ba, ngày 03 tháng chín năm 2013, Alex Meyer-Gleaves đã viết:
Hi, thanks for your new package. I have got the same issue and this resolved my issue. But, I have got another issue

"The type Autofac.Integration.WebApi.AutofacWebApiDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator.
Parameter name: commonServiceLocator"

Could you please give me your thought ?

Travis Illig

unread,
Jun 20, 2014, 9:44:58 AM6/20/14
to aut...@googlegroups.com, nguyenth...@gmail.com
Somewhere in your code you're trying to set the common service locator to be an AutofacWebApiDependencyResolver - like you're trying to use the same resolver instance for both Web API and Common Service Locator. The error message says it all - you can't do that. You need to use the Autofac.Extras.CommonServiceLocator package and share the *container* rather than the *resolver*.
Reply all
Reply to author
Forward
0 new messages