I am using .NET 4.5, C#.NET, WCF Repository Pattern
WCF Service
<@ ServiceHost Language="C#" Service="CustomerService" @>
<@ ServiceHost Language="C#" Service="OrderService" @>
<@ ServiceHost Language="C#" Service="SalesService" @>
<@ ServiceHost Language="C#" Service="ProductsService" @>
Implementing classes
public class CustomerService : Service<Customer>, ICustomerService.cs
{
private readonly IRepositoryAsync<Customer> _repository;
public CustomerService(IRepositoryAsync<Customer> repository)
{
_repository = repository;
}
}
public class OrdersService : Service<Orders>, IOrdersService.cs
{
private readonly IRepositoryAsync<Order> _repository;
public OrdersService(IRepositoryAsync<Order> repository)
{
_repository = repository;
}
}
public class SalesService : Service<Sales>, ISalesService.cs
{
private readonly IRepositoryAsync<Sales> _repository;
public SalesService(IRepositoryAsync<Sales> repository)
{
_repository = repository;
}
}
When I run my WCF service, I get a error like there is no empty constructor. How can I keep these services and implementing classes unchanged and have my WCF service work with those constructors.
WCF itself requires the Service Host to have a default/no-argument constructor; the standard WCF service creation implementation does no fancy activation - and it most certainly does not handle Dependency Injection! - when creating the Service Host object.
To bypass this default requirement, use a WCF Service Host Factory (such as one provided with Castle Windsor WCF Integration ) to create the service and inject dependencies using the appropriate constructor. Other IoCs provided their own integration factories. In this case the IoC-aware service factory creates the service and wires up the dependencies.
To use DI without an IoC (or otherwise dealing with a Service Factory), create a no-argument constructor that invokes the constructor with the required dependencies, eg
public class SalesService : Service<Sales>, ISalesService
{
private readonly IRepositoryAsync<Sales> _repository;
// This is the constructor WCF's default factory calls
public SalesService() : this(new ..)
{
}
protected SalesService(IRepositoryAsync<Sales> repository)
{
_repository = repository;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.