Copy file to remote computer with C# script and NAnt ( A specified logon session does not exist )
I'm currently working on a script (using NAnt and C#) running on a TeamCity server, which is to create and deploy an MSI file on a remote computer. The remote computer is set up with a shared folder which the TeamCity server is to connect to and copy the MSI file into, though the script. And it works the first time I run it without any problem.
But when I run the script a second time, it throws an exception with the following the message when it tries to connect to the share on the remote computer;
"A specified logon session does not exist. It may already have been terminated."
I use the solution from this thread to copy the MSI file from the TeamCity server to the remote computer using logon cridentials.
The following is the C# script in my NAnt file which does the file copy:
//Script main entry point
public static void ScriptMain(Project project)
{
NetworkCredential deployTargetCridentials = new NetworkCredential(project.Properties["deploy.remotesvr.username"],
project.Properties["deploy.remotesvr.password"]);
string connection = @"\\" + project.Properties["deploy.remotesvr"] + project.Properties["deploy.remotesvr.sharepath"];
string sourceFile = project.Properties["wix.output.dir"] + @"\" + project.Properties["wix.output.file"] + ".msi";
string destinationFile = @"\\" + project.Properties["deploy.remotesvr"] +
project.Properties["deploy.remotesvr.sharepath"] + @"\" +
project.Properties["deploy.remotesvr.deployfile"];
//Copy installation file to deploy share
Copy(project
, project.Properties["wix.output.dir"]
, project.Properties["wix.output.file"] + ".msi"
, @"\\" + project.Properties["deploy.remotesvr"] + project.Properties["deploy.remotesvr.sharepath"]
, project.Properties["deploy.remotesvr.deployfile"]
, deployTargetCridentials);
////
}
//Copy MSI
public static void Copy(Project project, string sourcePath, string sourceFile, string destinationPath, string destinationFile, NetworkCredential cridentials)
{
string source = sourcePath + @"\" + sourceFile;
string destination = destinationPath + @"\" + destinationFile;
try
{
project.Log(Level.Info, " ");
project.Log(Level.Info, "Copying " + source + " to " + destination);
project.Log(Level.Info开发者_运维知识库, " Connecting to copy share: " + destinationPath);
using (new NetworkConnection(destinationPath, cridentials))
{
project.Log(Level.Info, " Copying file");
File.Copy(source, destination, true);
}
project.Log(Level.Info, "Copy successfull!");
}
catch (Exception ex)
{
project.Log(Level.Warning, "WARNING: Could not copy file: " + ex.Message.ToString().Trim().Replace("\r\n", ""));
}
}
////
//NetworkConnection class
public class NetworkConnection : IDisposable
{
string _networkName;
public NetworkConnection(string networkName, NetworkCredential credentials)
{
_networkName = networkName;
NetResource netResource = new NetResource();
netResource.Scope = ResourceScope.GlobalNetwork;
netResource.ResourceType = ResourceType.Disk;
netResource.DisplayType = ResourceDisplaytype.Share;
netResource.RemoteName = networkName;
int result = WNetAddConnection2(netResource, credentials.Password, credentials.UserName, 0);
if (result != 0 && result != 1219)
{
throw new Win32Exception(result);
}
}
~NetworkConnection()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
WNetCancelConnection2(_networkName, 0, true);
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name, int flags, bool force);
}
[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
public enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};
public enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8
}
public enum ResourceDisplaytype : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}
The exception occurs at the following line in Copy;
using (new NetworkConnection(destinationPath, cridentials))
When I tried to run the same copy code in a test application, it copied the file every time. Its only when I run it through the script that the exception occurs.
Does anyone have any idea to why this is?
Have you tried testing for / closing any existing connections prior to opening a new one?
精彩评论