本文整理汇总了C#中Microsoft.AspNet.FileProviders.PhysicalFileProvider.Watch方法的典型用法代码示例。如果您正苦于以下问题:C# PhysicalFileProvider.Watch方法的具体用法?C# PhysicalFileProvider.Watch怎么用?C# PhysicalFileProvider.Watch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.AspNet.FileProviders.PhysicalFileProvider
的用法示例。
在下文中一共展示了PhysicalFileProvider.Watch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Token_Callbacks_Are_Async_And_TokenNotAffected_By_Exceptions
public async Task Token_Callbacks_Are_Async_And_TokenNotAffected_By_Exceptions()
{
var fileName = Guid.NewGuid().ToString();
var fileLocation = Path.Combine(Path.GetTempPath(), fileName);
File.WriteAllText(fileLocation, "Content");
var provider = new PhysicalFileProvider(Path.GetTempPath());
var changeToken = provider.Watch(fileName);
changeToken.RegisterChangeCallback(async _ =>
{
await Task.Delay(10 * 1000);
throw new Exception("Callback throwing exception");
}, null);
File.AppendAllText(fileLocation, "UpdatedContent");
// Wait for callback to be fired.
await Task.Delay(WaitTimeForTokenToFire);
Assert.True(changeToken.HasChanged);
// Verify file system watcher is stable.
int callbackCount = 0;
var changeTokenAfterCallbackException = provider.Watch(fileName);
changeTokenAfterCallbackException.RegisterChangeCallback(_ => { callbackCount++; }, null);
File.AppendAllText(fileLocation, "UpdatedContent");
// Wait for callback to be fired.
await Task.Delay(WaitTimeForTokenToFire);
Assert.True(changeToken.HasChanged);
Assert.Equal(1, callbackCount);
File.Delete(fileLocation);
}
示例2: Triggers_With_Regular_Expression_Pointing_To_SubFolder
public async Task Triggers_With_Regular_Expression_Pointing_To_SubFolder()
{
var subFolderName = Guid.NewGuid().ToString();
var pattern1 = "**/*";
var pattern2 = string.Format("{0}/**/*.cshtml", subFolderName);
var root = Path.GetTempPath();
var fileName = Guid.NewGuid().ToString();
var subFolder = Path.Combine(root, subFolderName);
Directory.CreateDirectory(subFolder);
int pattern1TriggerCount = 0, pattern2TriggerCount = 0;
var provider = new PhysicalFileProvider(root);
var trigger1 = provider.Watch(pattern1);
trigger1.RegisterExpirationCallback(_ => { pattern1TriggerCount++; }, null);
var trigger2 = provider.Watch(pattern2);
trigger2.RegisterExpirationCallback(_ => { pattern2TriggerCount++; }, null);
File.WriteAllText(Path.Combine(root, fileName + ".cshtml"), "Content");
await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);
pattern1TriggerCount.ShouldBe(1);
pattern2TriggerCount.ShouldBe(0);
trigger1 = provider.Watch(pattern1);
trigger1.RegisterExpirationCallback(_ => { pattern1TriggerCount++; }, null);
// Register this trigger again.
var trigger3 = provider.Watch(pattern2);
trigger3.RegisterExpirationCallback(_ => { pattern2TriggerCount++; }, null);
trigger3.ShouldBe(trigger2);
File.WriteAllText(Path.Combine(subFolder, fileName + ".cshtml"), "Content");
await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);
pattern1TriggerCount.ShouldBe(2);
pattern2TriggerCount.ShouldBe(2);
Directory.Delete(subFolder, true);
File.Delete(Path.Combine(root, fileName + ".cshtml"));
}
示例3: Trigger_Fired_On_Directory_Name_Change
public async Task Trigger_Fired_On_Directory_Name_Change()
{
var provider = new PhysicalFileProvider(Path.GetTempPath());
var oldDirectoryName = Guid.NewGuid().ToString();
var newDirectoryName = Guid.NewGuid().ToString();
var oldDirectoryFullPath = Path.Combine(Path.GetTempPath(), oldDirectoryName);
var newDirectoryFullPath = Path.Combine(Path.GetTempPath(), newDirectoryName);
Directory.CreateDirectory(oldDirectoryFullPath);
var oldDirectoryTrigger = provider.Watch("**/" + oldDirectoryName);
var newDirectoryTrigger = provider.Watch("**/" + newDirectoryName);
var oldTriggers = new List<IExpirationTrigger>();
var newTriggers = new List<IExpirationTrigger>();
oldTriggers.Add(provider.Watch(Path.Combine("**", oldDirectoryName, "*.txt")));
newTriggers.Add(provider.Watch(Path.Combine("**", newDirectoryName, "*.txt")));
for (int i = 0; i < 5; i++)
{
var fileName = string.Format("test{0}.txt", i);
File.WriteAllText(Path.Combine(oldDirectoryFullPath, fileName), "test content");
oldTriggers.Add(provider.Watch(Path.Combine("**", oldDirectoryName, fileName)));
newTriggers.Add(provider.Watch(Path.Combine("**", newDirectoryName, fileName)));
}
await Task.Delay(2 * 100); // Give it a while before trying rename.
Directory.Move(oldDirectoryFullPath, newDirectoryFullPath);
// Wait for triggers to fire.
await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);
oldDirectoryTrigger.IsExpired.ShouldBe(true);
newDirectoryTrigger.IsExpired.ShouldBe(true);
oldTriggers.All(t => t.IsExpired).ShouldBe(true);
newTriggers.All(t => t.IsExpired).ShouldBe(true);
newDirectoryTrigger = provider.Watch(newDirectoryName);
newTriggers = new List<IExpirationTrigger>();
newTriggers.Add(provider.Watch(Path.Combine("**", newDirectoryName, "*.txt")));
for (int i = 0; i < 5; i++)
{
var fileName = string.Format("test{0}.txt", i);
newTriggers.Add(provider.Watch(Path.Combine("**", newDirectoryName, fileName)));
}
Directory.Delete(newDirectoryFullPath, true);
// Wait for triggers to fire.
await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);
newDirectoryTrigger.IsExpired.ShouldBe(true);
newTriggers.All(t => t.IsExpired).ShouldBe(true);
}
示例4: Trigger_Fired_For_File_Or_Directory_Create_And_Delete
public async Task Trigger_Fired_For_File_Or_Directory_Create_And_Delete()
{
var root = Path.GetTempPath();
var provider = new PhysicalFileProvider(root);
string fileName = Guid.NewGuid().ToString();
string directoryName = Guid.NewGuid().ToString();
int triggerCount = 0;
var fileTrigger = provider.Watch(fileName);
fileTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null);
var directoryTrigger = provider.Watch(directoryName);
directoryTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null);
fileTrigger.ShouldNotBe(directoryTrigger);
File.WriteAllText(Path.Combine(root, fileName), "Content");
Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), directoryName));
// Wait for triggers to fire.
await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);
triggerCount.ShouldBe(2);
fileTrigger.IsExpired.ShouldBe(true);
directoryTrigger.IsExpired.ShouldBe(true);
fileTrigger = provider.Watch(fileName);
fileTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null);
directoryTrigger = provider.Watch(directoryName);
directoryTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null);
File.Delete(Path.Combine(root, fileName));
Directory.Delete(Path.Combine(Path.GetTempPath(), directoryName));
// Wait for triggers to fire.
await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);
triggerCount.ShouldBe(4);
}
示例5: Triggers_With_Path_Not_Ending_With_Slash
public async Task Triggers_With_Path_Not_Ending_With_Slash()
{
var provider = new PhysicalFileProvider(Path.GetTempPath());
string directoryName = Guid.NewGuid().ToString();
string fileName = Guid.NewGuid().ToString();
int triggerCount = 0;
// Matches file/directory with this name.
var fileTrigger = provider.Watch("/" + directoryName);
fileTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null);
Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), directoryName));
await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);
triggerCount.ShouldBe(1);
// Matches file/directory with this name.
fileTrigger = provider.Watch("/" + fileName);
fileTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null);
File.WriteAllText(Path.Combine(Path.GetTempPath(), fileName), "Content");
await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);
triggerCount.ShouldBe(2);
}
示例6: Trigger_With_MultipleFiles
public async Task Trigger_With_MultipleFiles()
{
var fileName1 = Guid.NewGuid().ToString();
var fileName2 = Guid.NewGuid().ToString();
var fileLocation1 = Path.Combine(Path.GetTempPath(), fileName1);
var fileLocation2 = Path.Combine(Path.GetTempPath(), fileName2);
File.WriteAllText(fileLocation1, "Content1");
File.WriteAllText(fileLocation2, "Content2");
var provider = new PhysicalFileProvider(Path.GetTempPath());
int invocationCount1 = 0, invocationCount2 = 0;
var trigger1 = provider.Watch(fileName1);
trigger1.RegisterExpirationCallback(_ => { invocationCount1++; }, null);
var trigger2 = provider.Watch(fileName2);
trigger2.RegisterExpirationCallback(_ => { invocationCount2++; }, null);
trigger1.ShouldNotBe(null);
trigger1.IsExpired.ShouldNotBe(true);
trigger1.ActiveExpirationCallbacks.ShouldBe(true);
trigger2.ShouldNotBe(null);
trigger2.IsExpired.ShouldNotBe(true);
trigger2.ActiveExpirationCallbacks.ShouldBe(true);
trigger1.ShouldNotBe(trigger2);
File.AppendAllText(fileLocation1, "Update1");
File.AppendAllText(fileLocation2, "Update2");
// Wait for callbacks to be fired.
await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);
invocationCount1.ShouldBe(1);
invocationCount2.ShouldBe(1);
trigger1.IsExpired.ShouldBe(true);
trigger2.IsExpired.ShouldBe(true);
File.Delete(fileLocation1);
File.Delete(fileLocation2);
// Callbacks not invoked on expired triggers.
invocationCount1.ShouldBe(1);
invocationCount2.ShouldBe(1);
}
示例7: ModifyContent_And_Delete_File_Succeeds_And_Callsback_Registered_Triggers
public async Task ModifyContent_And_Delete_File_Succeeds_And_Callsback_Registered_Triggers()
{
var fileName = Guid.NewGuid().ToString();
var fileLocation = Path.Combine(Path.GetTempPath(), fileName);
File.WriteAllText(fileLocation, "OldContent");
var provider = new PhysicalFileProvider(Path.GetTempPath());
var fileInfo = provider.GetFileInfo(fileName);
fileInfo.Length.ShouldBe(new FileInfo(fileInfo.PhysicalPath).Length);
fileInfo.Exists.ShouldBe(true);
IExpirationTrigger trigger3 = null, trigger4 = null;
var trigger1 = provider.Watch(fileName);
var trigger2 = provider.Watch(fileName);
// Valid trigger1 created.
trigger1.ShouldNotBe(null);
trigger1.IsExpired.ShouldBe(false);
trigger1.ActiveExpirationCallbacks.ShouldBe(true);
// Valid trigger2 created.
trigger2.ShouldNotBe(null);
trigger2.IsExpired.ShouldBe(false);
trigger2.ActiveExpirationCallbacks.ShouldBe(true);
// Trigger is the same for a specific file.
trigger1.ShouldBe(trigger2);
trigger1.RegisterExpirationCallback(state =>
{
var infoFromState = state as IFileInfo;
trigger3 = provider.Watch(infoFromState.Name);
trigger3.ShouldNotBe(null);
trigger3.RegisterExpirationCallback(_ => { }, null);
trigger3.IsExpired.ShouldBe(false);
}, state: fileInfo);
trigger2.RegisterExpirationCallback(state =>
{
var infoFromState = state as IFileInfo;
trigger4 = provider.Watch(infoFromState.Name);
trigger4.ShouldNotBe(null);
trigger4.RegisterExpirationCallback(_ => { }, null);
trigger4.IsExpired.ShouldBe(false);
}, state: fileInfo);
// Write new content.
File.WriteAllText(fileLocation, "OldContent + NewContent");
fileInfo.Exists.ShouldBe(true);
// Wait for callbacks to be fired.
await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);
trigger1.IsExpired.ShouldBe(true);
trigger2.IsExpired.ShouldBe(true);
// Trigger is the same for a specific file.
trigger3.ShouldBe(trigger4);
// A new trigger is created.
trigger3.ShouldNotBe(trigger1);
// Delete the file and verify file info is updated.
File.Delete(fileLocation);
fileInfo = provider.GetFileInfo(fileName);
fileInfo.Exists.ShouldBe(false);
new FileInfo(fileLocation).Exists.ShouldBe(false);
// Wait for callbacks to be fired.
await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);
trigger3.IsExpired.ShouldBe(true);
trigger4.IsExpired.ShouldBe(true);
}
示例8: Tokens_With_Path_Ending_With_Slash
public async Task Tokens_With_Path_Ending_With_Slash()
{
var provider = new PhysicalFileProvider(Path.GetTempPath());
string fileName = Guid.NewGuid().ToString();
string folderName = Guid.NewGuid().ToString();
int tokenCount = 0;
var filetoken = provider.Watch("/" + folderName + "/");
filetoken.RegisterChangeCallback(_ => { tokenCount++; }, null);
var folderPath = Path.Combine(Path.GetTempPath(), folderName);
Directory.CreateDirectory(folderPath);
File.WriteAllText(Path.Combine(folderPath, fileName), "Content");
await Task.Delay(WaitTimeForTokenToFire);
Assert.Equal(1, tokenCount);
filetoken = provider.Watch("/" + folderName + "/");
filetoken.RegisterChangeCallback(_ => { tokenCount++; }, null);
File.AppendAllText(Path.Combine(folderPath, fileName), "UpdatedContent");
await Task.Delay(WaitTimeForTokenToFire);
Assert.Equal(2, tokenCount);
filetoken = provider.Watch("/" + folderName + "/");
filetoken.RegisterChangeCallback(_ => { tokenCount++; }, null);
File.Delete(Path.Combine(folderPath, fileName));
await Task.Delay(WaitTimeForTokenToFire);
Assert.Equal(3, tokenCount);
}
示例9: Tokens_With_Path_Not_Ending_With_Slash
public async Task Tokens_With_Path_Not_Ending_With_Slash()
{
var provider = new PhysicalFileProvider(Path.GetTempPath());
string directoryName = Guid.NewGuid().ToString();
string fileName = Guid.NewGuid().ToString();
int tokenCount = 0;
// Matches file/directory with this name.
var filetoken = provider.Watch("/" + directoryName);
filetoken.RegisterChangeCallback(_ => { tokenCount++; }, null);
Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), directoryName));
await Task.Delay(WaitTimeForTokenToFire);
Assert.Equal(1, tokenCount);
// Matches file/directory with this name.
filetoken = provider.Watch("/" + fileName);
filetoken.RegisterChangeCallback(_ => { tokenCount++; }, null);
File.WriteAllText(Path.Combine(Path.GetTempPath(), fileName), "Content");
await Task.Delay(WaitTimeForTokenToFire);
Assert.Equal(2, tokenCount);
}
示例10: Token_For_AbsolutePath_Filters
public void Token_For_AbsolutePath_Filters()
{
var provider = new PhysicalFileProvider(Path.GetTempPath());
var path = Path.Combine(Path.GetTempPath(), "filename");
var token = provider.Watch(path);
Assert.Same(NoopChangeToken.Singleton, token);
}
示例11: Token_Fired_For_File_Or_Directory_Create_And_Delete
public async Task Token_Fired_For_File_Or_Directory_Create_And_Delete()
{
var root = Path.GetTempPath();
var provider = new PhysicalFileProvider(root);
string fileName = Guid.NewGuid().ToString();
string directoryName = Guid.NewGuid().ToString();
int tokenCount = 0;
var filetoken = provider.Watch(fileName);
filetoken.RegisterChangeCallback(_ => { tokenCount++; }, null);
var directorytoken = provider.Watch(directoryName);
directorytoken.RegisterChangeCallback(_ => { tokenCount++; }, null);
Assert.NotEqual(directorytoken, filetoken);
File.WriteAllText(Path.Combine(root, fileName), "Content");
Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), directoryName));
// Wait for tokens to fire.
await Task.Delay(WaitTimeForTokenToFire);
Assert.Equal(2, tokenCount);
Assert.True(filetoken.HasChanged);
Assert.True(directorytoken.HasChanged);
filetoken = provider.Watch(fileName);
filetoken.RegisterChangeCallback(_ => { tokenCount++; }, null);
directorytoken = provider.Watch(directoryName);
directorytoken.RegisterChangeCallback(_ => { tokenCount++; }, null);
File.Delete(Path.Combine(root, fileName));
Directory.Delete(Path.Combine(Path.GetTempPath(), directoryName));
// Wait for tokens to fire.
await Task.Delay(WaitTimeForTokenToFire);
Assert.Equal(4, tokenCount);
}
示例12: Token_For_Whitespace_Filters
public void Token_For_Whitespace_Filters()
{
var provider = new PhysicalFileProvider(Path.GetTempPath());
var token = provider.Watch(" ");
Assert.False(token.HasChanged);
Assert.True(token.ActiveChangeCallbacks);
}
示例13: Token_For_Empty_Filter
public void Token_For_Empty_Filter()
{
var provider = new PhysicalFileProvider(Path.GetTempPath());
var token = provider.Watch(string.Empty);
Assert.False(token.HasChanged);
Assert.True(token.ActiveChangeCallbacks);
}
示例14: Token_For_Null_Filter
public void Token_For_Null_Filter()
{
var provider = new PhysicalFileProvider(Path.GetTempPath());
var token = provider.Watch(null);
Assert.Same(NoopChangeToken.Singleton, token);
}
示例15: FileTrigger_NotTriggered_After_Expiry
public async Task FileTrigger_NotTriggered_After_Expiry()
{
var fileName = Guid.NewGuid().ToString();
var fileLocation = Path.Combine(Path.GetTempPath(), fileName);
File.WriteAllText(fileLocation, "Content");
var provider = new PhysicalFileProvider(Path.GetTempPath());
var expirationTrigger = provider.Watch(fileName);
int invocationCount = 0;
expirationTrigger.RegisterExpirationCallback(_ => { invocationCount++; }, null);
// Callback expected for this change.
File.AppendAllText(fileLocation, "UpdatedContent1");
// Callback not expected for this change.
File.AppendAllText(fileLocation, "UpdatedContent2");
// Wait for callbacks to be fired.
await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);
invocationCount.ShouldBe(1);
File.Delete(fileLocation);
}