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?
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 🤔
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;
}
}
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
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.
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.
TC not available in Controller
I am trying to use TC.GetVariant in a controller and am getting this error:
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?
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
I have seen that post - and looked at it again. However, I'm not quite sure I understand what the solution is?!
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
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:
Partial:
CSimportStockLevels is a ModelsBuilder class.
EDIT: just tried putting the partial code into the view and that made no difference.
Ahh, what does your call to
TC.GetVariant
look like?Matt
This is most of the surrounding code:
Yup, so I think the problem is the product variable is of type
Product
and so the call toGetVariant
, which is a generic method is usingProduct
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 theIPublishedContent
type.You either need to cast product to be
IPublishedContent
before passing it toGetVariant
likeor you need to call
GetVariant
likeMatt
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.
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
is working on a reply...