none
Object not set to instance of an object RRS feed

  • Question

  • Here's my function and I'm getting the error: 

    Object not set to instance of an object

    What could be the issue with this function?

             
           [FunctionName("AgentsPost")]
            public static async Task<HttpResponseMessage> Run(
                [HttpTrigger(
                AuthorizationLevel.Anonymous,
                methods: "post",
                WebHookType= "genericJson",
                Route = "agents")]
                HttpRequestMessage req,
                TraceWriter log)
            {
                string content = await req.Content.ReadAsStringAsync();
                return new HttpResponseMessage(HttpStatusCode.OK);
            }




    • Edited by JerryWiltse Friday, August 18, 2017 6:14 PM
    Friday, August 18, 2017 4:27 PM

Answers

  • Hi Jerry,

    It's best to discuss directly on the GitHub issue, and I see you have been doing that already, so I'll leave this one alone :)

    David

    • Marked as answer by JerryWiltse Wednesday, December 5, 2018 2:03 PM
    Tuesday, August 22, 2017 12:32 AM

All replies

  • Do you also get it when running locally? Can you share the full stack?

    thanks,
    David

    Friday, August 18, 2017 5:16 PM
  • Yes, I'm running locally and that's where it happens.  Here is the full stack:

    [8/18/2017 5:30:56 PM] A ScriptHost error has occurred
    [8/18/2017 5:30:56 PM] Exception while executing function: AgentsPost. API.Routing: Object reference not set to an instance of an object.
    [8/18/2017 5:30:56 PM] Exception while executing function: AgentsPost
    [8/18/2017 5:30:56 PM] Exception while executing function: AgentsPost. API.Routing: Object reference not set to an instance of an object.
    [8/18/2017 5:30:56 PM] Function completed (Failure, Id=b4708fdd-3214-4bed-88c6-aa6f147d59a0, Duration=128ms)
    [8/18/2017 5:30:56 PM]
    [8/18/2017 5:30:56 PM] Executed 'AgentsPost' (Failed, Id=b4708fdd-3214-4bed-88c6-aa6f147d59a0)
    [8/18/2017 5:30:56 PM] mscorlib: Exception while executing function: AgentsPost. API.Routing: Object reference not set to an instance of an object.
    [8/18/2017 5:30:56 PM]   Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is 'b4708fdd-3214-4bed-88c6-aa6f147d59a0'
    [8/18/2017 5:30:56 PM] mscorlib: Exception while executing function: AgentsPost. API.Routing: Object reference not set to an instance of an object.
    [8/18/2017 5:30:56 PM] {"id":"8902e3dd-7bba-4481-b3a2-1a8e0e139555","requestId":"2dd35ce4-d3a6-4172-a921-d011993c3bf0","statusCode":500,"errorCode":0,"message":"Exception while executing function: AgentsPost -> Object reference not set to an instance of an object.","errorDetails":"Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: AgentsPost ---> System.NullReferenceException : Object reference not set to an instance of an object.\r\n   at async Appany.Azure.Function.Agent.AgentsPost.Run(HttpRequestMessage req,TraceWriter log)\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync[TReflected,TReturnValue](Object[] arguments)\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker,ParameterHelper parameterHelper,CancellationTokenSource timeoutTokenSource,CancellationTokenSource functionCancellationTokenSource,Boolean throwOnTimeout,TimeSpan timerInterval,IFunctionInstance instance)\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstance instance,ParameterHelper parameterHelper,TraceWriter traceWriter,ILogger logger,CancellationTokenSource functionCancellationTokenSource)\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??) \r\n   End of inner exception\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryExecuteAsync(IFunctionInstance functionInstance,CancellationToken cancellationToken)\r\n   at Microsoft.Azure.WebJobs.Host.Executors.ExceptionDispatchInfoDelayedException.Throw()\r\n   at async Microsoft.Azure.WebJobs.JobHost.CallAsync(??)\r\n   at async Microsoft.Azure.WebJobs.Script.ScriptHost.CallAsync(String method,Dictionary`2 arguments,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.WebScriptHostManager.HandleRequestAsync(FunctionDescriptor function,HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.ProcessRequestAsync(HttpRequestMessage request,FunctionDescriptor function,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.<>c__DisplayClass3_0.<ExecuteAsync>b__0(??)\r\n   at async Microsoft.Azure.WebJobs.Extensions.Http.HttpRequestManager.ProcessRequestAsync(HttpRequestMessage request,Func`3 processRequestHandler,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.ExecuteAsync(HttpControllerContext controllerContext,CancellationToken cancellationToken)\r\n   at async System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.Handlers.SystemTraceHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.Handlers.WebScriptHostHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async System.Web.Http.HttpServer.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)"}
    [8/18/2017 5:30:56 PM] mscorlib: Exception while executing function: AgentsPost. API.Routing: Object reference not set to an instance of an object.

    Friday, August 18, 2017 5:31 PM
  • If I remove the parameter:  "method: "post" and then call the endpoint I no longer get the error above.  It's perhaps something with the syntax.

    Update:  There was no issue with syntax.  Removing this parameter just masked the issue because it caused the method to never get it. 

    I'd like to restrict this function to only "post", so i need to figure it out... but moving forward on troubleshooting. 

    Now the function processes my call and doesn't crash, but it returns 404 not found... WHY?

    Fortunately the local function runtime log shows everything needed to demonstrate why this makes no sense.  

    Again, refer to the function, all it does is return an Http OK from the Run method, so if the method is getting called, the response should just be OK.  You can see the endpoints below, and the logs that show I'm hitting the endpoint, but instead getting a 404. 

    Http Function AgentsPost: http://localhost:7071/api/agents
    Debugger listening on [::]:5858
    [8/18/2017 6:07:36 PM] Executing HTTP request: {
    [8/18/2017 6:07:36 PM]   "requestId": "ad72ec70-0b31-46e3-8008-32b33dbf78dc",
    [8/18/2017 6:07:36 PM]   "method": "POST",
    [8/18/2017 6:07:36 PM]   "uri": "/api/agents"
    [8/18/2017 6:07:36 PM] }
    [8/18/2017 6:07:36 PM] Executed HTTP request: {
    [8/18/2017 6:07:36 PM]   "requestId": "ad72ec70-0b31-46e3-8008-32b33dbf78dc",
    [8/18/2017 6:07:36 PM]   "method": "POST",
    [8/18/2017 6:07:36 PM]   "uri": "/api/agents",
    [8/18/2017 6:07:36 PM]   "authorizationLevel": "Anonymous"
    [8/18/2017 6:07:36 PM] }
    [8/18/2017 6:07:36 PM] Response details: {
    [8/18/2017 6:07:36 PM]   "requestId": "ad72ec70-0b31-46e3-8008-32b33dbf78dc",
    [8/18/2017 6:07:36 PM]   "status": "NotFound"
    [8/18/2017 6:07:36 PM] }
    [8/18/2017 6:08:10 PM] Executing HTTP request: {
    [8/18/2017 6:08:10 PM]   "requestId": "ff6caf48-4c93-4b4b-b432-f56ac299d6f7",
    [8/18/2017 6:08:10 PM]   "method": "GET",
    [8/18/2017 6:08:10 PM]   "uri": "/api/agents"
    [8/18/2017 6:08:10 PM] }
    [8/18/2017 6:08:10 PM] Executed HTTP request: {
    [8/18/2017 6:08:10 PM]   "requestId": "ff6caf48-4c93-4b4b-b432-f56ac299d6f7",
    [8/18/2017 6:08:10 PM]   "method": "GET",
    [8/18/2017 6:08:10 PM]   "uri": "/api/agents",
    [8/18/2017 6:08:10 PM]   "authorizationLevel": "Anonymous"
    [8/18/2017 6:08:10 PM] }
    [8/18/2017 6:08:10 PM] Response details: {
    [8/18/2017 6:08:10 PM]   "requestId": "ff6caf48-4c93-4b4b-b432-f56ac299d6f7",
    [8/18/2017 6:08:10 PM]   "status": "NotFound"
    [8/18/2017 6:08:10 PM] }




    • Edited by JerryWiltse Friday, August 18, 2017 8:19 PM
    Friday, August 18, 2017 6:12 PM
  • Here, i extracted the mystery into it's own repo.  Please give it a try. 

    https://github.com/solvingj/azure-function-problem-demo


    • Edited by JerryWiltse Friday, August 18, 2017 6:31 PM
    Friday, August 18, 2017 6:30 PM
  • I've moved the 404 not found issue to a new post. 

    Leaving this ticket open to identify the issue with the syntax of :

    methods : "post" 

    Update:  There was no issue with syntax.  Removing this parameter just masked the issue because it caused the method to never get it. 



    • Edited by JerryWiltse Friday, August 18, 2017 8:19 PM
    Friday, August 18, 2017 6:59 PM
  • Is the repo you shared for the original issue where you got the null ref, or with the issue after you removed 'methods: post'?
    Friday, August 18, 2017 7:02 PM
  • It's fine for both.  Currently, it shows the 404.  If you just add the method parameter above, you can see the "Object instance" error. 
    Friday, August 18, 2017 7:05 PM
  • Status update... I just shortened the namespace on a whim and now my method is being hit.  Also, in my sample I'm now unable to hit the object error... stand by for more detail.
    Friday, August 18, 2017 7:11 PM
  • Ok, I've found the "root cause" and have modified the Github repository for this issue. 

    Effectively the root cause is having a project reference to a Netstandard2 class library in the solution.  You can "enable/disable" the error by adding/removing a reference to "DataModel". Please pull the latest and give it a try. 

    Friday, August 18, 2017 7:38 PM
  • Can you clarify the state of the latest? Is it with or without the fix? It's best to have the repo in the exact state that it repros a very specific issue to make sure we are all looking at the same thing (or use branches/tags for the different states).
    Friday, August 18, 2017 7:41 PM
  • i deleted the other issue (404) on msdn because now I can't reproduce, and this is the real issue.

    We're now talking about one issue.  The master branch is in a state of "being broken".  Open in visual studio, press F5, and hit the route "/agents" with POST using postman.  You should get the error. 

    Friday, August 18, 2017 7:58 PM
  • As you found the act of referencing a NETStandard 2.0 library triggers the problem. The reason is that is causes an assembly mismatch, and we end up loading two copies of System.Net.Http. And one of them gets treated as a plain POCO, meaning that the HttpRequestMessage is not the real thing (it's a freshly created object with no data).

    Of course, this is a bug and we need to make this work. This is tracked by https://github.com/Azure/azure-webjobs-sdk-script/issues/1792

    Friday, August 18, 2017 9:02 PM
  • David, Is there any workaround?

    I just spent another 8 hour day unsuccessfully trying to port my old function app over to a new Azure Functions project.  It was an ASP. NET web project with associated .NET 4.6.1 class libraries and unit test projects along side it.  I was unsuccessful, as the DLL hell situation is very real. 

    As I mentioned before, I spent the two days prior to that porting all the supporting class libraries over to Netstandard 2.0 projects. While that was also challenging, and ultimately we discovered it fails at runtime due to the issue you posted, the project compiles and is far more desirable overall. 

    Is there any way I can work around the issues with HttpClient and use my NetStandard2.0 class libraries in my azure function now?  Are these issues something that the Functions team will be able to release fixes for in a matter of days or even weeks?  Not asking for a timeframe, but some description of the scope of the problem would help me figure out what to do.  

    Currently, I'm 3+ days in the hole, and I have two broken versions of a big azure function projects to show for it. 

    Saturday, August 19, 2017 11:50 PM
  • Hi Jerry,

    It's best to discuss directly on the GitHub issue, and I see you have been doing that already, so I'll leave this one alone :)

    David

    • Marked as answer by JerryWiltse Wednesday, December 5, 2018 2:03 PM
    Tuesday, August 22, 2017 12:32 AM