Вы можете попробовать пользовательскую авторизацию. См. Код ниже.
[HttpPost]
[CustomAuthorization(Foo.bar)]
public HttpResponseMessage Post([FromBody] Foo foo)
{
if (foo.bar !== user.bar)
{
return;
}
}
public class CustomAuthorizationAttribute : AuthorizeAttribute
{
private readonly string allowedroles;
public CustomAuthorizationAttribute(string roles)
{
this.allowedroles = roles;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
bool authorize = false;
if (Me.bar != allowedroles)
{
authorize = true;
}
return authorize;
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.Result = new HttpUnauthorizedResult();
}
}
Для получения более подробной информации вы можете пройти здесь :
Необходимо снова соединиться с FileSystemWatcher.
Сделайте свою переменную типа FileSystemWatcher глобальный к Вашему классу, добавьте событие WatcherError.
В методе добавьте что-то как этот:
private static void WatcherError(object source, ErrorEventArgs e)
{
watcher = new FileSystemWatcher();//You might want to do a method and to setup all config...
while (!watcher.EnableRaisingEvents)
{
try
{
watcher = new FileSystemWatcher();//You might want to do a method and to setup all config...
}
catch
{
System.Threading.Thread.Sleep(30000); //Wait for retry 30 sec.
}
}
}
Вы не хотите использовать наблюдателя = новый... Вы предпочли бы иметь метод, который добавит все событие и установит путь, но код выше дают Вам хорошую идею того, что сделать.