skip to Main Content

I am trying to enable the operator to reach the function by debugging in .NET CORE
in C#,
and I get an error like this:

An unhandled exception occurred while processing the request
InvalidOperationException: Unable to resolve service for type ;PlatformWebApi.Controllers.Order.IOrderHelp& while attempting to activate
PlatformWebApi.Controllers.Order.OrderApi
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

The code is:

OrderApi.cs

namespace PlatformWebApi.Controllers.Order
{
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public class OrderApi: Controller
    {
        public Log log = new Log();
        private readonly WebOptions _webOption;
        private IWebApiHelp _WebApiHelp;
        private ISapFunction _sapFunction;
        private readonly  IOrderHelp _orderHelp;
        public OrderApi(IOptions<WebOptions> myConfiguration,
           IWebApiHelp WebApiHelp,
           ISapFunction SapFunction,
           IOrderHelp OrderHelp)
        {
            _webOption = myConfiguration.Value;
            _WebApiHelp = WebApiHelp;
            _orderHelp = OrderHelp;
            _sapFunction = SapFunction;
        }
        [HttpPost(ApiRoutes.Order.AddUpdateOrder)]
        [Consumes("application/xml", "application/json")]
        //OrderXml.OrderDetails
        public ResponseData AddUpdateOrder([FromBody] OrderXml.OrderDetails xmlString)
        {
            ResponseData oRes = _orderHelp.Add_updateOrder(xmlString);
            return oRes;//Json(oRes);
        }
        //[HttpPost(ApiRoutes.Order.UpdateOrderStep)]
        //[Consumes("application/xml", "application/json")]

        //public ResponseData UpdateOrderStep([FromBody]string xmlString)
        //{
        //    ResponseData oRes = _orderHelp.Update_OrderStep(xmlString);
        //    return oRes;//Json(oRes);
        //}

    }
}

IOrderHelp.cs

namespace PlatformWebApi.Controllers.Order
{
    public interface IOrderHelp
    {
        ResponseData Add_updateOrder(OrderXml.OrderDetails xmlString);
    
    }
}

 

OrderHelp.cs:

public class OrderHelp :IOrderHelp
    {
        // _SAPConnectionHelp _SAPConnectionHelp = new _SAPConnectionHelp();
       

        string error = "";
        public Log log = new Log();
        private readonly WebOptions _webOption;
        private IWebApiHelp _WebApiHelp;
        private ISapFunction _sapFunction;
        private ISAPConnectionHelp _SAPConnectionHelp;


        public OrderHelp(IOptions<WebOptions> myConfiguration,
           IWebApiHelp WebApiHelp,
           ISapFunction SapFunction,
           ISAPConnectionHelp SAPConnectionHelp)
        {
            _webOption = myConfiguration.Value;
            _WebApiHelp = WebApiHelp;
            _sapFunction = SapFunction;
            _SAPConnectionHelp = SAPConnectionHelp;
        }
 public ResponseData Add_updateOrder(OrderXml.OrderDetails xmlString)
        {
            string sErrorMsg = "";
            //  //CustomLog.MyLogger.info("----------STRART NewOrder");
            int DocEntry = 0, res = 0;
            List<ResponseKeys> oListResponseKeys = new List<ResponseKeys>();
            ResponseData oResponseData = new ResponseData();

            SAPbobsCOM.Documents oOrder = (Documents)_SAPConnectionHelp.GetCompany().GetBusinessObject(BoObjectTypes.oOrders);
....
}
}

Would appreciate help

I wanted to reach the function by debugging,
and in fact I encountered an error:

An unhandled exception occurred while processing the request
InvalidOperationException: Unable to resolve service for type ;PlatformWebApi.Controllers.Order.IOrderHelp& while attempting to activate
PlatformWebApi.Controllers.Order.OrderApi
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

2

Answers


  1. Most likely the problem you are facing depends on Dependency Injection used within your controller.

    You declare your filed like this:
    private readonly IOrderHelp _orderHelp;

    Then in your case i would check if in your Program.cs you have added the correct interface and class to dependecy injection. It could be something like this:

    services.AddSingleton<IOrderHelp, OrderHelp>();
    

    Where in this case IOrderHelp is the interface and the OrderHelp is the class implementation.

    Login or Signup to reply.
  2. Hey it looks like you are missing the dependency injection for your service. Go to the Programm.cs and add :

    builder.Services.AddSingleton<IOrderHelper,OrderHelper>();
    

    Now you can use your service in your classes.

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-8.0

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search