Chris Tanger <(E-Mail Removed)> wrote:
> I have a multithreaded server application where each thread
> instantiates and uses the RSACryptoServiceProvider class when needed. During
> testing there can be as many as 100 threads attempting to instantiate
> RSACryptoServiceProvider simultaneously. When this happens, most of the
> time, but not always about 3 of the threads throw the following exception
> while attempting RSACryptoServiceProvider instantiation.
>
> "CryptoAPI cryptographic service provider (CSP) for this implementation
> could not be acquired."
>
> Is the underlying CryptoAPI not threadsafe? Or can it just not handle too
> many instantiations in close succession, perhaps because of not being able to
> cleanup its resources fast enough.
>
> Should I mark code that uses RSACryptoServiceProvider as a critical section
> by using lock statements?
>
> NOTE: When my application has been released to production there could be as
> many as 500-1000 close succession instantiations of RSACryptoServiceProvider.
>
> I also saw a post that appeared to have a similar problem on 10/25/2004 by
> Don Nelson in dotnet.framework.aspnet.security
I've heard about similar problems involving creating and opening SQL
connections. It could be that instantiation isn't properly threadsafe -
I'd imagine that once created, so long as you only used each instance
within one thread, you'd be okay. It's worth trying a lock just around
instantiation (make a factory for it to make it easier).
--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too