20

Having a bit of trouble with the events system when triggering an event via core service.

We have components saved to a publication that we need to copy across to another publication in the same instance of Tridion. These components are saved from a separate application that uses the core service.

So in summary: Create/update a component in webapp -> Save via core service to one publication -> Event system creates/updates copy in second publication.

We subscribe to the event using:

EventSystem.Subscribe<Component, SaveEventArgs>(Copy, EventPhases.TransactionCommitted);

We have a few configuration values stored in the Tridion.ContentManager.config file and access these using something similar to:

var configuration = ConfigurationManager.GetSection("custom.events") as NameValueCollection;
return configuration["Debug"];

The copy method works fine as manually opening the component in the interface and saving does exactly as expected. The config values are read in fine and the component gets copied across with all the correct content and metadata.

The event system falls over when we try to save a component externally using the core service. The errors we are seeing is as follows:

Object reference not set to an instance of an object.

StackTrace Information Details:
   at Custom.EventsSystem.Configuration.Config.get_Debug()
   at Custom.EventsSystem.Sync.Copy(Component component, SaveEventArgs args, EventPhases phase)
   at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at Tridion.ContentManager.Extensibility.EventSubscription.DeliverEvent(IEnumerable`1 subjects, TcmEventArgs eventArgs, EventPhases phase)

This only happens when the save is triggered via the core service so I wonder if there is anything different in the way that Tridion reads the configuration file when accessed via the core service?

Would the better option be to have a separate configuration file separate to Tridion that we deal with manually?

3
  • If you manage to save the component to one publication using the core service, can't you use the core service to create a copy of it to another publication instead of using the event system? Commented May 8, 2013 at 12:18
  • In addition to what Ibrar has suggested, you should not use Core Service in Event System but the TOM.NET API should be used. Commented May 8, 2013 at 12:31
  • We can't use the core service again without change to a piece of important, legacy code. The components are needed in both places in the blueprint but the blueprint can not be amended to share the items either which is a pain! Commented May 8, 2013 at 12:38

2 Answers 2

11

How do you connect to the core service? Are you using a netTCP binding or a wsHttp/basicHttp binding? In case of the former the app.config file would be TcmServiceHost.exe.config and for the latter it would be web.config in %TRIDION_HOME%\webservices...

1
  • 2
    This worked a treat. We use the wsHttp binding so amending the webservices\web.config to include our custom section fixed our problem. Cheers Bjorn. Commented May 9, 2013 at 9:01
13

This is a great example of the evil that is the "As" cast

If you wrote your code like this:

var configuration = (NameValueCollection)ConfigurationManager.GetSection("custom.events");

Instead of a Null Reference exception, you'd get an Invalid Cast Exception when GetSection returns a ConfigurationSection. Of course, you should still implement a null reference check anyway, in case the configuration section isn't present, otherwise it will throw immediately when you try to dereference the indexer at configuration["Debug"]

3
  • 2
    +2, just because +1 isn't enough for how evil the "As" cast can be ;o) Commented May 8, 2013 at 15:45
  • @BartKoopman Just count yourself lucky that I didn't pick you up for it yesterday with those "training samples". :-) Commented May 8, 2013 at 18:14
  • Hmmm you are right, let me change that example tomorrow ;o), as mentioned in there I just use the example, never really looked well enough at the actual code (besides the part about cleaning up that is). And while As casts are evil of course, in that example it would be a valid one (in my defense ;o) Commented May 9, 2013 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.