Files
BeWoPlaner/Service/ServiceUtils/UriTemplateResolver.cs

47 lines
1.2 KiB
C#

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<WebGetAttribute>();
var webInvoke = method.GetCustomAttribute<WebInvokeAttribute>();
if (webGet != null)
{
_uriTemplateTable.KeyValuePairs.Add(new KeyValuePair<UriTemplate, object>(
new UriTemplate(webGet.UriTemplate), method.Name));
}
else if (webInvoke != null)
{
_uriTemplateTable.KeyValuePairs.Add(new KeyValuePair<UriTemplate, object>(
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;
}
}
}