New functionality
I have just checked in an updated version of Sapit (Codeplex). Included now is the Retry attribute, that automatically retries failed method calls.
How to use Sapit
I figured i was about time I threw in a little documentation on how to apply Sapit to your code. The table below describes each of the 5 attributes and their parameters.
Atrtibute | Parameter | Meaning |
---|---|---|
Cache | Caches the result of the method call. | |
CacheSeconds | The number of seconds to cache the result | |
CircuitBreaker | Monitors the method for exceptions, and if too many consecutive calls fails, it shuts off the method for a period of time. | |
MaxConsecutive-Failures | The maximum number of consecutive failures before the method is shut off | |
ShutOffPeriodSeconds | The number of seconds to leave the method shut off | |
BehaviorWhen-ShutOff | How to react to method calls when shut off, return af default value or throw an exception | |
ShutOffReturnValue | The default value (or exception message) to return | |
ResponseTimeThrottle | Throttles away calls to a method when the response time rises above a given threshold | |
ThrottleThreshold-Milliseconds | The maximum response time before throttling sets in | |
SkipCallsWhen-Throttling | How many calls to throttle away when throttling | |
BehaviorWhen-Throttling | How to react to method calls when throttling, return af default value or throw an exception | |
ThrottleReturnValue | Caches the result of the method callThe default value (or exception message) to return | |
Retry | Retries failed method calls up to a given number of times before giving up. | |
TimesToRetry | The maximum number of times to retry the metod call before giving up | |
Timeout | Aborts method calls taking longer than a given limit. | |
TimeoutMilliseconds | The number of milliseconds before aborting the method call |
Each of the above attributes are defined in each of the assemblies Sapit.PostSharp.dll, Sapit.Unity.dll and Sapit.Windsor.dll, and they functionality in the assembly Sapit.dll. This means, that when you wish to include Sapit in your project, you only need to add a reference to Sapit.dll and to Sapit.<your IoC>.dll, where <your IoC> is your IoC of choice, PostSharp, Unity or Windsor. After adding these two references, you just apply the appropriate attributes to the methods you want. The only thing left is to configure your IoC (this is only true of Unity and Windsor, since PostSharp simply alters the MSIL after compilation).
Configure your IoC to use Sapit
Unity
Use the following code to configure Unity to use Sapit, where IMyInterface is the interface for the class MyImplementation:IUnityContainer container = new UnityContainer();
ServiceProvider.IUC = container;
container.AddNewExtension<Interception>();
container.RegisterType<IMyInterface, MyImplementation>();
container.Configure<Interception>().SetInterceptorFor<IMyInterface>(new TransparentProxyInterceptor());
Windsor
Use the following code to configure Windsor to use Sapit, where IMyInterface is the interface for the class MyImplementation:WindsorContainer ioc = new WindsorContainer();
ServiceProvider.IWC = ioc;
ioc.Register(Component.For<CacheInterceptor<string>>());
ioc.Register(Component.For<CircuitBreakerInterceptor>());
ioc.Register(Component.For<ResponseTimeThrottleInterceptor>());
ioc.Register(Component.For<TimeoutInterceptor>());
ioc.Register(Component.For<RetryInterceptor>());
ioc.Register(Component.For<IMyInterface>()
.ImplementedBy<MyImplementation>()
.Interceptors<CacheInterceptor<string>>()
.Interceptors<CircuitBreakerInterceptor>()
.Interceptors<ResponseTimeThrottleInterceptor>()
.Interceptors<TimeoutInterceptor>()
.Interceptors<RetryInterceptor>());
Feedback?
Please let me know if you find bugs in the code, errors in the above documentation, or if there is something unclear.
Also, I am very open to suggestions and requests for improvements and more features.
Enjoy :-)
Ingen kommentarer:
Send en kommentar