Reputation: 2628
I have the following four projects:
Service.WCFApplication
-> References
-> Service.WCFLibrary
MainService.svc
Service.WCFLibrary
-> References
-> Service.WCFModels
-> Domain.BusinessLogic
IMainService.cs
MainService.cs
Service.WCFModels
-> Models
GetTestRequest
GetTestResponse
Domain.BusinessLogic
The Service.WCFLibrary contains all ServiceContract
and Implementation.
As an example the IMainService.cs:
[ServiceContract]
public interface IMainService
{
[OperationContract]
[XmlSerializerFormat]
GetTestResponse GetTest(GetTestRequest request);
}
And MainService.cs as an implementation of the interface:
public class MainService : IMainService
{
public GetTestResponse GetTest(GetTestRequest request);
{
//TODO: call business logic
}
}
The Service.WCFApplication
is only a wrapper of the library to host this WCF service on an IIS, so the MainService.svc
only contains this line of code:
<%@ ServiceHost Language="C#" Debug="true" Service="Service.WCFLibrary.MainService" %>
I would like to register my business logic with IoC, but I don't know where to find a point of entry. I added a Startup- or Global.asax class in WCFApplication but it doesn't work.
Where can I register my business logic so that I can consume it in the WCF library?
Upvotes: 1
Views: 1037
Reputation: 128
The easiest way is to use conventions.You can create a folder named App_Startup
in your Service.WCFLibrary
. In this folder you created a startup class like this:
public class Startup
{
private ServiceContainer serviceContainer;
public static void Initialize()
{
this.serviceContainer = new ServiceContainer();
serviceContainer.Register<ISomething, Something>();
//etc...
}
}
In your Service.WCFApplication
you can create a folder named App_Code
with file Initializer.cs
like this:
public class Initializer
{
private ServiceContainer serviceContainer;
public static void AppInitialize()
{
this.serviceContainer = new ServiceContainer();
serviceContainer.Register<ISomething, Something>();
//etc...
}
}
Upvotes: 1
Reputation: 31312
WCF extensibility point for DI is IInstanceProvider
interface:
public interface IInstanceProvider
{
object GetInstance(InstanceContext instanceContext);
object GetInstance(InstanceContext instanceContext, Message message);
void ReleaseInstance(InstanceContext instanceContext, object instance);
}
But usually you don't need to implement it by yourself. There are existing implementations for common DI containers.
For example Unity.WCF for Unity. Here is detailed article how to integrate Unity container it in WCF application.
There are implementations for Castle Windsor, Autofac, etc. Just google and you will find the articles how to use them.
Upvotes: 2