Changed NSWSendService dbh to use WinSCP .NET Wrapper and API
This commit is contained in:
parent
ab04e3ce58
commit
332780e6eb
@ -12,6 +12,8 @@
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@ -50,6 +52,9 @@
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="WinSCPnet, Version=1.14.0.13797, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\WinSCP.6.1.2\lib\net40\WinSCPnet.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\bsmd.database\Properties\AssemblyProductInfo.cs">
|
||||
@ -88,6 +93,13 @@
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="..\packages\WinSCP.6.1.2\build\WinSCP.targets" Condition="Exists('..\packages\WinSCP.6.1.2\build\WinSCP.targets')" />
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
<PropertyGroup>
|
||||
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('..\packages\WinSCP.6.1.2\build\WinSCP.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\WinSCP.6.1.2\build\WinSCP.targets'))" />
|
||||
</Target>
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="log4net" version="2.0.15" targetFramework="net48" />
|
||||
<package id="WinSCP" version="6.1.2" targetFramework="net48" />
|
||||
</packages>
|
||||
@ -10,6 +10,10 @@
|
||||
using log4net;
|
||||
using System.Diagnostics;
|
||||
using bsmd.database;
|
||||
using WinSCP;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace bsmd.dakosy
|
||||
{
|
||||
@ -107,16 +111,102 @@ namespace bsmd.dakosy
|
||||
winscp.StartInfo.RedirectStandardOutput = true;
|
||||
winscp.StartInfo.RedirectStandardError = true;
|
||||
winscp.StartInfo.CreateNoWindow = true;
|
||||
winscp.EnableRaisingEvents = true;
|
||||
winscp.OutputDataReceived += (s, e) => { if (!e.Data.IsNullOrEmpty()) _log.Debug(e.Data); };
|
||||
winscp.ErrorDataReceived += (s, e) => { if (!e.Data.IsNullOrEmpty()) _log.Warn(e.Data); };
|
||||
//winscp.EnableRaisingEvents = true;
|
||||
//winscp.OutputDataReceived += (s, e) => { if (!e.Data.IsNullOrEmpty()) _log.Debug(e.Data); };
|
||||
//winscp.ErrorDataReceived += (s, e) => { if (!e.Data.IsNullOrEmpty()) _log.Warn(e.Data); };
|
||||
winscp.Start();
|
||||
winscp.BeginErrorReadLine();
|
||||
winscp.BeginOutputReadLine();
|
||||
//winscp.BeginErrorReadLine();
|
||||
//winscp.BeginOutputReadLine();
|
||||
|
||||
return winscp;
|
||||
}
|
||||
|
||||
public static void TransmitAllFiles(string remoteDir, string localDir, string host, string user, string keyPath, string hostkey)
|
||||
{
|
||||
try
|
||||
{
|
||||
SessionOptions sessionOptions = new SessionOptions();
|
||||
|
||||
sessionOptions.Protocol = Protocol.Sftp;
|
||||
sessionOptions.HostName = host;
|
||||
sessionOptions.UserName = user;
|
||||
sessionOptions.SshPrivateKeyPath = keyPath;
|
||||
sessionOptions.SshHostKeyFingerprint = hostkey;
|
||||
|
||||
using (Session session = new Session())
|
||||
{
|
||||
session.Open(sessionOptions);
|
||||
|
||||
// upload
|
||||
TransferOptions transferOptions = new TransferOptions();
|
||||
transferOptions.TransferMode = TransferMode.Binary;
|
||||
|
||||
TransferOperationResult transferResult = session.PutFiles(localDir, remoteDir, false, transferOptions);
|
||||
|
||||
transferResult.Check();
|
||||
|
||||
foreach(TransferEventArgs tea in transferResult.Transfers.Cast<TransferEventArgs>())
|
||||
{
|
||||
_log.InfoFormat("Upload of {0} successful", tea.FileName);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
_log.Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ReceiveAllFiles(string remoteDir, string localDir, string host, string user, string keyPath, string hostkey)
|
||||
{
|
||||
try
|
||||
{
|
||||
SessionOptions sessionOptions = new SessionOptions();
|
||||
|
||||
sessionOptions.Protocol = Protocol.Sftp;
|
||||
sessionOptions.HostName = host;
|
||||
sessionOptions.UserName = user;
|
||||
sessionOptions.SshPrivateKeyPath = keyPath;
|
||||
sessionOptions.SshHostKeyFingerprint = hostkey;
|
||||
|
||||
using (Session session = new Session())
|
||||
{
|
||||
session.Open(sessionOptions);
|
||||
|
||||
// upload
|
||||
TransferOptions transferOptions = new TransferOptions();
|
||||
transferOptions.TransferMode = TransferMode.Binary;
|
||||
|
||||
TransferOperationResult transferResult = session.GetFiles(remoteDir, localDir, false, transferOptions);
|
||||
transferResult.Check();
|
||||
|
||||
foreach (TransferEventArgs tea in transferResult.Transfers.Cast<TransferEventArgs>())
|
||||
{
|
||||
_log.InfoFormat("Download of {0} successful", tea.FileName);
|
||||
}
|
||||
|
||||
if (transferResult.Transfers.Count > 0)
|
||||
{
|
||||
RemovalOperationResult removalResult = session.RemoveFiles(remoteDir + "/*.*");
|
||||
if ((removalResult.IsSuccess) && (removalResult.Removals.Count > 0))
|
||||
{
|
||||
_log.InfoFormat("Removed {0} downloaded files on remote host");
|
||||
}
|
||||
if (!removalResult.IsSuccess)
|
||||
{
|
||||
_log.WarnFormat("RemoveFiles was not successful, {0} failures", removalResult.Failures.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void TransmitAll(string remoteDir, string localDir, Direction direction, string openCommand)
|
||||
{
|
||||
Process winscp = StartWinSCPProcess();
|
||||
@ -133,10 +223,20 @@ namespace bsmd.dakosy
|
||||
winscp.StandardInput.WriteLine("get *.xml");
|
||||
if(direction == Direction.OUTGOING)
|
||||
winscp.StandardInput.WriteLine("put *.xml");
|
||||
winscp.StandardInput.Close();
|
||||
winscp.StandardInput.Close();
|
||||
|
||||
// Collect all output
|
||||
string output = winscp.StandardOutput.ReadToEnd();
|
||||
_log.Debug(output);
|
||||
output = winscp.StandardError.ReadToEnd();
|
||||
if (!output.Trim().IsNullOrEmpty())
|
||||
_log.Warn(output);
|
||||
// Wait until WinSCP finishes
|
||||
winscp.WaitForExit();
|
||||
if(winscp.ExitCode != 0)
|
||||
{
|
||||
_log.Warn("WinSCP exited with an error");
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveProcessedFile(string remoteDir, string filename, string openCommand)
|
||||
|
||||
@ -117,7 +117,10 @@ namespace bsmd.dbh
|
||||
public static void SendAndReceive()
|
||||
{
|
||||
// sent unsent messages in output folder
|
||||
bsmd.dakosy.SFtp.TransmitAll(Properties.Settings.Default.RemoteIncomingFolder, Properties.Settings.Default.OutgoingFolder, dakosy.SFtp.Direction.OUTGOING, Properties.Settings.Default.SFTPOpenCommand);
|
||||
dakosy.SFtp.TransmitAllFiles(Properties.Settings.Default.RemoteIncomingFolder, Properties.Settings.Default.OutgoingFolder,
|
||||
Properties.Settings.Default.SFTPHost, Properties.Settings.Default.SFTPUsername, Properties.Settings.Default.SFTPrivateKeyPath, Properties.Settings.Default.SFTPHostKey);
|
||||
|
||||
//bsmd.dakosy.SFtp.TransmitAll(Properties.Settings.Default.RemoteIncomingFolder, Properties.Settings.Default.OutgoingFolder, dakosy.SFtp.Direction.OUTGOING, Properties.Settings.Default.SFTPOpenCommand);
|
||||
// move files from output folder to archive folder
|
||||
foreach(string sentFile in Directory.GetFiles(Properties.Settings.Default.OutgoingFolder))
|
||||
{
|
||||
@ -130,8 +133,10 @@ namespace bsmd.dbh
|
||||
|
||||
// receive files from remote host
|
||||
// SFTP verbindung öffnen und alle Dateien herunterladen
|
||||
bsmd.dakosy.SFtp.TransmitAll(Properties.Settings.Default.RemoteOutgoingFolder, Properties.Settings.Default.IncomingFolder, dakosy.SFtp.Direction.INCOMING, Properties.Settings.Default.SFTPOpenCommand);
|
||||
|
||||
//bsmd.dakosy.SFtp.TransmitAll(Properties.Settings.Default.RemoteOutgoingFolder, Properties.Settings.Default.IncomingFolder, dakosy.SFtp.Direction.INCOMING, Properties.Settings.Default.SFTPOpenCommand);
|
||||
dakosy.SFtp.ReceiveAllFiles(Properties.Settings.Default.RemoteOutgoingFolder, Properties.Settings.Default.IncomingFolder, Properties.Settings.Default.SFTPHost,
|
||||
Properties.Settings.Default.SFTPUsername, Properties.Settings.Default.SFTPrivateKeyPath, Properties.Settings.Default.SFTPHostKey);
|
||||
|
||||
foreach (string inputFile in Directory.GetFiles(Properties.Settings.Default.IncomingFolder))
|
||||
{
|
||||
string justFilename = Path.GetFileName(inputFile);
|
||||
@ -150,8 +155,9 @@ namespace bsmd.dbh
|
||||
File.Copy(inputFile, archivePath, true);
|
||||
File.Delete(inputFile);
|
||||
}
|
||||
|
||||
// remote Dateien löschen
|
||||
bsmd.dakosy.SFtp.RemoveProcessedFile(Properties.Settings.Default.RemoteOutgoingFolder, Path.GetFileName(inputFile), Properties.Settings.Default.SFTPOpenCommand);
|
||||
// bsmd.dakosy.SFtp.RemoveProcessedFile(Properties.Settings.Default.RemoteOutgoingFolder, Path.GetFileName(inputFile), Properties.Settings.Default.SFTPOpenCommand);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
51
bsmd.dbh/Properties/Settings.Designer.cs
generated
51
bsmd.dbh/Properties/Settings.Designer.cs
generated
@ -12,7 +12,7 @@ namespace bsmd.dbh.Properties {
|
||||
|
||||
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.10.0.0")]
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.5.0.0")]
|
||||
public sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
|
||||
|
||||
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
|
||||
@ -88,18 +88,7 @@ namespace bsmd.dbh.Properties {
|
||||
|
||||
[global::System.Configuration.ApplicationScopedSettingAttribute()]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Configuration.DefaultSettingValueAttribute("dbh")]
|
||||
public string SFTPOpenCommand {
|
||||
get {
|
||||
return ((string)(this["SFTPOpenCommand"]));
|
||||
}
|
||||
}
|
||||
|
||||
[global::System.Configuration.ApplicationScopedSettingAttribute()]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Configuration.DefaultSettingValueAttribute("sftp://f-000333@fx.dbh.de/ -hostkey=\"ecdsa-sha2-nistp256 256 453JBU8hAOOQ7wEvmTw5" +
|
||||
"fXsUyZsVCSufyATbGKLh+ak\" -privatekey=\"C:\\work\\Services\\SendNSWMessageService\\dbh" +
|
||||
"\\sshkeynp.ppk\"")]
|
||||
[global::System.Configuration.DefaultSettingValueAttribute("e:\\temp\\dbh\\in_error")]
|
||||
public string IncomingErrorFolder {
|
||||
get {
|
||||
return ((string)(this["IncomingErrorFolder"]));
|
||||
@ -114,5 +103,41 @@ namespace bsmd.dbh.Properties {
|
||||
return ((string)(this["SenderMaersk"]));
|
||||
}
|
||||
}
|
||||
|
||||
[global::System.Configuration.ApplicationScopedSettingAttribute()]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Configuration.DefaultSettingValueAttribute("fx.dbh.de")]
|
||||
public string SFTPHost {
|
||||
get {
|
||||
return ((string)(this["SFTPHost"]));
|
||||
}
|
||||
}
|
||||
|
||||
[global::System.Configuration.ApplicationScopedSettingAttribute()]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Configuration.DefaultSettingValueAttribute("C:\\work\\Services\\SendNSWMessageService\\dbh\\sshkeynp.ppk")]
|
||||
public string SFTPrivateKeyPath {
|
||||
get {
|
||||
return ((string)(this["SFTPrivateKeyPath"]));
|
||||
}
|
||||
}
|
||||
|
||||
[global::System.Configuration.ApplicationScopedSettingAttribute()]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Configuration.DefaultSettingValueAttribute("ecdsa-sha2-nistp256 256 453JBU8hAOOQ7wEvmTw5fXsUyZsVCSufyATbGKLh+ak")]
|
||||
public string SFTPHostKey {
|
||||
get {
|
||||
return ((string)(this["SFTPHostKey"]));
|
||||
}
|
||||
}
|
||||
|
||||
[global::System.Configuration.ApplicationScopedSettingAttribute()]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Configuration.DefaultSettingValueAttribute("f-000333")]
|
||||
public string SFTPUsername {
|
||||
get {
|
||||
return ((string)(this["SFTPUsername"]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,14 +23,23 @@
|
||||
<Setting Name="RemoteOutgoingFolder" Type="System.String" Scope="Application">
|
||||
<Value Profile="(Default)">test/dbh_2_bsmd</Value>
|
||||
</Setting>
|
||||
<Setting Name="SFTPOpenCommand" Type="System.String" Scope="Application">
|
||||
<Value Profile="(Default)">dbh</Value>
|
||||
</Setting>
|
||||
<Setting Name="IncomingErrorFolder" Type="System.String" Scope="Application">
|
||||
<Value Profile="(Default)">sftp://f-000333@fx.dbh.de/ -hostkey="ecdsa-sha2-nistp256 256 453JBU8hAOOQ7wEvmTw5fXsUyZsVCSufyATbGKLh+ak" -privatekey="C:\work\Services\SendNSWMessageService\dbh\sshkeynp.ppk"</Value>
|
||||
<Value Profile="(Default)">e:\temp\dbh\in_error</Value>
|
||||
</Setting>
|
||||
<Setting Name="SenderMaersk" Type="System.String" Scope="Application">
|
||||
<Value Profile="(Default)">00006017</Value>
|
||||
</Setting>
|
||||
<Setting Name="SFTPHost" Type="System.String" Scope="Application">
|
||||
<Value Profile="(Default)">fx.dbh.de</Value>
|
||||
</Setting>
|
||||
<Setting Name="SFTPrivateKeyPath" Type="System.String" Scope="Application">
|
||||
<Value Profile="(Default)">C:\work\Services\SendNSWMessageService\dbh\sshkeynp.ppk</Value>
|
||||
</Setting>
|
||||
<Setting Name="SFTPHostKey" Type="System.String" Scope="Application">
|
||||
<Value Profile="(Default)">ecdsa-sha2-nistp256 256 453JBU8hAOOQ7wEvmTw5fXsUyZsVCSufyATbGKLh+ak</Value>
|
||||
</Setting>
|
||||
<Setting Name="SFTPUsername" Type="System.String" Scope="Application">
|
||||
<Value Profile="(Default)">f-000333</Value>
|
||||
</Setting>
|
||||
</Settings>
|
||||
</SettingsFile>
|
||||
@ -28,15 +28,24 @@
|
||||
<setting name="RemoteOutgoingFolder" serializeAs="String">
|
||||
<value>test/dbh_2_bsmd</value>
|
||||
</setting>
|
||||
<setting name="SFTPOpenCommand" serializeAs="String">
|
||||
<value>dbh</value>
|
||||
</setting>
|
||||
<setting name="IncomingErrorFolder" serializeAs="String">
|
||||
<value>sftp://f-000333@fx.dbh.de/ -hostkey="ecdsa-sha2-nistp256 256 453JBU8hAOOQ7wEvmTw5fXsUyZsVCSufyATbGKLh+ak" -privatekey="C:\work\Services\SendNSWMessageService\dbh\sshkeynp.ppk"</value>
|
||||
<value>e:\temp\dbh\in_error</value>
|
||||
</setting>
|
||||
<setting name="SenderMaersk" serializeAs="String">
|
||||
<value>00006017</value>
|
||||
</setting>
|
||||
<setting name="SFTPHost" serializeAs="String">
|
||||
<value>fx.dbh.de</value>
|
||||
</setting>
|
||||
<setting name="SFTPrivateKeyPath" serializeAs="String">
|
||||
<value>C:\work\Services\SendNSWMessageService\dbh\sshkeynp.ppk</value>
|
||||
</setting>
|
||||
<setting name="SFTPHostKey" serializeAs="String">
|
||||
<value>ecdsa-sha2-nistp256 256 453JBU8hAOOQ7wEvmTw5fXsUyZsVCSufyATbGKLh+ak</value>
|
||||
</setting>
|
||||
<setting name="SFTPUsername" serializeAs="String">
|
||||
<value>f-000333</value>
|
||||
</setting>
|
||||
</bsmd.dbh.Properties.Settings>
|
||||
</applicationSettings>
|
||||
</configuration>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user