Migration from Saraff.IoCX
Saraff.IoCX (netstandard2.0) is superseded by Saraff.IoC 2.x, which ships both
net40 and netstandard2.0 assemblies under the Saraff.IoC package ID.
Package replacement
dotnet remove package Saraff.IoCX
dotnet add package Saraff.IoC
<!-- Before -->
<PackageReference Include="Saraff.IoCX" Version="1.x" />
<!-- After -->
<PackageReference Include="Saraff.IoC" Version="2.0.0" />
The namespace Saraff.IoC and the ServiceContainer type are unchanged. Existing code that
uses Bind, Load, CreateInstance and [BindService] continues to work.
flowchart LR
Old["Saraff.IoCX 1.x<br/>netstandard2.0"] --> New["Saraff.IoC 2.x<br/>net40 + netstandard2.0"]
New --> Legacy["Legacy API<br/>Bind / Load / CreateInstance"]
New --> Modern["Modern API<br/>Register / Scopes / Async"]
What changed in 2.0
Added
IServiceRegistryandIServiceContainerwithRegister,Resolve,ResolveAll,ResolveNamed,IsRegisteredandCreateScope.Singleton,ScopedandTransientlifetimes.- Scopes and nested containers with deterministic disposal.
- Multiple registrations, named registrations, open generics and
IEnumerable<T>. - POCO registrations (no
IComponentrequirement forRegister). ServiceContainerConfigurationand configuration groups.CircularDependencyExceptionandServiceResolutionException.- Async initialization (
IAsyncInitializable,ResolveAsync,InitializeAsync). - Async disposal (
DisposeAsync) fornetstandard2.0. AsyncLazy<T>.IContainerCoreand replaceable policies (IServiceLifetime,IServiceRegistration,IInvocationResult,IAsyncInitializationAdapter).
Changed
- A single multi-target project
Saraff.IoC.csproj(net40;netstandard2.0).Saraff.IoCX.csproj,Saraff.IoC.nuspecandpackages.configare removed. - Versioning is centralized in
Directory.Build.props. Disposeis idempotent; resolution after disposal throwsObjectDisposedException.- Resolution is thread-safe; cycle detection spans nested containers.
- Interception fixes: non-generic proxies,
out/refpropagation, explicit interface implementations, generic methods and a configurable service-required attribute. IConfigurationis an aggregate marker; settings live in group interfaces.- Configuration is read through an immutable
ContainerSettingssnapshot. ServiceRegistrationis no longer public; the core usesIServiceRegistration.ServiceContainerimplementsIContainerCore.
Breaking
- Members of
IConfigurationmoved to base group interfaces. Binary compatibility with older assemblies is broken; recompile against 2.x. ServiceRegistration.Lifetimeis nowIServiceLifetime(the type is internal).
API mapping
1.x / Saraff.IoCX |
2.x |
|---|---|
Bind<IService, Service>() |
Register<IService, Service>() (POCO friendly) or keep Bind |
Bind<IService>(instance) |
RegisterInstance<IService>(instance) or keep Bind |
Load(assembly) |
Load(assembly) or explicit Register calls |
CreateInstance<T>() |
CreateInstance<T>() (still available) |
| Singleton only | Singleton, Scoped, Transient, custom IServiceLifetime |
| No scopes | CreateScope() / CreateNestedContainer() |
IConfiguration with all members |
IConfiguration + configuration groups |
Listener returns object |
Listener returns InvocationResult |
| No async startup | IAsyncInitializable, ResolveAsync (netstandard2.0) |
| No async disposal | DisposeAsync (netstandard2.0) |
Migration steps
- Replace the package reference.
- Recompile. Fix any code that implemented the old
IConfigurationshape. - Replace listener implementations that returned
objectwithInvocationResult. - Move new code to
Registerand scopes. - Keep
Bindfor existingComponent-based code; migrate gradually. - If you target
netstandard2.0, useDisposeAsyncwhen services hold async resources.
Listener migration
Before:
public object OnInvoking(MethodBase method, object instance, object[] parameters) {
if(method.Name == "MethodA") {
return "short";
}
return null; // null means "not handled"
}
public object OnInvoked(MethodBase method, object instance, object result) {
return result;
}
public Exception OnCatch(MethodBase method, object instance, Exception ex) {
return null; // null means "not handled"
}
After:
public InvocationResult OnInvoking(MethodBase method, object instance, object[] parameters) {
if(method.Name == "MethodA") {
return InvocationResult.FromResult("short");
}
return InvocationResult.NotHandled;
}
public InvocationResult OnInvoked(MethodBase method, object instance, object result) {
return InvocationResult.NotHandled;
}
public InvocationResult OnCatch(MethodBase method, object instance, Exception ex) {
return InvocationResult.NotHandled;
}
The new result type removes the “null means not handled” ambiguity: a listener can now
explicitly return null as a handled result.
Configuration migration
Old code implemented IConfiguration with all properties. New code can:
- keep
IConfigurationand implement the base groups; - use
ServiceContainerConfigurationor compose it; - add only the optional group interfaces you need to
IConfiguration.
var configuration = new ServiceContainerConfiguration {
ServiceRequiredAttributeType = typeof(MyInjectAttribute),
ListenerType = typeof(IMyListener)
};
See Configuration.
Compatibility notes
net40has no async initialization/disposal.AsyncLazy<T>is still available.RegisterInstanceandBind(instance)do not transfer ownership; the caller disposes the instance.Resolve<T>()returnsnullfor unregistered services. CheckIsRegisteredif that is not expected.BindrequiresIComponent;Registerdoes not.