i have an extension that was something like that :
public static int GetMultiLanguageRelatedNode(this IContent content, int nodeId = 0)
{
var a = UmbracoContext.Current.PageId.Value;
return a;
}
it works perfect on U7
but why the hell in U8 "UmbracoContext.Current.PageId.Value" does not work ? im i doing something wrong ?
always saying that "UmbracoContext" does not exists in the current context but i checked the documentation and it says that nothing changed in this method ... or im i wrong ?
the problem its not the method it self ...because i know that the language engine has changed ! ... the problem is not getting access to ApplicationContext and UmbracoCOntext
so ... if i have a static class with several extensions ...how can i get the contexts without passing as parameter the service it self ?
in the U8 documentation it says in the last example that we can do like that :
IUserService us = ApplicationContext.Current.Services.UserService
the problem as i said its not the method, its the static instatiation inside a static class ( ApplicationContext.Current.Services.UserService) not being recognized
You will want to use the following namespace in Umbraco 8:
using Umbraco.Core.Composing
Then you can access services like:
Current.Services.UserService
Umbraco 8 uses dependency injection and on top of that a composition layer. The above code allows you to get services (among other things) straight from the IoC container.
btw: The documentation sadly is not yet up to date with Umbraco 8. Whenever looking through the documentation look for the note at the top of the page that says "This article has not yet been verified against Umbraco 8.". That basically means that possibly something changes that impact the accuracy of the article. Sadly in your case you ran into one of the more massive (but good!) changes.
I don't think there really is a way to not do this in the correct way :P
Just add the namespace to the using at the top of your file and change all occurrences of "ApplicationContext.Current.Services.---" to "Current.Services.---" and you should be good to go.
Also instead of using "UmbracoContext.Current.PageId.Value" you can use "Umbraco.Web.Composing.Current.UmbracoHelper.AssignedContentItem.Id"
Having that said, you want to add "Umbraco.Web.Composing" as the using on top of the page instead of "Umbraco.Core.Composing" as they both provide the same services, but Umbraco.Web.Composing also exposes the UmbracoHelper.
Well in that article it is recommended you use UmbracoContext.Current which basically the same as what I suggested above. The difference is that in the recommendation above you do use the IoC container that Umbraco now utilizes :)
UmbracoContext.Current.PageId.Value
sorry for the stupid question but
i have an extension that was something like that :
it works perfect on U7 but why the hell in U8 "UmbracoContext.Current.PageId.Value" does not work ? im i doing something wrong ?
always saying that "UmbracoContext" does not exists in the current context but i checked the documentation and it says that nothing changed in this method ... or im i wrong ?
thank you
Angelo
Hi Angelo
Can you show all method "GetMultiLanguageRelatedNode"?
Alex
Hello Alex thans for the answer :) here it goes:
some of the methods are mine ... but "ApplicationContext" and "UmbracoContext" dont work :(
let me say also that i only was able to run U8 with .NET 4.7.2 i dont know if it helps ...
U8 works only with .NET 4.7.2 it's fine
Probably, you need to rewrite this method totally.
As I see this method uses ContentService, it looks like not the best performance. When do you use this method?
in U8 there is absolutely different support of multi-languages so maybe you need to rethink an architecture?
Alex let me reformulate the question
the problem its not the method it self ...because i know that the language engine has changed ! ... the problem is not getting access to ApplicationContext and UmbracoCOntext
so ... if i have a static class with several extensions ...how can i get the contexts without passing as parameter the service it self ?
in the U8 documentation it says in the last example that we can do like that :
https://our.umbraco.com/documentation/Reference/Management/Services/UserService/
the problem as i said its not the method, its the static instatiation inside a static class ( ApplicationContext.Current.Services.UserService) not being recognized
i dont know if i was more precise now ...
than you
AS
You will want to use the following namespace in Umbraco 8:
Then you can access services like:
Umbraco 8 uses dependency injection and on top of that a composition layer. The above code allows you to get services (among other things) straight from the IoC container.
btw: The documentation sadly is not yet up to date with Umbraco 8. Whenever looking through the documentation look for the note at the top of the page that says "This article has not yet been verified against Umbraco 8.". That basically means that possibly something changes that impact the accuracy of the article. Sadly in your case you ran into one of the more massive (but good!) changes.
Hello Dave fantastic ... im elucidated now :)
is there a way to do this in the "correct way" ?
thank you
AS
I don't think there really is a way to not do this in the correct way :P
Just add the namespace to the using at the top of your file and change all occurrences of "ApplicationContext.Current.Services.---" to "Current.Services.---" and you should be good to go.
Also instead of using "UmbracoContext.Current.PageId.Value" you can use "Umbraco.Web.Composing.Current.UmbracoHelper.AssignedContentItem.Id"
Having that said, you want to add "Umbraco.Web.Composing" as the using on top of the page instead of "Umbraco.Core.Composing" as they both provide the same services, but Umbraco.Web.Composing also exposes the UmbracoHelper.
ok
i read this article https://our.umbraco.com/documentation/reference/Common-Pitfalls/
this is why im asking ..
but i will give it a try ... thank you a lot Dave ! :)
Angelo
Ah okay,
Well in that article it is recommended you use UmbracoContext.Current which basically the same as what I suggested above. The difference is that in the recommendation above you do use the IoC container that Umbraco now utilizes :)
ok dave thank you a lot :)
all the best
Angelo
is working on a reply...