Files
BeWoPlaner/Service/Core/ServiceHelper.cs
Rene Evertz 2832bb9c5e Merge branch 'master' into feature_rene_18_ai - kein kommentar...
# Conflicts:
#	BeWo/BeWo.csproj
#	BeWo/BeWoApp.xaml.cs
#	BeWo/ServiceProxy/GeneratedAiEnhancedService.cs
#	BeWo/Services/BeWoControlFactory.cs
#	BeWo/Services/BeWoWindowFactory.cs
#	BeWo/Services/BeWoWindowService.cs
#	BeWo/Services/IControlFactory.cs
#	BeWo/Services/IWindowFactory.cs
#	BeWo/Services/IWindowService.cs
#	BeWo/View/BeWoFileView.xaml
#	BeWo/View/Detail/Report/AbwesenheitsView.xaml
#	BeWo/View/Windows/AnimatedBeWoWindow.xaml.cs
#	BeWo/app.config
#	BeWoAllReports.sln
#	BeWoPlanerMobil.sln
#	BeWoPlanerMobil/.vs/BeWoPlanerMobil.csproj.dtbcache.json
#	BeWoTests.sln
#	Dakota/DakotaUtils.cs
#	Dakota/Logic/DakotaInfoCreator.cs
#	Dakota/Models/DakotaFile.cs
#	Data/Access/GenericDAO.cs
#	Data/Access/SearchDAO.cs
#	Data/Data.csproj
#	Host/Web.config
#	Server/Components/ServerUtils/ApiFacade/WebClientFacade.cs
#	Service/BeWoServiceEnums.cs
#	Service/Core/ServiceHelper.cs
#	Service/Core/ServiceValidator.cs
#	Service/Extensions/ServiceEnumExtension.cs
#	Service/Service.csproj
#	Service/ServiceContracts/Enhanced/IAiEnhancedService.cs
#	Service/ServiceContracts/Enhanced/IOperationsEnhancedService.cs
#	Service/ServiceImplementations/EmployeeServiceImp.cs
#	Service/ServiceImplementations/Enhanced/AiEnhancedServiceImp.cs
#	Service/ServiceImplementations/Enhanced/OperationsEnhancedServiceImp.cs
#	Service/ServiceProxy/OpenWebUIFacade.cs
#	Service/ServiceProxy/ServiceFacade.cs
#	Service/ServiceUtils/DistanceCalculator/GoogleDistanceMatrixAPI.cs
#	Shared/BeWoEntityEnums.cs
#	Shared/Core/AppError.cs
#	Shared/Core/Facade/WebClientFacade.cs
#	Shared/Core/WebClientFacade.cs
#	Shared/Shared.csproj
2025-11-28 12:07:35 +01:00

130 lines
4.0 KiB
C#

using System;
using System.Reflection;
using System.Runtime.Remoting.Channels;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;
using BeWo.Service.ServiceContracts;
using System.ServiceModel.Dispatcher;
using BeWo.Service.Attributes;
using BeWo.Service.ServiceUtils;
using BS.Shared.Extensions;
namespace BeWo.Service.Core
{
public static class ServiceHelper
{
public static FileLogger CoreLogger { get; } = new FileLogger(MergedConfig.GetSetting(WebConfigSetting.ServiceLogFilePath));
public static FileLogger WCFErrorLogger { get; } = new FileLogger(MergedConfig.GetSetting(WebConfigSetting.WCFErrorLogFilePath));
public static FileLogger JsonErrorLogger { get; } = new FileLogger(MergedConfig.GetSetting(WebConfigSetting.JsonErrorLogFilePath));
public static Tuple<string, string> GetInterfaceAndMethodWCF(Message request, IClientChannel channel)
{
var lTemp = request.Headers.Action.Split("/");
string lMethodName = lTemp[lTemp.Count - 1];
string lServiceInterface = lTemp[lTemp.Count - 2];
return new Tuple<string, string>(lServiceInterface, lMethodName);
}
public static Tuple<string, string> GetInterfaceAndMethodASP(Message request, IClientChannel channel)
{
// Hole Interface
string lInterface = null;
var segments = channel.LocalAddress.Uri.Segments;
for (int i = 0; i < segments.Length; i++)
{
var seg = segments[i].Trim('/');
if (seg.EndsWith(".svc"))
{
lInterface = "I" + seg.Substring(0, seg.Length - 4);
}
}
if (string.IsNullOrWhiteSpace(lInterface))
{
throw new ArgumentException($"Zugriff verweigert. Interface unbekannt");
}
// Hole Methode
string lMethod = request.Properties[WebHttpDispatchOperationSelector.HttpOperationNamePropertyName] as string;
if (string.IsNullOrWhiteSpace(lMethod))
{
throw new ArgumentException($"Zugriff verweigert. Methode unbekannt");
}
return new Tuple<string, string>(lInterface, lMethod);
}
public static T GetCustomAttribute<T>(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<T>(methodInfo);
}
public static T GetCustomAttribute<T>(Message request, IClientChannel channel) where T : Attribute
{
var (inter, method) = GetInterfaceAndMethodASP(request, channel);
var authentification = GetCustomAttribute<T>(inter, method);
return authentification;
}
public static T GetCustomAttribute<T>(string pServiceInterface, string pMethodName) where T : Attribute
{
MethodInfo methodInfo = null;
try
{
// Hole entsprechende Methode
var name = $"BeWo.Service.ServiceContracts.{pServiceInterface}";
var t = Type.GetType(name);
methodInfo = t?.GetMethod(pMethodName);
}
catch (Exception)
{
throw new ArgumentException($"Zugriff verweigert. Interface oder Methode unbekannt");
}
return getCustomAttribute<T>(methodInfo);
}
private static T getCustomAttribute<T>(MethodInfo methodInfo) where T : Attribute
{
if (methodInfo == null)
{
throw new ArgumentException($"Zugriff verweigert. Interface oder Methode unbekannt");
}
// Hole Custome Attribute
var attribute = methodInfo.GetCustomAttribute<T>();
return attribute;
}
}
}