HttpContext.Items vs Scoped Service
<p>Which is the better way to carry request data(Is there any difference between two way)? </p>

<p>For example:</p>

<p>Option 1(<code>Scoped Service</code>):</p>

<pre><code>//Scoped Service(this may be interface)
public class SampleScopedService
{
    public string Data { get; set; }
}

//Register service
services.AddScoped&lt;SampleScopedService&gt;();

//Set and Get Data
public class SampleUsage
{
    private readonly SampleScopedService _sampleScopedService;
    public SampleUsage(SampleScopedService sampleScopedService)
    {
        _sampleScopedService = sampleScopedService;
        // _sampleScopedService.Data = "Sample";
        // _sampleScopedService.Data 
    }
}
</code></pre>

<p>Option 2(<code>HttpContext.Items</code>)</p>

<pre><code>//Scoped Service
public class SampleScopedService
{
    private readonly IHttpContextAccessor _accessor;

    public SampleScopedService(IHttpContextAccessor accessor)
    {
        _accessor = accessor;
    }
    public string GetData()
    {
        return (string)_accessor.HttpContext.Items["Data"];
    }
}

//Register service
services.AddScoped&lt;SampleScopedService&gt;();

//Set Data
HttpContext.Items[“Data”] = ”Sample”;

//Get Data
public class SampleUsage
{
    private readonly SampleScopedService _sampleScopedService;
    public SampleUsage(SampleScopedService sampleScopedService)
    {
        _sampleScopedService = sampleScopedService;
        //_sampleScopedService.GetData();
    }
}
</code></pre>