Web 福彩12选5走势图 DbContext与依赖项注入?

Raysefo

知名会员
已加入
2019年2月22日
留言内容
192
编程经验
10+
您好,
我想知道如何在asp.net Web 福彩12选5走势图中将DbContext与依赖项注入一起使用。我得到类型'EPINMiddleWare福彩12选5走势图.Controllers.InitiatesController'没有默认的构造函数。

这是我的示例代码:
C#:
public class EPINMiddleWare福彩12选5走势图Context : DbContext
    {
        
        public EPINMiddleWare福彩12选5走势图Context() : base("name=EPINMiddleWare福彩12选5走势图Context")
        {
        }
    
        public DbSet<InitiateRequest> InitiatesRequests { get; set; }
        public DbSet<InitiateResponse> InitiateResponses { get; set; }
    }

这是我的控制器:
C#:
[RoutePrefix("api/v2/pin")]
    public class InitiatesController : ApiController
    {
        private EPINMiddleWare福彩12选5走势图Context context;

        public InitiatesController(EPINMiddleWare福彩12选5走势图Context context)
        {
            this.context = context;
        }


        // POST: api/Game
        [HttpPost, Route("initiation")]
        public async Task<IHttpActionResult> PostInitiate(InitiateRequest initiate)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            context.InitiatesRequests.Add(initiate);
            await context.SaveChangesAsync();

            HttpClient httpClient = new HttpClient();


            HttpContent content = new StringContent(
                JsonConvert.SerializeObject(initiate),
                Encoding.UTF8,
                "application/json"
            );


            HttpResponseMessage response =
                await httpClient.PostAsync("//test.com/purchaseinitiation", content);

            string htmlResponse = await response.Content.ReadAsStringAsync();

            return Ok(htmlResponse);
        }

        protected override void Dispose(bool disposing)
        {
            context.Dispose();
            base.Dispose(disposing);
        }
    }
 

Raysefo

知名会员
已加入
2019年2月22日
留言内容
192
编程经验
10+
I am using basic authentication in my 福彩12选5走势图. I use Unity as DI container. I wonder if this registration is enough for User Validation? Or should I add to 寄存器Type<UnitOfWork>(new HierarchicalLifetimeManager()) it?

C#:
public static class UnityConfig
    {
        #region Unity Container
        private static Lazy<IUnityContainer> container =
          new Lazy<IUnityContainer>(() =>
          {
              var container = new UnityContainer();
              寄存器Types(container);
              return container;
          });

        /// <summary>
        /// Configured Unity Container.
        /// </summary>
        public static IUnityContainer Container => container.Value;
        #endregion

      
        public static void 寄存器Types(IUnityContainer container)
        {
            
            container.RegisterType<IGameServices, GameServices>().RegisterType<UnitOfWork>(new HierarchicalLifetimeManager());
            container.RegisterType<IUserValidate, UserValidate>();
            
          
        }
    }

这是用户验证:
C#:
public class UserValidate : IUserValidate
    {
        private readonly UnitOfWork _unitOfWork;

        /// <summary>
        /// Public constructor.
        /// </summary>
        public UserValidate(UnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// Public method to authenticate user by user name and password.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<bool> Login(string userName, string password)
        {
            var user = await _unitOfWork.UserRepository.Get(u => u.userName.Equals(userName, StringComparison.OrdinalIgnoreCase) && u.password == password);
            if (user == null) return false;
            return true;
        }
    }
 
最佳 底部