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.
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>();
}
} }
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;
}
}
}
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.
I am posting my code :-
ISumService.cs
SumService.cs
SumController.cs
Compose.cs
Could you please help me?
Hi,
I think you've got your SumService being a bit circular ?
The
SumService
implementsISumService
, so it shouldn't need aSumService
passing into it rather that is where the code is to do the work?e.g
Thanks for noticing it. I have removed the circular dependency. Now, it is working fine :). Thank you so much for your help.
is working on a reply...