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
    Jul 29, 2020 @ 13:45
    Manish Prajapati
    0

    WebApi 500 error code

    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 1867 posts 11859 karma points MVP 4x c-trib
    Jul 29, 2020 @ 15:05
    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
    Jul 30, 2020 @ 04:26
    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.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies