Developer forum

Forum » Development » Problem with add-in classes

Problem with add-in classes

Anders Ebdrup
Reply

Hello

I have created an addin class for the TagExtensionMethod, but often when the application starts up it results in a "An item with the same key has already been added." exception (se attached log-file). The Exception is gone after the first reload of the page. 

There seems to be missing a lock when searching for addin classes

[AddInName("ToShortDateString")]
    public class ToShortDateString : TagExtensionMethod
    {
        public override string ExecuteMethod(string value)
        {
            DateTime date;
            if (DateTime.TryParse(value, out date))
            {
                return date.ToShortDateString();
            }
            else
            {
                return base.ExecuteMethod(value);
            }
        }
    }

 

 


Replies

 
Mikkel Ricky
Reply

This is not an answer to your problem which is more general, but note that any DateTime value in templates already has a number of additional attributes exposed: http://templates.dynamicweb-cms.com/TemplateTags/Dynamicweb-template-tags/General-tags/Date/time-tag-extensions.aspx

 

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Hi Anders

 

This exception should only occur if the type is loaded twice. Either by being twice in the /bin, or because the assembly somehow is loaded in app-domain twice. It could indicate a configuration error of some sort.

 

I've made a change to the add-in manager so it checks if a found add-in is already loaded - will be out with the next hotfix. TFS #11656

 

In the management center, developer node, you have something called add-ins - in there you can find a list of assemblies loaded twice. It should be empty - if not it might be worth examining why.

 

Another note: the attached exception seems to be related to a page template extender and not a tag extension?

 

Have a nice weekend, Nicolai

Votes for this answer: 1
 
Anders Ebdrup
Reply

Thank you very much :-) And as you can see in the attached screen dump the list from the management center is empty.

 

The error happens both from tag extensions and page template extensions and I assume that it happens because the method "GetAddInClasses" is called twice because multiple threads are accessing the application pool

Capture.PNG
 
Nicolai Høeg Pedersen
Reply

Hi Anders

 

How are multiple threads accessing the application pool?

 

Is this a 'normal' Dynamicweb installation or is it part of some special configuration?

 

Maybe you can mail me the URL?

 

BR Nicolai

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

FYI, I am seeing the same behavior on one of our live sites. I am using Version: 8.2.2.2 with some extensibility code. haven't seen the error before but now it starts popping up. No assemblies show up in the "loaded twice" list.

 

Will e-mail you the URL in a minute.

 

Imar

 

 
Anders Ebdrup
Reply

Hi Nicolai,

Regarding threads then each request to the server is handled as a thread and I have seen before that the some events do not seem to be thread safe?

 

Besides, where to find your mail address? :-)

//Anders

 
Nicolai Høeg Pedersen
Reply

@Anders @Imar

 

Ok, seems like something is going on which should not.

 

Will have someone look into it.

 

Anders, I can be found at np at dynamicweb.dk

 
Pavel Volgarev
Reply

Anders,

 

Could you paste the stack trace in the message? I'm unable to download your original attachment.

 

-- Pavel

 
Anders Ebdrup
Reply

Yes for sure - I have it for the different extenders (and added a screen dump from another solution):

 

NavigationProvider:

Logging 'Exception':
Type: System.ArgumentException
Message: An item with the same key has already been added.
StackTrace:    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at Dynamicweb.Extensibility.AddInManager.GetAddInClasses(Type AddInBaseType)
   at Dynamicweb.Frontend.XmlNavigation.GetProviders()
   at Dynamicweb.Frontend.XmlNavigation.XML(Int32 ParentID, Int32 StartLevel, Int32 StopLevel, Expand expand, Int32 areaID)
   at Dynamicweb.Frontend.XmlNavigation.GetNavigationHtml(Int32 ParentID, Int32 StartLevel, Int32 StopLevel, Expand ExpandMode, String NavigationName, String XsltPath, Int32 AreaID, Boolean SitemapMode, NameValueCollection settings, NameValueCollection attributes, IncludeMode mode)
   at Dynamicweb.Frontend.XmlNavigation.GetNavigationHtml(NameValueCollection settings, NameValueCollection attributes)
   at Dynamicweb.Frontend.XmlNavigation.GetNavigationHtml(DynamicElement d)
   at Dynamicweb.Frontend.XmlNavigation.ParseNavigationTags(Template t, Layout l)
   at Dynamicweb.Frontend.PageView.SetPageTemplateValues()
   at Dynamicweb.Frontend.PageView.Output()
   at Dynamicweb.Frontend.PageviewControl.ParseControls()
   at System.Web.UI.Control.InitRecursive(Control namingContainer)
   at System.Web.UI.Control.AddedControl(Control control, Int32 index)
   at xxx.Default.Page_Load(Object sender, EventArgs e)
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

 

PageTemplateExtender:

Logging 'Exception':
Type: System.ArgumentException
Message: An item with the same key has already been added.
StackTrace:    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at Dynamicweb.Extensibility.AddInManager.GetAddInClasses(Type AddInBaseType)
   at Dynamicweb.Frontend.PageView.RenderPageTemplateExtenders(Template Template)
   at Dynamicweb.Frontend.PageView.SetPageTemplateValues()
   at Dynamicweb.Frontend.PageView.Output()
   at Dynamicweb.Frontend.PageviewControl.ParseControls()
   at System.Web.UI.Control.InitRecursive(Control namingContainer)
   at System.Web.UI.Control.AddedControl(Control control, Int32 index)
   at xxx.Default.Page_Load(Object sender, EventArgs e)
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
 

TemplateExtender:

Logging 'Exception':
Type: System.ArgumentException
Message: An item with the same key has already been added.
StackTrace:    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at Dynamicweb.Extensibility.AddInManager.GetAddInClasses(Type AddInBaseType)
   at Dynamicweb.Extensibility.AddInManager.GetTemplateExtenders[T](Boolean ignoreCache)
   at Dynamicweb.Content.Items.Rendering.Renderer.RenderItem(ItemEntry item, ItemSettings settings)
   at Dynamicweb.Frontend.Content.WriteParagraph(ParagraphTemplateJoinClass PtRecord)
   at Dynamicweb.Frontend.Content.CreateLayoutContent(ParagraphTemplateJoinClass PtRecord)
   at Dynamicweb.Frontend.Content.CreateContentEXE()
   at Dynamicweb.Frontend.Content.GetContent(Int32 pageId)
   at Dynamicweb.Frontend.PageView.SetPageTemplateValues()
   at Dynamicweb.Frontend.PageView.Output()
   at Dynamicweb.Frontend.PageviewControl.ParseControls()
   at System.Web.UI.Control.InitRecursive(Control namingContainer)
   at System.Web.UI.Control.AddedControl(Control control, Int32 index)
   at xxx.Default.Page_Load(Object sender, EventArgs e)
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

 

Please let me know if you need some more information

Capture.PNG
 
Nicolai Høeg Pedersen
Reply

I've changed the file name of your original upload from .log to .txt - so now it can be downloaded.

 

We are looking into this - I made the change mentioned above so it will not happen. We are now looking at why this has started to happen.

 

BR Nicolai

 
Anders Ebdrup
Reply

Hi Nicolai,

 

After upgrading to v8.3.2.1 this problem seems to be back again. See stacktrace:

 


[ArgumentException: An item with the same key has already been added.]
   System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +53
   System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +354
   Dynamicweb.Extensibility.AddInManager.GetAddInClasses(Type AddInBaseType) +1127
   Dynamicweb.Extensibility.AddInManager.GetTemplateExtenders(Boolean ignoreCache) +426
   Dynamicweb.Content.Items.Rendering.Renderer.RenderItem(ItemEntry item, ItemSettings settings) +3379
   Dynamicweb.Content.Items.Rendering.Renderer.RenderAreaItem(ItemEntry item, Template template) +80
   Dynamicweb.Frontend.PageView.SetPageTemplateValues() +6377
   Dynamicweb.Frontend.PageView.Output() +984
   Dynamicweb.Frontend.PageviewControl.ParseControls() +56
   Dynamicweb.Frontend.PageviewControl.OnInit(EventArgs e) +86
   System.Web.UI.Control.InitRecursive(Control namingContainer) +381
   System.Web.UI.Control.AddedControl(Control control, Int32 index) +204
   System.Web.UI.ControlCollection.Add(Control child) +157
   DanskStandard.Default.Page_Load(Object sender, EventArgs e) in d:\Work\Repositories\xxx\Main\Source\Application\Default.aspx.cs:10
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +85
   System.Web.UI.Control.OnLoad(EventArgs e) +74
   System.Web.UI.Control.LoadRecursive() +120
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2847

Add I have looked in the administration under: Management center -> Developer -> Add-ins and the list i empty.

It only happens at startup of the application.

 

Best regards, Anders

 
Nicolai Høeg Pedersen
Reply

Will look into this.

 

Nicolai

 
Anders Ebdrup
Reply

 

Original message by Nicolai Høeg Pedersen posted on 17/06/2013, 11:24:

Will look into this.

 

Nicolai

Hi Nicolai

 

Is this problem fixed? As I still see the problem in the 8.3.0.5 release under startup of the applicatino?

This time for a complete standard solution without any custom code:

 

[ArgumentException: An item with the same key has already been added.]
   System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +52
   System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +10695474
   Dynamicweb.Extensibility.NotificationManager.Notify(String notification, Object[] args) +553
   Dynamicweb.Extensibility.NotificationManager.Notify(String notification, Object eventArgs) +53
   Dynamicweb.Frontend.GlobalAsaxHandler.Application_AuthenticateRequest(Object sender, EventArgs e) +107
   Dynamicweb.Global.Application_AuthenticateRequest(Object sender, EventArgs e) in e:\Work\Repositories\xxx\Main\Source\Application\Global.asax.cs:34
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

 

Best regards, Anders

 
Morten Bengtson
Reply

It looks like the add-in types are added to a static/shared Dictionary (cachedTypes) in a way that is not thread safe (outside synclock). It might be easier to just use a static ConcurrentDictionary and then call the TryAdd or AddOrUpdate method.

http://msdn.microsoft.com/en-us/library/dd997305.aspx

 

 

You must be logged in to post in the forum