0

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" 
} 
2
  • can you show the request that you are sending? Commented Jul 23, 2015 at 7:59
  • 192.168.1.129/myproject/api/device/123456/app/appname JSON: { "applicazione" : "Gestione Interventi", "hashImei" : "123123121323123121", "modello" : "Nexus 5", "pushNotificatioToken" : "oifjwfijowfjfoiwjrgfoirwj42rohfoifrj", "sistemaOperativo" : "ANDROID", "versione" : "LOLLIPOP_MR1" } Commented Jul 23, 2015 at 8:39

1 Answer 1

1

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();
    }

    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Stupid me. I missed it. Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.