gpt4 book ai didi

c# - 使用 caSTLe windsor 在控制台应用程序中使用特定命名空间在程序集中安装服务

转载 作者:太空宇宙 更新时间:2023-11-03 13:26:12 26 4
gpt4 key购买 nike

我有以下设置:一个控制台应用程序,以及一个包含一系列服务的服务项目

我正在尝试使用温莎城堡在应用程序中安装特定服务,具体取决于它们在应用程序中的命名空间。

我的应用程序中有以下内容可以正常工作:

Container.Install(Castle.Windsor.Installer.Configuration.FromXmlFile("components.config"));

但是,我无法让“使用特定命名空间注册所有服务才能工作”。

在我之前制作的网络应用程序中,我使用了以下代码。但是,当我在我的控制台应用程序中使用 Container.Register 时,我似乎无法稍后解析这些服务。

我之前在网络应用程序中使用过的代码:

// All services in service DLL
var assembly = Assembly.LoadFrom(Server.MapPath("~/bin/LetterAmazer.Business.Services.dll"));
;
Container.Register(
Classes.FromAssembly(assembly)
.InNamespace("LetterAmazer.Business.Services.Services")
.WithServiceAllInterfaces());

Container.Register(
Classes.FromAssembly(assembly)
.InNamespace("LetterAmazer.Business.Services.Services.FulfillmentJobs")
.WithServiceAllInterfaces());

Container.Register(
Classes.FromAssembly(assembly)
.InNamespace("LetterAmazer.Business.Services.Services.PaymentMethods.Implementations")
.WithServiceAllInterfaces());



// All factories in service DLL
Container.Register(
Classes.FromAssembly(assembly)
.InNamespace("LetterAmazer.Business.Services.Factory")
.WithServiceAllInterfaces());


Container.Register(Component.For<LetterAmazerEntities>());

当我在控制台应用程序中使用 Container.Register 代码时,我无法执行以下操作:

orderService = ServiceFactory.Get<IOrderService>();
fulfillmentService = ServiceFactory.Get<IFulfillmentService>();

当我收到“无法解析服务”时。

代码运行在一个BackgroundService类中,代码如下:

public void Start()
{
logger.Info("Starting Background Service...");
Container.Install(Castle.Windsor.Installer.Configuration.FromXmlFile("components.config"));

ISchedulerFactory scheduleFactory = new StdSchedulerFactory();
scheduler = scheduleFactory.GetScheduler();
scheduler.Start();

ThreadStart threadStart = new System.Threading.ThreadStart(ScheduleJobs);
new Thread(threadStart).Start();

logger.Info("DONE!");
}

总结一下:如何在控制台应用程序的外部 DLL 中注册具有特定命名空间的所有服务/类?

编辑:

服务工厂.cs:

public class ServiceFactory
{
private static ServiceFactory instance = new ServiceFactory();
private IWindsorContainer container = new WindsorContainer();

private ServiceFactory()
{

}

/// <summary>
/// Gets the container.
/// </summary>
/// <value>The container.</value>
public static IWindsorContainer Container
{
get { return instance.container; }
}

/// <summary>
/// Gets this instance.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T Get<T>()
{
return instance.container.Resolve<T>();
}

/// <summary>
/// Gets all this instance.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static IList<T> GetAll<T>()
{
return instance.container.ResolveAll<T>();
}

/// <summary>
/// Gets the specified name.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name">The name.</param>
/// <returns></returns>
public static T Get<T>(string name)
{
return (T)instance.container.Resolve<T>(name);
}

/// <summary>
/// Gets all the specified name.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name">The name.</param>
/// <returns></returns>
public static IList<T> GetAll<T>(string name)
{
return instance.container.ResolveAll<T>(name);
}
}

EDIT-2

如果我尝试在 BackgroundService 中进行解析,在将服务注册到我的容器后,我会得到:

An unhandled exception of type 'Castle.MicroKernel.ComponentNotFoundException' occurred in Castle.Windsor.dll

Additional information: No component for supporting the service LetterAmazer.Business.Services.Domain.Orders.IOrderService was found

但是,我可以在调试中看到 IOrderService 存在于我的容器中的“所有服务”中。

EDIT-3

 public interface IOrderService
{
Order Create(Order order);

Order Update(Order order);
void UpdateByLetters(IEnumerable<Letter> letters);

List<Order> GetOrderBySpecification(OrderSpecification specification);
Order GetOrderById(int orderId);
Order GetOrderById(Guid orderId);

void Delete(Order order);

List<OrderLine> GetOrderLinesBySpecification(OrderLineSpecification specification);

void ReplenishOrderLines(Order order);
}

EDIT-4:订单服务

注意:部分实现已被删减

public class OrderService : IOrderService
{
private IOrderFactory orderFactory;

private LetterAmazerEntities repository;
private ILetterService letterService;
private ICustomerService customerService;

public OrderService(LetterAmazerEntities repository,
ILetterService letterService,
IOrderFactory orderFactory, ICustomerService customerService)
{
this.repository = repository;
this.letterService = letterService;
this.orderFactory = orderFactory;
this.customerService = customerService;
}

public Order Create(Order order)
{
DbOrders dborder = new DbOrders();

foreach (var orderLine in order.OrderLines)
{
var dbOrderLine = setOrderline(orderLine);
dborder.DbOrderlines.Add(dbOrderLine);
}


dborder.Guid = Guid.NewGuid();
dborder.OrderCode = GenerateOrderCode();
dborder.OrderStatus = (int)OrderStatus.Created;
dborder.DateCreated = DateTime.Now;
dborder.DateUpdated = DateTime.Now;
dborder.PaymentMethod = "";
dborder.CustomerId = order.Customer != null ? order.Customer.Id : 0;

Price price = new Price();
price.PriceExVat = order.CostFromLines();
price.VatPercentage = order.Customer.VatPercentage();
order.Price = price;

dborder.Total = order.Price.Total;
dborder.VatPercentage = order.Price.VatPercentage;
dborder.PriceExVat = order.Price.PriceExVat;

repository.DbOrders.Add(dborder);

repository.SaveChanges();

return GetOrderById(dborder.Id);
}

public Order GetOrderById(Guid orderId)
{
DbOrders dborder = repository.DbOrders.FirstOrDefault(c => c.Guid == orderId);
if (dborder == null)
{
throw new ItemNotFoundException("Order");
}

var lines = repository.DbOrderlines.Where(c => c.OrderId == dborder.Id).ToList();
var order = orderFactory.Create(dborder, lines);

return order;
}

public Order GetOrderById(int orderId)
{
DbOrders dborder = repository.DbOrders.FirstOrDefault(c => c.Id == orderId);
if (dborder == null)
{
throw new ItemNotFoundException("Order");
}

var lines = repository.DbOrderlines.Where(c => c.OrderId == orderId).ToList();
var order = orderFactory.Create(dborder, lines);

return order;
}

public void Delete(Order order)
{
var dborder = repository.DbOrders.FirstOrDefault(c => c.Id == order.Id);
repository.DbOrders.Remove(dborder);
repository.SaveChanges();

}
public void DeleteOrder(Order order)
{
var dborder = repository.DbOrders.FirstOrDefault(c => c.Id == order.Id);

repository.DbOrders.Remove(dborder);
repository.SaveChanges();
}




#endregion


}

最佳答案

我认为您也可以在控制台应用程序中做同样的事情。您只需创建程序集实例。考虑到程序集位于同一个文件夹中,这应该可行:

Assembly assembly = Assembly.LoadFrom("MyAssembly.dll");

Container.Register(
Classes.FromAssembly(assembly)
.InNamespace("MyNamespace")
.WithServiceAllInterfaces());

关于c# - 使用 caSTLe windsor 在控制台应用程序中使用特定命名空间在程序集中安装服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22344954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com