Я хотел бы создать файл отображения памяти между пользовательским модулем и веб-службами, развернутыми в IIS. Реализация выглядит следующим образом:
- Сначала создайте файл отображения памяти из модуля / веб-службы.
- Затем откройте этот существующий файл из веб-приложения и обновите значение в отображенный в память файл в соответствии с некоторыми условиями
- Затем считайте значение из памяти, сопоставленной из пользовательского модуля.
Иногда это работает правильно, но иногда исключение FileNotFoundException вызывается из OpenExisting со стороны веб-службы. Может ли кто-нибудь иметь идеи для моих проблем?
Модуль
public class TestModuleMemory
{
static MemoryMappedViewAccessor accessor = null;
// Call whenever module is initialized
public static void Start()
{
Create();
Open();
}
// Create memory mapped file
private static void Create()
{
MemoryMappedFileSecurity sec = new MemoryMappedFileSecurity();
sec.AddAccessRule(new AccessRule<MemoryMappedFileRights>(new
SecurityIdentifier(WellKnownSidType.WorldSid, null), MemoryMappedFileRights.FullControl,
AccessControlType.Allow));
MemoryMappedFile mappedFile = MemoryMappedFile.CreateNew(@"Global\TestMap", 1024,
MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, mSec, HandleInheritability.Inheritable);
}
// Open and create view accessor
private static void Open()
{
MemoryMappedFile mappedFile = MemoryMappedFile.OpeningExisting(@"Global\TestMap", MemoryMappedFileRights.FullControl, HandleInheritability.Inheritable);
accessor = mappedFile.CreateViewAccessor();
}
// Call from begin request whenever request is accepted
// Read from memory mapped file
private static int Read()
{
accessor.ReadInt32(0);
}
}
Веб-сервис
public class TestService
{
static MemoryMappedViewAccessor accessor = null;
public void Start()
{
try
{
MemoryMappedFile mappedFile = MemoryMappedFile.OpeningExisting(@"Global\TestMap", MemoryMappedFileRights.FullControl, HandleInheritability.Inheritable)
accessor = mappedFile.CreateViewAccessor();
}
catch(FileNotFoundException)
{
// getting the error from here.
// If new mapped file with same map name is created from, file is created without any error but that new created file is different from the one created by module.
}
while(condition)
{
if(condition)
{
int value = condition1 ? 0 : 1;
accessor.Write(0,value);
}
}
}
}