質問

Hi I've been searching for a while to get a definitive way of using Unity 3 with a Web.API project, I just seem to be hitting brick walls and getting examples that dont work, I would much appreciate it if somebody could point me to a definitive example as everything out there seem to be outdated. I have a Web.API project newly created I then do install-package Unity.WebAPI, this appears to install unity etc but there is no BootStrapper.cs. but I add UnityConfig.RegisterComponents(); to the WebApiApplication file as instructed. having looked around i then run install-package unity.mvc4 this does give me bootstrapper.cs I then run into major problems as the project does not compile Error 1 'xxxx.Web.API.Areas.HelpPage.XmlDocumentationProvider' does not implement interface member 'System.Web.Http.Description.IDocumentationProvider.GetResponseDocumentation(System.Web.Http.Controllers.HttpActionDescriptor)'

I have tried to implement them members as follows public string GetDocumentation(HttpControllerDescriptor controllerDescriptor) { throw new NotImplementedException(); }
and public string GetResponseDocumentation(HttpActionDescriptor actionDescriptor) { throw new NotImplementedException(); }

i also get Warning 1 Found conflicts between different versions of the same dependent assembly. In Visual Studio, double-click this warning (or select it and press Enter) to fix the conflicts; otherwise, add the following binding redirects to the "runtime" node in the application configuration file:

so I then click the message to update the references run the project and get "Attempt by method 'System.Web.Http.GlobalConfiguration..cctor()' to access field 'System.Web.Http.GlobalConfiguration.CS$<>9__CachedAnonymousMethodDelegate2' failed."}

I really am not sure which way to go? there seems to be so munch out there on unity but nothing actually works, surely I'm missing something?

役に立ちましたか?

解決 2

Thanks for your help, it sort of helped there still seems to be some inconsistency, I now finally got this working through much pain so these are the steps I followed.

make sure you run VS as administrator to avoid package errors

Run Update-Package to ensure all packages are upto date when asked for File 'Areas\HelpPage\Views_ViewStart.cshtml' already exists in project 'ItsSorted.WebApi'. Do you want to overwrite it? [Y] Yes [A] Yes to All [N] No [L] No to All [?] Help (default is "N"):a use A

updatate web.config to stop razor errors

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

and add

<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
 </dependentAssembly>

At this point the app is in a state to have Unity

run Unity install Install-Package Unity.WebAPI

Painfull experience but now working :)

Forgot to mention the framework was not an issue I tried with both versions

他のヒント

Here's how I've implemented Unity in Web API (note: there's no bootstraper). You should have a UnityConfig.cs after you've installed Unity 3. It will be in your App_Start folder and have a RegisterComponents() method. Mine looks like this:

public static void RegisterComponents()
        {
            var container = new UnityContainer();

            // register all your components with the container here
            // it is NOT necessary to register your controllers

            // Conventional registration mapping
            // container.RegisterType<IHomeService, HomeService>();

            // This will register all types with a ISample/Sample naming convention 
            container.RegisterTypes(
                AllClasses.FromLoadedAssemblies(),
                WithMappings.FromMatchingInterface,
                WithName.Default);          

            GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
        }

You'll need to add this to your Global.asax's Application_Start() method:

UnityConfig.RegisterComponents();

If you want to go the conventional mapping route, you'll need to add your mappings to the RegisterComponent() method. If you're following the convention-based approach like Sample.cs implements ISample, then you don't have to register. You can always use both ways together.

Update:

Here are the Unity entries in my packages.config

<package id="Unity" version="3.0.1304.1" targetFramework="net45" />
<package id="Unity.WebAPI" version="5.1" targetFramework="net45" />
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top