using System; using System.Collections.Generic; using System.IdentityModel.Claims; using System.Linq; using System.Reflection; using System.ServiceModel.Channels; using System.Text; using System.Threading.Tasks; using BeWo.Service.ServiceContracts; using BS.Shared.Attributes; using BS.Shared.Extensions; using NHibernate.Hql.Ast.ANTLR.Tree; namespace BeWo.Service.ServiceUtils { public static class ServiceHelper { public static T GetCustomAttribute(Uri baseAddress, Uri request) where T : Attribute { // Hole Interface string lInterface = null; var segments = baseAddress.Segments; for (int i = 0; i < segments.Length; i++) { var seg = segments[i].Trim('/'); if (seg.EndsWith(".svc")) { lInterface = seg.Substring(0, seg.Length - 4); } } // Falls kein Interface gefunden wird if (string.IsNullOrWhiteSpace(lInterface) ) { throw new ArgumentException($"Zugriff verweigert. Interface unbekannt"); } // Wird für URLTemplate != Methodname verwendet var name = $"BeWo.Service.ServiceContracts.I{lInterface}"; var t = Type.GetType(name); var resolver = new UriTemplateResolver(baseAddress, t); var method = resolver.ResolveMethod(request.AbsoluteUri); var methodInfo = t.GetMethod(method); // Rückgabe return getCustomAttribute(methodInfo); } public static T GetCustomAttribute(string pServiceInterface, string pMethodName) where T : Attribute { MethodInfo methodInfo = null; try { // Hole entsprechende Methode var name = $"BeWo.Service.ServiceContracts.I{pServiceInterface}"; var t = Type.GetType(name); methodInfo = t.GetMethod(pMethodName); } catch (Exception) { throw new ArgumentException($"Zugriff verweigert. Interface oder Methode unbekannt"); } return getCustomAttribute(methodInfo); } private static T getCustomAttribute(MethodInfo methodInfo) where T : Attribute { if (methodInfo == null) { throw new ArgumentException($"Zugriff verweigert. Interface oder Methode unbekannt"); } // Hole Custome Attribute var attribute = methodInfo.GetCustomAttribute(); return attribute; } } }