配置OAuth后Unity不会注入依赖关系?

时间:2016-11-29 14:09:09

标签: c# oauth dependency-injection asp.net-web-api2 unity-container

嗨大家我是Oauth和Identity的新手,所以我尝试使用OAuth配置基于令牌的身份验证,我能够生成令牌,但奇怪的是我配置OAuth之后我的Unity依赖关系停止工作并且它没有注入依赖性不再给出异常"对象实例未设置为对象的实例"

运行应用程序时运行的新的startup.cs类看起来像这样。

public class Startup
{
    public void Configuration(IAppBuilder app)
    {  
        HttpConfiguration config = new HttpConfiguration();
        WebApiConfig.Register(config);         
        ConfigureOAuth(app);
        app.UseWebApi(config);
        UnityConfig.RegisterComponents();

    }

我知道它必须对这些配置的排序做一些事情,但我尝试了不同的其他可能的订单,但根本没有运气。 WebApiConfig.Register(config)看起来像这样

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();            
        var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

    }

现在不起作用的部分是我在之前使用良好的控制器中使用依赖...

例外情况如下

  

对象引用未设置为对象的实例。

at MIS.WebAPI.Controllers.ProductController.GetAll() in E:\\MIS (2)\\MIS\\MIS.WebAPI\\Controllers\\ProductController.cs:line 27
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"

这是ProductController.cs类

namespace MIS.WebAPI.Controllers
{
[EnableCors(origins:"*",headers:"*",methods:"*")]
public class ProductController : ApiController
{
    // GET: api/Product
    [Dependency]
    public IProductRepository _productRepo { get; set; }
    [Route("api/getAllProduct")]
    public Response GetAll()
    {
        try
        {
            var result = _productRepo.GetALl();
            return Response.Ok(result);
        }
        catch (Exception ex)
        {
            return Response.BadRequest(ex.Message);
        }
    }

0 个答案:

没有答案