嗨我與框架4.0工作,微軟的Ioc實踐團結 在主程序中我加載的所有服務時創建的服務,我有兩個參數WCF:傳遞參數的ServiceHost
(IUnitOfWork unitOfWork,IRepository<Persona> personaRepository)
在這種情況下
。
在主程序中,如何在創建時將這兩個參數傳遞給ServiceHost
?
ServiceHost host = new ServiceHost(service)?
服務:
public class RegService : IRegService
{
private IUnitOfWork unitOfWork;
private IRepository<Persona> personaRepository;
public RegService(IUnitOfWork unitOfWork,IRepository<Persona> personaRepository){
this.unitOfWork = unitOfWork;
this.personaRepository = personaRepository;
}
public IEnumerable<Persona> ListaPersonas(){
return personaRepository.GetAll();
}
public int PersonaInsert(Persona oPersona){
personaRepository.Add(oPersona);
return oPersona.IdPersona;
}
}
主要:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting services...");
Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);
ServiceElementCollection serviceSection = serviceModel.Services.Services;
foreach (ServiceElement objService in serviceModel.Services.Services){
Type service = Type.GetType(objService.Name + ", Mistic.Contador.Services.Implementation");
{
ServiceHost host = new ServiceHost(service);
host.Open();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Service started: " + objService.Endpoints[0].Contract);
}
}
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Services are ready... Press enter to close the services.");
Console.ReadLine();
}
}
當我在此行中執行host.Open()我有這樣的信息:
服務類型提供不能作爲服務加載,因爲它沒有默認(參數呃更少)的構造函數。要解決該問題,請向該類型添加默認構造函數,或將該類型的實例傳遞給主機。
更新引導(統一)
public static class Bootstrapper{
public static IUnityContainer Initialise(){
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
return container;
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
string connectionString = "ModelMovieSolContainer";container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager(), new InjectionConstructor(connectionString));
container.RegisterType(typeof(IGenericRepository<>), typeof(GenericRepository<>));
container.BindInRequestScope<IMovieService, MovieService>();
return container;
}
}
你不能 - 'ServiceHost'類沒有任何構造函數來接受你的參數... – 2014-10-04 09:12:26
@marc_s你能告訴我一些解決方案來解決這個問題嗎?如果我不能使用ServiceHost – raranibar 2014-10-04 09:59:39
您在解決方案中提到了有關Microsoft統一性的內容,但是我沒有看到您在哪裏使用它。你應該使用一個特殊的InstanceProvider(你自己的或者使用任何可用的塊)來工作。 – Roman 2014-10-04 16:34:57