1

I am a newbie in C #, and just started to learn IoC in web api. What is the difference in Transient and PerWebRequest lifestyles

Sorry for bad English.

Developer
  • 66
  • 1
  • 6

3 Answers3

4
  • Singleton

It's mean "one instance for all". All times when you call Resolve (even implicitly) you got the same object

  • Transient

It's opposite to singletone. You'll get as many object as you call Resolve

  • PerWebRequest

Read how Singleton for one request and transient for other (You'll get as many object as request receive)

for more information read the catle.windsor manual or official asp.net docs

darkhac
  • 577
  • 4
  • 22
2

PerWebRequest scope lasts from the beginnning of a webcall to the end of the webcall. Transient lives as long as you hold a reference to the resolved entity. The AddTransient method is used to map abstract types to concrete services that are instantiated separately for every object that requires it.

Refer: Asp.Net Dependency Injection

Vivek Sharma
  • 1,912
  • 1
  • 13
  • 15
1

Transient means you have as many objects as you called Ioc container. It also means you have to dispose all IDisposable items you created.

PerWebRequest means you will have 1 instance for every request, so if you have multiple requests running on server, each one will have its own object instance. IDisposable objects may be disposed by IoC framework.

dlxeon
  • 1,932
  • 1
  • 11
  • 14