Max Bertoli
Max Bertoli

Reputation: 614

Property null in web api controller when using Entity Framework 5 and Oracle

i have a problem in a new web api controller when i try to use EF5 with Oracle.

My controller:

    public class DeviceV1Controller : ApiController
{
    private readonly IDevice _repository;

    public DeviceV1Controller()
    {
        IDevice _repository = new EFDeviceRepository();
    }

    [Route("api/Device/{hashImei}/app/{nome}")]
    public HttpResponseMessage Post(string hashImei, string nome, [FromBody] DeviceInfo deviceInfo)
    {
        _repository.Add(deviceInfo);
        return Request.CreateResponse(HttpStatusCode.OK);

    }
}

_repository is correctly bound in the constructor, but entering the Post api this variable become null and i get this error:

{
message: "An error has occurred."
exceptionMessage: "object reference not set to an instance of an object."
exceptionType: "System.NullReferenceException"
stackTrace: " in MpssApiRest.Controllers.DeviceV1Controller.Post(String hashImei, String nome, DeviceInfo deviceInfo) in c:\SVILUPPO\MpssApiRest\MpssApiRest\MpssApiRest\Controllers\DeviceV1Controller.cs:riga 28 in lambda_method(Closure , Object , Object[] ) in System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) in System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) in System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) --- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione --- in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext() --- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione --- in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() --- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione --- in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()"
}

EFDeviceRepository concrete class is:

    public class EFDeviceRepository : IDevice
{
    private readonly EntityDevice ctx;

    public EFDeviceRepository()
    {
        ctx = new EntityDevice();
    }

    public void Add(Models.V1.DeviceInfo deviceInfo)
    {
        EntityDevice ctx = new EntityDevice();
        MPSS_APP_DEVICE device = new MPSS_APP_DEVICE();
        device.HASHIMEI = deviceInfo.HashImei;
        ctx.MPSS_APP_DEVICE.Add(device);

        ctx.SaveChanges();
    }
}

Thanks


EDIT: Sample Web Request (retrieved from the comments)

Ip Address: 192.168.1.129
Url: /myproject/api/device/123456/app/appname 
JSON: 
{ 
    "applicazione" : "Gestione Interventi", 
    "hashImei" : "123123121323123121", 
    "modello" : "Nexus 5", 
    "pushNotificatioToken" : "oifjwfijowfjfoiwjrgfoirwj42rohfoifrj",
     "sistemaOperativo" : "ANDROID", "versione" : "LOLLIPOP_MR1" 
} 

Upvotes: 0

Views: 1652

Answers (1)

Parth Shah
Parth Shah

Reputation: 2140

The reason why _repository is null in your Action is because you are not initializing it in your constructor. Instead you have declared and initialized a local variable of the same name in your constructor!

public class DeviceV1Controller : ApiController
{
    private readonly IDevice _repository;

    public DeviceV1Controller()
    {
        _repository = new EFDeviceRepository();
    }

    // ...
}

Upvotes: 1

Related Questions