c# - WCF Service with parameterized constructor -
i using .net 4.5, c#.net, wcf repository pattern
wcf service
- customer.svc:
<@ servicehost language="c#" service="customerservice" @>
- order.svc:
<@ servicehost language="c#" service="orderservice" @>
- sales.svc:
<@ servicehost language="c#" service="salesservice" @>
- products.svc:
<@ 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 run wcf service, error there no empty constructor. how can keep these services , implementing classes unchanged , have wcf service work constructors.
wcf requires service host have default/no-argument constructor; standard wcf service creation implementation no fancy activation - , not handle dependency injection! - when creating service host object.
to bypass default requirement, use wcf service host factory (such 1 provided castle windsor wcf integration) create service , inject dependencies using appropriate constructor. other iocs provided own integration factories. in case ioc-aware service factory creates service , wires dependencies.
to use di without ioc (or otherwise dealing service factory), create no-argument constructor invokes constructor required dependencies, e.g.
public class salesservice : service<sales>, isalesservice { private readonly irepositoryasync<sales> _repository; // constructor wcf's default factory calls public salesservice() : this(new ..) { } protected salesservice(irepositoryasync<sales> repository) { _repository = repository; } }
Comments
Post a Comment