Sftp Retry Logic Inside Azure function App [duplicate] - c#

This question already has answers here:
Automatically resume SFTP download after network connection is restored with SSH.NET
(1 answer)
Retry SFTP if it fails?
(2 answers)
Closed 28 days ago.
I am facing one issue ,where i am getting intermittent error message
"An existing connection was forcibly closed by the remote host"
while trying to connect sftp server. I am trying to connect sftp inside azure function app. I believe this error happens while there is a connection issue.
To handle that I need to implement a retry logic incase if such error happens repeatedly connect sftp server with a specified delay until configured maximum retry count is reached. I believe the maximum execution time allowed for function app is 5 minutes (need to hear it from azure expert).
Can anyone help how i can achieve this retry functionality in C#?
I am using Renci.SshNet nuget package for managing sftp
using (var _sftpCn = new SftpClient(host, port, userName, password))
{
_sftpCn.Connect();//error happens on this line:"An existing connection was forcibly closed by the remote host"
log.LogInformation("Successful");
byte[] byteArray = Encoding.UTF8.GetBytes(datacontent);
sftpCl.WriteAllBytes("{remotePath}", byteArray);
log.LogInformation("Sent successfully");
_sftpCn.Disconnect();
}

I would suggest to use Polly here.
Polly is a .NET resilience and transient-fault-handling library that
allows developers to express policies such as Retry, Circuit Breaker,
Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe
manner.
It is free library can be downloaded as nuget package from this command:
dotnet add package Polly
There are many more things to it, please see here and here for more details and examples.

Related

Upload bytes to a an SFTP server from an AWS Lambda function in .NET Core 3.1

I keep searching everywhere and I cannot find an answer so my last resort is posting here.
My requirements are that I need to SFTP a file from an S3 bucket using a Lambda written in C# (.NET Core 3.1). Don't ask why or suggest alternatives... I wasn't the one to set the requirements. So I have written the code that does it using the SSH.NET library which works when running the lambda code using the Mock Lambda Test Tool on my local machine. However, the same code deployed to AWS times out after 30 seconds. The Lambda is in a VPC but an EC2 within the same VPC and Security Group is able to access the SFTP server so I don't think it's a network related issue.
Here is the code I'm using to upload:
try
{
using (var sftpClient = new SftpClient(server, port, username, password))
{
sftpClient.Connect();
if (sftpClient.IsConnected)
{
sftpClient.WriteAllText(path, contents);
LambdaLogger.Log($"Uploaded file to {server}");
}
else
{
LambdaLogger.Log($"Could not connect to the {server} SFTP server.");
}
sftpClient.Disconnect();
}
}
catch (Exception e)
{
LambdaLogger.Log($"An error has occurred while uploading to the {server} SFTP server. Error: {e.Message}");
}
Edit: The error I'm seeing in the lambda logs is Connection timing out after 30 seconds. That's the exception message.
The issue was the VPC. When removing the VPC configuration and just trying to upload some basic text instead of accessing the databases within the VPC it was able to connect and not timeout.
You CAN use SSH.NET to create an SFTP connection and upload files to a server from a Lambda function.

Entity Framework connection on iOS Xamarin closing when screen is turned off [duplicate]

This question already has an answer here:
Entity Framework - How to clear connection pool manually? SNIX_Excecute Error
(1 answer)
Closed 3 years ago.
I have an Xamarin application that uses Entity Framework.
It works great on UWP however when on iOS if I lock the phone, then unlock minutes later I get the following error when loading data from or to the database:
Snix_Execute (provider: SNI_PN7, error: 35 - SNI_ERROR_35) Snix_Execute (provider: SNI_PN7, error: 35 - SNI_ERROR_35)
With an inner exception of:
Unable to write data to the transport connection: The socket has been shut down.
I think it's to do with iOS closing the connection as part of cleanup but how do I reopen the connection? What other information can I provide to help solve this issue? I know I can use the OnResume method to reopen the connection but what's the code to actually reopen the connection?
EDIT:
The issue was todo with pooling meaning that the connections were basically being cached even though when the phone turned off, they were closed. To overcome this you can either turn of pooling in the connection string by adding Pooling=False; to the string:
Post
Or clear the pools everytime that the app wakes up on the OnResumeMethod:
SqlConnection.ClearAllPools();
Post

Tamir SharpSSH not able to connect again. error "SSH_MSG_DISCONNECT: 2 Too many users for this IP"

I am facing strange issue in Tamir SharpSSH while connecting it. first time when I connect SFTP using below code, it connect successfully. but when this method calls again it breaks into this error : SSH_MSG_DISCONNECT: 2 Too many users for this IP
public bool getDir(string directory){
Sftp s = new Sftp(ConfigurationManager.AppSettings["SFTPUrl"], username, password);
s.Connect();
s.Mkdir(directory);
s.Close();
}
This issue happens only at my production environment not from my localhost.
please help me here.
Hard to say without more information, but it looks to me like the SFTP server is preventing you from connecting too often. Maybe you can keep the Sftp object around instead of creating/closing it every time?

Windows Service 100% CPU with C# IMAP Chilkat

Chilkat IMAP service is stuck on 100% CPU after several days the Windows service that uses it executes normally. It is being reproduced every several days (3-7 days).
I'm using Chilkat IAMP for .NET 4.5 version 9.5.0 64-bit.
The way I abort through c# code is (it runs once on every end of iteration):
if (imapCon != null)
{
if (imapCon.IsLoggedIn())
{
imapCon.Logout();
}
if (imapCon.IsConnected())
{
imapCon.Disconnect();
}
imapCon.Dispose();
imapCon = null;
}
From the logs, I get: WSAECONNABORTED An established connection was aborted by the software in your host machine.
The service that runs it resides in a virtual cloud environment.
Is this an issue with how Chilkat IMAP connection is implemented, the cloud environment implementation or something from my service (the application that uses the Chilkat module)...?
The following are Chilkat logs:
DllDate: May 6 2014
ChilkatVersion: 9.5.0.38
UnlockPrefix: SNILIKIMAPMAIL
Username: WIN-OCJD4A0985E:SYSTEM
Architecture: Little Endian; 64-bit
Language: .NET 4.5 / x64
VerboseLogging: 0
listMailboxes:
bSubscribedOnly: 0 reference:
mailbox: *
Escaping quotes and backslashes in mailbox name...
utf7EncodedMailboxPath: *
getCompleteResponse:
WindowsError: An established connection was aborted by the software in your host machine.
WindowsErrorCode: 0x2745
numBytesRequested: 5
Failed to receive data on the TCP socket
Failed to read beginning of SSL/TLS record.
Failed to read incoming handshake messages. (3)
(leaveContext)
Client handshake failed. (3)
(leaveContext)
ConnectFailReason: 0
(leaveContext) failReason: 0
connect failed.
(leaveContext) Login:
DllDate: May 6 2014
ChilkatVersion: 9.5.0.38
UnlockPrefix: SNILIKIMAPMAIL
Username: WIN-OCJD4A0985E:SYSTEM
Architecture: Little Endian; 64-bit
Language: .NET 4.5 / x64
VerboseLogging: 0
login: **
ConnectionType: SSL/TLS
Error sending on socket (1)
SocketError: WSAECONNABORTED An established connection was aborted by
the software in your host machine.
For more information see this Chilkat Blog post:
http://www.cknotes.com/?p=91
send_size: 90
Failed to send TLS message.
Failed to send LOGIN command
Failed.
If a Chilkat method call never returns and utilizes 100% of the CPU, then you would not be able to get the contents of the LastErrorText (which is the Chilkat log you have provided). The fact that you have a LastErrorText indicates that the Chilkat method call has returned and your app then proceeded to display the LastErrorText.
My guess is that your app has a loop where normally a Chilkat method call involving communications with an IMAP mail server succeeds (with a normal amount of time spent communicating with the server), but then for some reason the method call begins returning immediately with a failed status. At that point, I suspect your application is probably in a tight loop calling the Chilkat method over and over. The 100% CPU utilization is likely caused by the loop in your app, NOT by code within a call to a Chilkat method.

Configuring mq websphere 7 with .net

I am trying to connect to a remote queue using c#.
I tried many ways to connect to the remote queue but it always fails with common errors like: MQRC_CHANNEL_CONFIG_ERROR or MQRC_HOST_NOT_AVAILABLE.
What I am doing is this:
string channel = "QM_TEST.SVRCONN";
string hostname = "<serverIp>";
string queueName = "QM_TEST";
string port = 1414;
props.Add(MQC.HOST_NAME_PROPERTY, hostname);
props.Add(MQC.CHANNEL_PROPERTY, channel);
props.Add(MQC.PORT_PROPERTY, port );
props.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
MQQueueManager mqQueue = new MQQueueManager(queueName, props);
I have tried changing this but all failed.
I think that my problem is the server configurations..
can you point me to a full guide to how to configure a server and connect to it with .net?
My problem is connecting to a REMOTE server using .net and not to a local server.
Thank you!
The problem was that the CCSID between the client and the server were different.
http://publib.boulder.ibm.com/infocenter/wmqv7/v7r0/index.jsp?topic=%2Fcom.ibm.mq.csqzaf.doc%2Fcs12480_.htm
On the client side I had to put
Environment.SetEnvironmentVariable("MQCCSID", "437");
Thats why I got:
MQRC_CHANNEL_CONFIG_ERROR
I'm guessing the problem (or at least a problem) is here:
MQQueue mqQueue = new MQQueueManager(queueName, props);
This should be
queueManager = new MQQueueManager(queueManagerName, properties);
If you have installed the WebSphere MQ client to the default location, there are many sample programs under the following directory:
C:\Program Files (x86)\IBM\WebSphere MQ\tools\dotnet\samples\cs\base\
There are a number of sample programs there for various tasks. If you have the latest V7.1 client installed then you will see the following programs:
SimpleAsyncPut
SimpleClientAutoReconnectGet
SimpleClientAutoReconnectPut
SimpleGet
SimpleMessageProperties
SimplePublish
SimplePut
SimpleReadAhead
SimpleSharingConversation
SimpleSubscribe
SimpleXAGet
SimpleXAPut
There are also WCF and XMS samples.
If you need the client code, please see my response to another SO question here for links.
Update:
Here's the normal diagnostic process.
If the WMQ components were installed by relocating libraries or classes from somewhere else, perform an install using the full vendor-supplied client media. This includes troubleshooting utilities such as trace, dspmqver, etc. It also resolves any library or class mismatch issues.
Use the pre-compiled client programs to test the connection. The amqsputc, amqsgetc and amqsbcgc programs require the MQSERVER environment variable as described here. The Q program from SupportPac MA01 is a separate download but has the advantage of NOT requiring any environment variables, CCDT files or other dependencies.
If the sample programs fail, check the QMgr's error logs at [WMQ install]/qmgrs/[QMgr name]/errors/AMQERR01.LOG for messages. Also check for FDC files and errors in [WMQ install]/errors.
If no errors on the QMgr side, attempt the connection again while using a client-side trace as described here and here.
Most client problems are resolved through installation of the full WMQ client as supplied by IBM. (Conversely that implies most people are installing by grabbing DLL or JAR files.) If the problem persists, error log inspection on the QMgr and client side usually reveals the underlying cause. If these do not work then tracing usually diagnoses the remaining issues.
UPDATE 2:
Per the error messages posted at MQSeries.net, the channel has a security exit set. A security exit is external code that the channel calls out to when starting a channel. There is no way to know what the exit expects or does without having access to the code or docs of the exit. If the exit is written in-house, you'll need to talk to the programmer to figure out what it requires. If the exit is a commercial product then you will need to get the documentation for it.
Alternatively, alter the channel so that SCYEXIT is blank to disable the exit.
The data posted at MQSeries.net was as follows:
MQ9575: DCE Security: failed to get the user's login name.
EXPLANATION:
System call 192.168.50.55 to get the login name of the user running WebSphere
MQ client application process 5 failed with error value -1. This occurred in
security exit function create_cred. The exit will now attempt to open channel
using the DCE default login context.
ACTION:
If you wish to run using the DCE default login context take no action. If you
wish to run using the user's login name as the DCE security exit principal
examine the documentation for the operating system on which you are running MQ
clients and reconfigure the operating system as necessary to allow the
192.168.50.55 call to succeed.
Note that it states the call is failing in the security exit.

Categories

Resources