#if Middleware using System; using System.Collections.Generic; using System.Text; namespace Apewer.Web { /// API 服务描述。 public sealed class ApiServiceDescriptor : IToJson { /// 订阅器的生命周期。 public ApiServiceLifetime Lifetime { get; private set; } /// 服务的类型。 public Type ServiceType { get; private set; } /// 实现服务的类型。 public Type ImplementationType { get; private set; } public object ImplementationInstance { get; } public Func ImplementationFactory { get; } /// 创建订阅器的实例。 /// API 调用器。 /// 订阅器的生命周期。 /// 服务的类型。 /// 实现服务的类型。 /// /// internal ApiServiceDescriptor(ApiServiceLifetime lifetime, Type service, Type implementation) { if (invoker == null) throw new ArgumentNullException(nameof(invoker)); if (service == null) throw new ArgumentNullException(nameof(service)); if (implementation == null) throw new ArgumentNullException(nameof(implementation)); if (!service.IsAssignableFrom(implementation)) throw new ArgumentException($"类型 {implementation.Name} 未实现服务。"); if (!implementation.IsClass) throw new ArgumentException($"实现服务的类型 {implementation.Name} 不是引用类型。"); if (implementation.IsAbstract) throw new ArgumentException($"实现服务的类型 {implementation.Name} 是抽象类型,无法实例化。"); Lifetime = lifetime; ServiceType = service; ImplementationType = implementation; } /// 创建订阅器的实例。 /// API 调用器。 /// 订阅器的生命周期。 /// 服务的类型。 /// 实现服务的方法。 /// /// internal ApiServiceDescriptor(ApiServiceLifetime lifetime, Type service, Func生成 JSON 实例。 public Json ToJson() { var json = new Json(); json["Lifetime"] = Lifetime.ToString(); json["Service"] = ServiceType.FullName; json["Implementation"] = Implementation.FullName; return json; } } } #endif