Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1342 posts 1603 karma points
    27 days ago
    Gordon Saxby
    0

    TC not available in Controller

    I am trying to use TC.GetVariant in a controller and am getting this error:

    The requested service 'TeaCommerce.Umbraco.Configuration.Variants.Services.IVariantService`2[[KervPlatform.Extensions.Models.Generated.Product, KervPlatform.Extensions, Version=1.10.7.0, Culture=neutral, PublicKeyToken=null],[TeaCommerce.Umbraco.Configuration.Variants.Models.VariantPublishedContent, TeaCommerce.Umbraco.Configuration, Version=3.4.7409.20189, Culture=neutral, PublicKeyToken=null]]' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

    It is a SurfaceController and is handling a Html.BeginUmbracoForm (multipart/form-data) submission.

    I have other controllers that use TC and they are working fine. I'm sure this is something very simple but it's evading me at the moment! :-(

    Any clues / ideas on where to look and what to look for?

  • Matt Brailsford 2765 posts 14041 karma points MVP 7x c-trib
    27 days ago
    Matt Brailsford
    0

    Hi Gordon,

    It could be a Models Builder issue. Give this forum post a look over and see if it solves your problem https://our.umbraco.com/packages/website-utilities/tea-commerce/tea-commerce-support/97306-autofac-error

    Matt

  • Gordon Saxby 1342 posts 1603 karma points
    27 days ago
    Gordon Saxby
    0

    I have seen that post - and looked at it again. However, I'm not quite sure I understand what the solution is?!

  • Matt Brailsford 2765 posts 14041 karma points MVP 7x c-trib
    27 days ago
    Matt Brailsford
    0

    Hmm, maybe it isn't your issue actually given they are working in a view, not a surface controller.

    Are you using Models Builder models?

    It appears something is messing with TC's DI container and so the dependency doesn't appear to be registered at the point of access. I'm not sure why it's messing with it though 🤔

    Matt

  • Gordon Saxby 1342 posts 1603 karma points
    27 days ago
    Gordon Saxby
    0

    Yes, I am using ModelsBuilder.

    "TC." works in other controllers, which as far as I can tell are configured the same way.

    "ProductService.Instance" works OK.

    Maybe it is the way my View and Partial are setup?

    View:

    @inherits UmbracoViewPage<MasterModel<CSimportStockLevels>>
    @{
        Layout = "CSMaster.cshtml";
    }
    ... other stuff ...
    <section class="pad-tb-large" id="mainContent" role="main">
        @Html.Partial("CustomerServices/Import/CSImportStockLevelsFile", Model)
    </section>
    

    Partial:

    @inherits UmbracoViewPage<MasterModel<CSimportStockLevels>>
    
    <div class="row">
        <div class="small-12 columns">
            <div class="panel clearfix">
                <div class="row">
                    <div class="small-12 columns ">
                        @if (Model.Content.Errors != null && Model.Content.Errors.Any())
                        {
                            foreach (var error in Model.Content.Errors)
                            {
                                <p>@error</p>
                            }
                        }
                        @using (Html.BeginUmbracoForm("ImportStockLevels", "StockLevelImport", FormMethod.Post, new {enctype = "multipart/form-data"}))
                        {
                            <input type="file" name="ImportFile"/>
                            <input type="submit" class="button" value="@Umbraco.GetDictionaryValue("CustServices.ButtonImportStockLevels")"/>
                        }
                    </div>
                </div>
            </div>
        </div>
    </div>
    

    CSimportStockLevels is a ModelsBuilder class.

    EDIT: just tried putting the partial code into the view and that made no difference.

  • Matt Brailsford 2765 posts 14041 karma points MVP 7x c-trib
    27 days ago
    Matt Brailsford
    0

    Ahh, what does your call to TC.GetVariant look like?

    Matt

  • Gordon Saxby 1342 posts 1603 karma points
    27 days ago
    Gordon Saxby
    0

    This is most of the surrounding code:

            var partSKU = string.Format("{0}-{1}-{2}-{3}", skuParts[0], skuParts[1], skuParts[2], skuParts[3].Left(1));
    
            var productPages = _storePage.Descendants<Product>().Where(p => p.Sku == partSKU).ToList();
            if (!productPages.Any())
            {
                return false;
            }
    
            // Check that there is a Variant with the current full SKU
            foreach (var product in productPages)
            {
                var variant = TC.GetVariant(1, product, recordSKU);
                if (variant == null)
                {
                    return false;
                }
            }
    
  • Matt Brailsford 2765 posts 14041 karma points MVP 7x c-trib
    27 days ago
    Matt Brailsford
    0

    Yup, so I think the problem is the product variable is of type Product and so the call to GetVariant, which is a generic method is using Product as it's generic type parameter and so when it looks up a variants service from the DI container, one doesn't exist as it hasn't been registered explicitly for that Type, one is only regenerated for the IPublishedContent type.

    You either need to cast product to be IPublishedContent before passing it to GetVariant like

    TC.GetVariant(1, (IPublishedContent)product, recordSKU); 
    

    or you need to call GetVariant like

    TC.GetVariant<IPublishedContent>(1, product, recordSKU);
    

    Matt

  • Gordon Saxby 1342 posts 1603 karma points
    27 days ago
    Gordon Saxby
    0

    Ah, right, OK.

    I was using TC.GetVariant to basically check if the variant / SKU existed on the product. However, because I need to update other values (2 True/False fields) on the Variant record (for a specific SKU), I am going to have to do it a different way anyway.

    Just tried both of the above and the code din't error - however, it also failed to find the Variant record :-(

    It may well be that I don't need to use this code / method any more, but it would be nice to see it working.

  • Matt Brailsford 2765 posts 14041 karma points MVP 7x c-trib
    27 days ago
    Matt Brailsford
    0

    Ok, so looking at GetVariant closer, it looks like you don't pass a variant SKU, it accepts a variant identifier which I think is the ID of the variant, rather than the SKU.

    I think you'd have to call GetVariants to get all the variants and then find the one with the SKU property on it.

    Matt

Please Sign in or register to post replies

Write your reply to:

Draft