using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.ServiceModel.Web; using System.Text; using System.Threading.Tasks; namespace BeWo.Service.ServiceUtils { public class UriTemplateResolver { private UriTemplateTable _uriTemplateTable; public UriTemplateResolver(Uri baseAddress, Type serviceType) { _uriTemplateTable = new UriTemplateTable(baseAddress); foreach (var method in serviceType.GetMethods()) { var webGet = method.GetCustomAttribute(); var webInvoke = method.GetCustomAttribute(); if (webGet != null) { _uriTemplateTable.KeyValuePairs.Add(new KeyValuePair( new UriTemplate(webGet.UriTemplate), method.Name)); } else if (webInvoke != null) { _uriTemplateTable.KeyValuePairs.Add(new KeyValuePair( new UriTemplate(webInvoke.UriTemplate), method.Name)); } } _uriTemplateTable.MakeReadOnly(false); } public string ResolveMethod(string requestUri) { var match = _uriTemplateTable.MatchSingle(new Uri(requestUri)); return match?.Data as string; } } }