Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Manish Prajapati 4 posts 34 karma points
    17 days ago
    Manish Prajapati
    0

    Hi All,

    I am trying to implemented a web api which will be consumed on front end. I am calling the api in the browser but getting the attached error.

    enter image description here

    I am posting my code :-

    ISumService.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MyProject.Umbraco.Api
    {
     public interface ISumService
     {
        int Sum(int x, int y);
     }} 
    

    SumService.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MyProject.Umbraco.Api
    {
        public class SumService : ISumService
       {
    
        private ISumService _sumService;
    
         public SumService(
            ISumService sumService)
        {
            _sumService = sumService;
        }
        public int Sum(int x, int y)
        {
            int sum = _sumService.Sum(x, y);
            return sum;
        }
    
     }
    }
    

    SumController.cs

     using System.Web.Http;
     using Umbraco.Web.WebApi;
    
    namespace MyProject.Umbraco.Api
    {
      public class SumController : UmbracoApiController
      {
    
        private ISumService _sumService;
    
        public SumController(ISumService sumService)
        {
            _sumService = sumService;
    
        }
    
    
        [HttpGet]
        public int GetSum()
        {
            return _sumService.Sum(10, 30);
        }
    
       }
      }
    

    Compose.cs

     using Umbraco.Core.Composing;
    
     namespace MyProject.Umbraco.Api
     {
       public class Composer : IUserComposer
       {
        public void Compose(Composition composition)
        {
            composition.Register<ISumService, SumService>();
        }
    } }
    

    Could you please help me?

  • Kevin Jump 1753 posts 11263 karma points MVP 4x c-trib
    17 days ago
    Kevin Jump
    100

    Hi,

    I think you've got your SumService being a bit circular ?

    The SumService implements ISumService, so it shouldn't need a SumService passing into it rather that is where the code is to do the work?

    e.g

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MyProject.Umbraco.Api
    {
       public class SumService : ISumService
       {
           // this is now parameterless - because we pass nothing in.
           public SumService() 
           { }
    
           public int Sum(int x, int y)
           {
                // the actually summing happens here?
                int sum = x + y ;
                return sum;
           }
        }
    }
    
  • Manish Prajapati 4 posts 34 karma points
    16 days ago
    Manish Prajapati
    0

    Thanks for noticing it. I have removed the circular dependency. Now, it is working fine :). Thank you so much for your help.

Please Sign in or register to post replies

Write your reply to:

Draft