Copied to clipboard

Flag this post as spam?

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


  • Tommi Gynther 5 posts 25 karma points
    Oct 23, 2014 @ 08:33
    Tommi Gynther
    0

    Umbraco 7 Web Api and Internal Server Error

    I implemented a basic Web Api interface to Umbraco (v7.1.6)

    Server code:

    public class MemberController : UmbracoApiController
    {
       [HttpGet]
       public IEnumerable GetMembers(string countryCode)
       {
            return GSKTools.Server.Handlers.MemberHandler.GetMembers(countryCode);
       }
    }

    Client code (console application):

    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(serverProperties.BaseAddress);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response = await client.GetAsync("Api/Member/GetMembers?countryCode=FI");
    }

    Code above works fine during first run. Second run from client causes always an internal server error. I have to restart Web site or do a clean build to get rid of an error

    Response contains following information when it fails:

    StatusCode: 500
    ReasonPhrase: Internal Server Error

    And following lines appears to UmbracoTraceLog.txt

    2014-10-23 09:14:39,243 [30] ERROR Umbraco.Core.UmbracoApplicationBase - [Thread 34] An unhandled exception occurred
    System.TypeLoadException: Inheritance security rules violated by type: 'System.Net.Http.Formatting.JsonContractResolver'. Derived types must either match the security accessibility of the base type or be less accessible.
       at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor()
       at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()
       at System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes)
       at System.Web.Http.GlobalConfiguration.<.cctor>b__0()
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.LazyInitValue()
       at Umbraco.Web.WebBootManager.InitializeResolvers()
       at Umbraco.Core.CoreBootManager.Initialize()
       at Umbraco.Web.WebBootManager.Initialize()
       at Umbraco.Core.UmbracoApplicationBase.StartApplication(Object sender, EventArgs e)
    2014-10-23 09:14:49,263 [30] INFO  Umbraco.Core.UmbracoApplicationBase - [Thread 39] Application shutdown. Reason: InitializationError

    What I am doing wrong? Configuration issue?
  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 23, 2014 @ 23:32
    Dan Diplo
    0

    Shouldn't your API controller method be returning an IEnumerable of a specific type? eg.

    publicIEnumerable<IMember>GetMembers(string countryCode)


  • Tommi Gynther 5 posts 25 karma points
    Oct 24, 2014 @ 00:00
    Tommi Gynther
    0

    Yes it should and actually is. Must be a copy-paste mistake:

    IEnumerable<Umbraco.Core.Models.IMember> 
Please Sign in or register to post replies

Write your reply to:

Draft