Copied to clipboard

Flag this post as spam?

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


  • Simon 24 posts 127 karma points
    Oct 28, 2016 @ 11:57
    Simon
    0

    A single instance of controller 'MySurfaceController' cannot be used to handle multiple requests.

    I'm trying to use surface controllers to display a list of products. This is working fine however if I try to call this twice I get the exception:

    @Html.Action("List", "Product", new { size = 10 })
    @Html.Action("List", "Product", new { size = 10 })
    
    A single instance of controller 'ProductController' cannot be used to handle multiple requests.
    

    The way I'm using it is to call the Product list controller via a Macro so that the user can add a product list anywhere.

    Controller:

    /// <summary>
    /// Product details controller
    /// </summary>
    public class ProductController : SurfaceController
    {
        private readonly string uri = ConfigurationManager.AppSettings["uri:searchApi"];
    
    public ActionResult List(ProductSearch request, int size = 10)
    {
        string requestUri = "workgroups/?page=0&pageSize=" + size;
    
        ProductSearch search = SearchHelper.CreateSearch(request, null);
    
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(uri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
            // New code:
            HttpResponseMessage response = client.PostAsJsonAsync(requestUri, search).Result;
    
            if (response.IsSuccessStatusCode)
            {
                WorkGroupSearchResult searchResult = response.Content.ReadAsAsync<WorkGroupSearchResult>().Result;
    
                return PartialView("List", new SearchModel(this.CurrentPage, searchResult));
            }
            else
            {
                throw new HttpRequestException("Request failed: " + response.StatusCode.ToString());
            }
        }            
    }
    }
    

    Any help would be much appreciated.

  • Simon 24 posts 127 karma points
    Oct 28, 2016 @ 13:24
    Simon
    100

    This was caused because I'm using th custom IoC framework StructureMap.

    Once I added the following to the container it runs fine:

    x.For<ProductController>().AlwaysUnique();
    
Please Sign in or register to post replies

Write your reply to:

Draft