1

hello I understand the life cycle of injected service like scoped that after the request is finished the service is unavailable. But the parameter (like myObject in the example) when their are destroyed? If i pass this parameter to long async task and I don't await the result I could have some problem of null in the task?

public class mycontroller : ControllerBase
{
    private MyService _myservice;

    public mycontroller(MyService myservice)
    {
        _myservice = myservice;
    }

    [HttpPost]
    public IActionResult Post([FromBody] MyObject myObject)
    {
        _myservice.dosomethinglongasync(myObject);
        return OK();
    }
}
1
  • Normal .net object life cycle rules apply. Instances are GCed once there are no more references. As far as how to do a fire/forget in asp.net core there are other already asked and answered questions about that on Stack Overflow. Commented May 10, 2021 at 15:20

1 Answer 1

1

The task will continue to hold a reference onto myObject until it's completed. You should not face an issue that it's destroyed beforehand.

But(!) this is a bad design. You can not check the task for completion, etc. Firing async tasks without awaiting them somewhere is no good practice.

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

Comments

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.