Mr *_*ush 5 .net c# wcf process ioexception
我们有一个具有以下架构的客户端应用程序:一个管理器进程管理几个工作进程(读取器和写入器)并定期向服务器查询版本更新。如果版本更新可用,管理器将其下载到客户端计算机,关闭工作线程,启动更新程序进程来处理更新并退出。更新程序在启动时接收管理器 PID 和更新文件位置;然后等待管理器退出,备份管理器和工人的所有文件,重新创建他们的目录并将新版本文件传播到新目录。
当按照所述进行此过程时,第一次调用Directory.Move(string, string)(用于备份管理器目录)会抛出IOException. 奇怪的是,如果我让管理器关闭而不启动更新程序,然后自己启动更新程序可执行文件,则不会抛出异常。
用于管理工作线程的管理器代码:
public void Run()
{
_config = GetConfiguration();
Process reader, writer;
//Start reader and writer with appropriate arguments
//Keep reader and writer alive
reader.Kill();
writer.Kill();
reader.WaitForExit();
writer.WaitForExit();
reader.Dispose();
writer.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
用于查询数据库的管理器代码:
EndpointAddress endpoint;
BasicHttpBinding httpBinding = new BasicHttpBinding();
httpBinding.MaxReceivedMessageSize = 2000000000;
ChannelFactory<IService> chanFactory = new ChannelFactory<IService>(httpBinding);
IService service;
try
{
endpoint = new EndpointAddress(ConfigurationManager.AppSettings["Service URL"]);
service = chanFactory.CreateChannel(endpoint);
UpdateInstructions instructions = service.GetUpdateInstructions(_config.SiteID, Assembly.GetExecutingAssembly().GetName().Version.ToString(), _config.Version);
HandleUpdateInstructions(instructions); //Downloads files and starts the updater process
}
catch (Exception ex)
{
//Report exception
}
finally
{
if (chanFactory.State != CommunicationState.Faulted)
chanFactory.Close();
}
Run Code Online (Sandbox Code Playgroud)
启动更新程序的管理器代码:
private void StartUpdater(string updateFilePath, string configFilePath)
{
ProcessStartInfo updaterStartInfo = new ProcessStartInfo(_config.UpdaterExePath, string.Format("{0} \"{1}\" \"{2}\"", Process.GetCurrentProcess().Id, updateFilePath, configFilePath));
Process updater = Process.Start(updaterStartInfo);
updater.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
等待管理器关闭的更新程序代码:
bool isManagerUp = true;
while (isManagerUp)
{
try
{
Process managerProcess = Process.GetProcessById(bDoxForceManagerPID);
managerProcess.WaitForExit();
managerProcess.Dispose();
isManagerUp = false;
}
catch
{
isManagerUp = false;
}
}
Run Code Online (Sandbox Code Playgroud)
用于更新模块的更新程序代码:
//updateDirectory is the directory of the new files to be inserted, moduleDirectory is the working directory of the module that will be updated, in this case the manager
private void UpdateModule(DirectoryInfo updateDirectory, DirectoryInfo moduleDirectory)
{
string backupDirectory = MakeBackupDirectoryFullPath(moduleDirectory.Parent.FullName);
Directory.Move(moduleDirectory.FullName, backupDirectory); // IOException as described above.
Directory.CreateDirectory(moduleDirectory.FullName);
foreach (FileInfo updateFile in updateDirectory.EnumerateFiles())
{
string newFilePath = moduleDirectory.FullName + "\\" + updateFile.Name;
File.Copy(updateFile.FullName, newFilePath);
}
Directory.Delete(updateDirectory.FullName, true);
}
Run Code Online (Sandbox Code Playgroud)
感谢Adam Caviness 的回答,我们才得以弄清楚。
我们的进程是控制台应用程序,它们创建了一个 .vshost 文件,该文件在进程被命令终止后继续工作。尝试移动正在运行的 .vshost 文件的目录导致了该问题。
将进程转换为 Windows 服务不会创建 .vshost 文件并解决了此问题。
| 归档时间: |
|
| 查看次数: |
4595 次 |
| 最近记录: |