这是一个与辅助角色托管的虚拟机有关的问题。我有一个简单的辅助角色,它跨越了其中的一个流程。生成的进程是 32 位编译的 TCPServer 应用程序。辅助角色中定义了终结点,TCP服务器绑定到辅助角色的端点。因此,当我连接到我的辅助角色端点并发送一些东西时,TCPserver 会收到它,处理它返回一些东西。因此,这里向外界公开的辅助角色的端点在内部连接到TCPserver。
string port = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[""TCPsocket].IPEndpoint.Port.ToString();
var myProcess = new Process()
{
StartInfo = new ProcessStartInfo(Path.Combine(localstorage.RootPath, "TCPServer.exe"))
{
CreateNoWindow = true,
UseShellExecute = true,
WorkingDirectory = localstorage.RootPath,
Arguments = port
}
};
它工作正常。但突然断绝了脚步,回应道。签入门户时,VM 角色正在自动重启。但它从未成功。它显示了Role Initializing..
状态。手动停止和启动也不起作用。我重新部署了相同的包,而代码没有任何更改。这次部署本身失败了。
Warning: All role instances have stopped
- There was no endpoint listening at https://management.core.windows.net/<SubscriptionID>/services/hostedservices/TCPServer/deploymentslots/Production that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
但是一段时间后,我再次尝试部署,它工作正常。谁能告诉我会有什么问题?
更新:
public override void Run()
{
Trace.WriteLine("RasterWorker entry point called", "Information");
string configVal = RoleEnvironment.GetConfigurationSettingValue("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString");
CloudStorageAccount _storageAccount = null;
_storageAccount = CloudStorageAccount.Parse(configVal); // accepts storage cridentials and create storage account
var localstorage = RoleEnvironment.GetLocalResource("MyLocalStorage");
CloudBlobClient _blobClient = _storageAccount.CreateCloudBlobClient();
bool flag = false;
while (true)
{
Thread.Sleep(30000);
if (!flag)
{
if (File.Exists(Path.Combine(localstorage.RootPath, "test.ppm")))
{
CloudBlobContainer _blobContainer = _blobClient.GetContainerReference("reports");
CloudBlob _blob = _blobContainer.GetBlobReference("test.ppm");
_blob.UploadFile(Path.Combine(localstorage.RootPath, "test.ppm"));
Trace.WriteLine("Copy to blob done!!!!!!!", "Information");
flag = true;
}
else
{
Trace.WriteLine("Copy Failed-> File doesnt exist!!!!!!!", "Information");
}
}
Trace.WriteLine("Working", "Information");
}
}
若要防止重启辅助角色,需要阻止入口点类的 Run 方法。
如果重写 Run 方法,则代码应阻止 无限期。如果 Run 方法返回,则通过引发 Stop 事件并调用 OnStop 方法自动回收角色 以便可以在角色之前执行关闭序列 离线。
http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.serviceruntime.roleentrypoint.run.aspx
您需要确保无论发生什么情况,如果要使角色保持活动状态,则永远不会从 Run 方法返回。
现在,如果您在控制台应用程序中承载 TCPServer(我假设您在粘贴 Process.Start 代码后正在执行此操作),则需要在启动进程后阻止 Run 方法。
public override void Run()
{
try
{
Trace.WriteLine("WorkerRole entrypoint called", "Information");
var myProcess = new Process()
{
StartInfo = new ProcessStartInfo(Path.Combine(localstorage.RootPath, "TCPServer.exe"))
{
CreateNoWindow = true,
UseShellExecute = true,
WorkingDirectory = localstorage.RootPath,
Arguments = port
}
};
myProcess.Start();
while (true)
{
Thread.Sleep(10000);
Trace.WriteLine("Working", "Information");
}
// Add code here that runs in the role instance
}
catch (Exception e)
{
Trace.WriteLine("Exception during Run: " + e.ToString());
// Take other action as needed.
}
}
PS:这与您的部署问题无关,我认为这是巧合