0

I am working with the web application and I have a class which have all the business logic which implement Interface. I am trying to referenced Interface and class in startup.cs and trying to initialise in Controller. My business logic class is like this:

 public class User_MasterDataAccesss: IUserMasterRepository
 {
   .... business logic
   public int validate(string name)
   {
     ...... doing something
   }
 }

I have an interface which have definition for the validate function like this

public interface IUserMasterRepository
{
    int validate(string name);
}

I am trying to bind the interface in startup.cs like this

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
    }
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.           
       // services.AddMvc();

        // Add application services.
        services.AddSingleton<IUserMasterRepository, User_MasterDataAccesss>();

    }
}

and I am trying to initialise the interface in controller like this

public class AuthenticationController : Controller
{
    private  IUserMasterRepository UserMasterRepository;
    public AuthenticationController() { }
    public AuthenticationController(IUserMasterRepository userMasterRepository)
    {
        this.UserMasterRepository = userMasterRepository;
    }
    ......... action result actions which is using the UserMasterRepository 

 }

My problem is as follows:

  1. Constructor is not executing and as a result UserMasterRepository is null when accessing from action.
  2. services.AddMvc(); in startup.cs is doesnot exist
  3. I know I can achieve my requirement by using Ninject but I want to use startup.cs file for that matter
  4. I do not want to use new key word in controller.

How can I achieve this? Where I am wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
Iswar
  • 2,211
  • 11
  • 40
  • 65
  • If `services.AddMvc()` is not available it sounds like you have a reference issue. Make sure you have installed the Nuget package `Microsoft.AspNet.Mvc 6.0.0-rc1-final`. And make sure you have the correct `using` statements. – Brad Mar 18 '16 at 07:21
  • Package 'Microsoft.AspNet.Mvc.5.2.3' already exists in project and trying to add the package gives error Install-Package : Unable to find a version of 'Microsoft.AspNet.Razor' that is compatible with 'Microsoft.AspNet.Razor.Runtime 4.0.0-rc1-final constraint: Microsoft.AspNet.Razor (≥ 4.0.0-rc1-final)', – Iswar Mar 18 '16 at 07:46
  • What about your `using` statements? Nothing will work until you can get `services.AddMvc()` working in the `ConfigureServices()` method and `app.UseMvc()` in the `Configure()` method. I have a simple project that works with these `using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.Extensions.DependencyInjection;` – Brad Mar 18 '16 at 07:54

1 Answers1

0

Installing ASP.NET 5 runtime and tooling solve my problem.

Iswar
  • 2,211
  • 11
  • 40
  • 65