0

My C# solution (.Net 4.0) in VS2013 includes a project defining a static class. The constructor of this static class tries to connect to a database and does some manipulation with the data.

I'm facing the problem that every time I'm trying to build my solution or I'm referencing this static class in other projects (in the same solution), VS2013 is trying to call the constructor of the static class. After calling the constructor, VS2013 gets nonresponding. Definitely, as a temporary solution, I can comment the complicated part of the constructor, but eventually I'll still have to uncomment it and try to build.

Could you please tell me how to turn off in VS2013 this option of automatic loading of static classes in design mode? Tried to google, but no success.

Thanks!

leppie
  • 115,091
  • 17
  • 196
  • 297
user2668470
  • 301
  • 1
  • 10
  • 2
    You can't. Add the code the touches the 'static' type to another method and call that separately or use the `DesignMode` property if that is available. – leppie Nov 04 '14 at 13:45
  • 1
    Using [singleton](http://csharpindepth.com/articles/general/singleton.aspx) instead of pure `static` classes is exactly for such cases. – Sinatr Nov 04 '14 at 13:48

1 Answers1

1

You could add a test in your static constructor and only execute your code logic if you're not in design mode.

That being said, I wouldn't recommend you to run code that accesses a database in a static constructor. The main problem is you don't really know when it's called. See https://csharpindepth.com/articles/BeforeFieldInit

N3pp
  • 33
  • 1
  • 4
ken2k
  • 48,145
  • 10
  • 116
  • 176