/// <summary> /// Initializes a new instance of the <see cref="ServiceMetaData"/> class. /// </summary> /// <param name="service">The service type.</param> /// <param name="rootPath">The root path.</param> public ServiceMetaData(Type service, string rootPath) { this.Path = service.GetPath(); this.RootPath = rootPath; this.EndPoints = EndPointMetaData.Create(service).ToList(); this.ServiceType = service; this.Name = service.Name; var attribute = service.GetAllAttributes <EndPointAttribute>().FirstOrDefault(); if (attribute != null) { this.Name = attribute.Name ?? this.Name; } }
/// <summary> /// Creates endpoint metadata for the specified service. /// </summary> /// <param name="service">The owning service.</param> /// <param name="rootPath">The root path.</param> /// <returns>Returns endpoint metadata for the specified service.</returns> public static IEnumerable <EndPointMetaData> Create(Type service, string rootPath = ServiceHost.LocalPath) { var interfaces = service.GetInterfaces().Where(e => e.GetTypeInfo().IsGenericType&& (e.GetGenericTypeDefinition() == typeof(IEndPoint <>) || e.GetGenericTypeDefinition() == typeof(IEndPoint <,>))).ToList(); if (interfaces.Any()) { var path = service.GetPath(); var version = service.GetVersion(); var summary = service.GetComments(); var timeout = service.GetTimeout(); foreach (var item in interfaces) { var method = item.GetMethod("Receive"); if (method.DeclaringType != null) { var map = service.GetInterfaceMap(method.DeclaringType); var index = Array.IndexOf(map.InterfaceMethods, method); var m = map.TargetMethods[index]; var attribute = m?.GetCustomAttributes <EndPointAttribute>().FirstOrDefault(); if (!string.IsNullOrWhiteSpace(attribute?.Path)) { path = attribute.Path; } var requestType = method.GetParameters().FirstOrDefault()?.ParameterType; var endPoint = new EndPointMetaData { Path = path, ServiceType = service, RequestType = requestType, ResponseType = method.ReturnType, Rules = requestType?.GetRules().Select(e => new EndPointRule(e)).ToList(), Version = version, RequestProperties = requestType?.GetInputProperties().ToList(), Summary = summary?.Summary, RootPath = rootPath, Timeout = timeout, Method = method, Public = service.GetAllAttributes <EndPointAttribute>().FirstOrDefault()?.Public ?? true }; yield return(endPoint); } } } }