ScriptContext, thread safe and generating key names

I’m really loving the script stuff. I’m looking to use it to generate partition and row keys in a generic way. Is the script context thread safe? Any performance issues here? If I have just one in a singleton class so it only gets init’d one time and am calling it:

context.EvaluateScript(strKeyFormat, argWithItem)

Will this work? Any suggestions?

More info:

I am registering how keys get built like:

KeyStrategy.Register(type.PartitionKey, “{{Model.Id}}-{{Model.Date}}”)

Then when being used we can easily get the partition key for this class and type:

var item=new MyClass; // …
KeyStrategy.Generate(item, type.PartitionKey)

This also allows us to build a report of all the formats being used, use attributes to do the registrations, etc which is why we are pre-registering them.

Yes ScriptContext is ThreadSafe to be used as a singleton provided all ScriptContext configuration is done prior to calling .Init(). After initialization the only ThreadSafe collections you should modify are the Concurrent Dictionary caches.

The SharpPagesFeature used in #Script Pages is itself a singleton ScriptContext that all pages are executed within.

I don’t understand what relationship your KeyStrategy class has with the ScriptContext, if it’s just used as arguments in your script then there’s no issue.

1 Like

Thanks, the KeyStrategy iis the singleton class that will have the one script context that will be used to generate all the partition row keys. Just passing into it an item so its key is generated in a consistent manner across the system.

Sure, if it’s only being read from in scripts there’s no thread safety issue, it just can’t be modified whilst it’s being read anywhere.