title: Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.IUrlHelper' while attempting to activate
body: <p>I am trying to separate code from controller to service that I created. What I did is to create a User Service with interface IUserService.</p>
  
  <p>Moved RegisterUser code from directly controller to UserService, the next challenge I get was that Url which works directly with Controller doesn't work with Service.</p>
  
  <p>This code from Controller to Service has been changed like this:</p>
  
  <p><strong>if in Controller</strong></p>
  
  <pre><code>var callbackUrl = Url.EmailConfirmationLink(user.Email, token, model.contactno, Request.Scheme);
  </code></pre>
  
  <p><strong>Changed from Controller to Service:</strong></p>
  
  <pre><code>  private IUrlHelper _urlHelper;
  
      public UserService (IUrlHelper urlHelper, HttpRequest request) {
  _urlHelper = urlHelper;
  }
  </code></pre>
  
  <p>this was constructor,</p>
  
  <p>in Method I am calling it like this:</p>
  
  <pre><code>        var callbackUrl = _urlHelper.EmailConfirmationLink (user.Email, token, U.Email, _request.Scheme);
  </code></pre>
  
  <p>I mentioned in DI in Startup.cs like this:</p>
  
  <pre><code>services.AddScoped&lt;IUserService, UserService&gt;();
  </code></pre>
  
  <p>There is no Excpetion at compile time. At run time it is throwing below excption:</p>
  
  <pre><code>Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
        An unhandled exception has occurred while executing the request.
  System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.IUrlHelper' while attempting to activate 'erp.Services.UserService'.
  </code></pre>
  
  <p>Not very clear what exactly has to be done.</p>
  
  <p>here is full stack trace to see the error more closely. well I can't say the exact line as I am not debugging but just copying from the logs:</p>
  
  <pre><code>Executed endpoint 'erp.Controllers.AccountController.Register (erp)'
  fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
        An unhandled exception has occurred while executing the request.
  System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.HttpRequest' while attempting to activate 'erp.Services.UserService'.
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(Type serviceType, Type implementationType, CallSiteChain callSiteChain)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, CallSiteChain callSiteChain)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceType)
     at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
  </code></pre>