为什么存储库为空

时间:2019-01-24 11:03:30

标签: c# asp.net-core

我正在制作API,并且正在进行API调用。
我创建了一个名为IPartnerRepository
的存储库 当我运行呼叫时,它会给出nullexception错误。
调试代码时,我看到IPartnerRepository为空。
我的Visual Studio没有显示任何错误或错误。
为什么我的程序找不到那个存储库?为什么为空?

这是我的代码:

public IPartnerRepository PartnerRepository { get; set; } // = null

[RequiresAnyRole(Roles.superadmin, Roles.admin)]
public async Task<PartnerListResponse> Get(PartnerListRequest request)
{
   var items = await PartnerRepository.GetAllAsync();
   return new PartnerListResponse
   {
      Items = items
   };
}

IPartnerRepository

using System;
using DTO = Ringdesk.Api.ServiceModel.DTO;

namespace Ringdesk.Api.ServiceModel.Interfaces
{
   public interface IPartnerRepository : IDtoRepository<DTO.Partner, 
      DTO.PartnerForInsert, DTO.PartnerForUpdate>
   {
   }
}

Partner.cs

using ServiceStack.Model;
using Ringdesk.Api.ServiceModel.Attributes;

namespace Ringdesk.Api.ServiceModel.DTO
{
    public class Partner : IHasId<int>
    {
        public int Id { get; set; }
        public string Guid { get; set; }
        [PropertyValue("name")]
        public string Name { get; set; }
    }

    public class PartnerForUpdate : IHasId<int>
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class PartnerForInsert
    {
        public string Name { get; set; }    
    }
}

the working code with customers

the partners code that goes wrong

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ServiceStack;
using Ringdesk.Api.Web.Jobs;
using NLog.Extensions.Logging;
using NLog.Web;

namespace Ringdesk.Api.Web
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOrchardCms();
            //services.AddMvc();
        }

        public class StartupSettings
        {
            public bool RunJobs { get; set; }
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime lifetime)
        {
            var startupSettings = Configuration.GetSection("startupSettings").Get<StartupSettings>();

            if (startupSettings == null || startupSettings.RunJobs)
            {
                var quartz = new QuartzSetup();
                lifetime.ApplicationStarted.Register(quartz.Start);
                lifetime.ApplicationStopping.Register(quartz.Stop);
            }

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddNLog();
            env.ConfigureNLog("nlog.config");
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();
            var appSettings = new NetCoreAppSettings(Configuration);
            app.UseServiceStack(new AppHost(appSettings));
            app.UseOrchardCore();

            //app.UseMvc(routes =>
            //      {
            //        routes.MapRoute(
            //            name: "default",
            //            template: "{controller=Home}/{action=Index}/{id?}");
            //      });
        }
    }
}

2 个答案:

答案 0 :(得分:1)

通过将PartnerRepository添加到服务模块中,我对其进行了修复!

container.RegisterAs<PartnerRepository, IPartnerRepository>();
container.Register<IDtoRepository<Db.PartnerProperty, Db.PartnerProperty, Db.PartnerProperty>>(c =>
{
   var repository = new DtoRepository<Db.PartnerProperty, Db.PartnerProperty, Db.PartnerProperty, Db.PartnerProperty>();
   c.AutoWire(repository);
   return repository;
});

CustomerRepository中已经存在

就像@Croxy一样,我忘记为CustomerRepository内的ServiceModule分配一个值

答案 1 :(得分:-1)

这是运行时错误,由逻辑错误引起。 Visual Studio抱怨编译器错误。

有诸如ReSharper之类的工具,它们在编码时为开发人员提供了对开发人员友好的反馈,例如variable may be null,但是您不能期望从Visual Studio或任何其他开发环境/工具中总能找到有关逻辑的建议错误/运行时错误。

在您的情况下,出现此问题是因为您声明了public IPartnerRepository PartnerRepository { get; set; }但从未将其设置为实例。

实例化可以通过多种方式完成,但最简单和常见的方法是构造函数初始化

class Something
{
     Something()
     {
          PartnerRepository = new PartnerRepository(....);
     }

     // then your methods here
}