I just created an FTP server based on the code provided here http://www.codeguru.com/csharp/csharp/cs_network/sockets/article.php/c7409/A-C-FTP-Server.htm . Then I installed Nuget packages for FtpClient, so that I can create a test FTP client which connects to FTP server. Following is my code:
[TestMethod]
async public Task TestMethod1() // wanted to make it async as I may connect have something related to database in future
{
var client = new FtpClient
{
Host = "127.0.0.1",
Port = 21,
Credentials = new NetworkCredential
{
UserName = "123",
Password = "123",
},
DataConnectionType = FtpDataConnectionType.PASV // If I don't add this, I get EPSV unknown command
};
await client.ConnectAsync();
Debug.WriteLine("Connected");
client.SetWorkingDirectory(#"C:\Users\myname\Desktop");
Debug.WriteLine(client.GetWorkingDirectory());
try
{
var items = client.GetListing(); // Exception shows up here.
Debug.WriteLine(items.Count());
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
The issue could be that I don't have a command handler for FEAT in my server. But, my FTP server runs completely fine with filezilla. If command handler is required, can someone provide me code for this?
If I ignore this error, I get this exception in test method "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
Any ideas?
[EDIT]
Here's the full exception:
Test method Blackhawk.Core.FtpServer.Test.UnitTest1.TestMethod1 threw exception:
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
at System.Net.FtpClient.FtpSocketStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.FtpClient.FtpSocketStream.ReadLine(Encoding encoding)
at System.Net.FtpClient.FtpClient.GetReply()
at System.Net.FtpClient.FtpClient.Execute(String command)
at System.Net.FtpClient.FtpClient.OpenPassiveDataStream(FtpDataConnectionType type, String command, Int64 restart)
at System.Net.FtpClient.FtpClient.OpenDataStream(String command, Int64 restart)
at System.Net.FtpClient.FtpClient.GetListing(String path, FtpListOption options)
at System.Net.FtpClient.FtpClient.GetListing(String path)
at Blackhawk.Core.FtpServer.Test.UnitTest1.<TestMethod1>d__2.MoveNext() in UnitTest1.cs: line 44
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
I was able to solve the problem by changing data connection type to AutoActive. Here's the final code which works:
[TestMethod]
async public Task FtpTestUser1()
{
var client = new FtpClient
{
Host = "127.0.0.1",
Port = 21,
Encoding = System.Text.Encoding.Default,
Credentials = new NetworkCredential
{
// Storing username, password and default directory in a .dat file
UserName = "123",
Password = "123",
},
DataConnectionType = FtpDataConnectionType.AutoActive // Changed this part.
};
await client.ConnectAsync();
Debug.WriteLine("Connected");
var path = #"Test"; // This path is now relative to the default directory
client.SetWorkingDirectory(path);
Debug.WriteLine(client.GetWorkingDirectory());
var items = client.GetListing();
Assert.IsTrue(items.Any());
Debug.WriteLine(items.Count());
foreach (var ftpListItem in items)
{
Debug.WriteLine(string.Format("File name is {0}", ftpListItem.Name));
}
}
Related
I've implemented the HomeGraph API with the help of the package Google.Apis.HomeGraphService.v1 (1.50.0.2260)
It seems to work fine as well, the ReportStateAndNotification function works fine on the query, execute, and some sync requests.
But when I add a new device to my system through our app and a SYNC request is sent to Google and comes in our backend, the HomeGraph API will return an exception when sending this sync request..
-> The sync request does not throw an exception when I modify a device name in our app. It only occurs when new devices are added.
I've searched through google and multiple StackOverflow posts.. But I'm probably missing something. Most posts say check the API key etc but then the ReportStateAndNotification function should always fail, not only when the sync request comes from Google to our backend.
Could anyone point me in the right direction?
Function that is used for sync requests:
public static void Send(Dictionary<string, object> deviceStateList, string requestId, string googleCustomerId)
{
string deviceIdList = String.Format("({0})", string.Join(", ", deviceStateList.Keys));
try
{
var jsonFilePath = _appSettingsRetriever.PrivateGoogleAuthenticationFile;
string scope = "https://www.googleapis.com/auth/homegraph";
using (var stream = new FileStream(jsonFilePath, FileMode.Open, FileAccess.Read))
{
GoogleCredential credentials = GoogleCredential.FromStream(stream);
if (credentials.IsCreateScopedRequired)
credentials = credentials.CreateScoped(scope);
HomeGraphServiceService service = new HomeGraphServiceService(new BaseClientService.Initializer()
{
HttpClientInitializer = credentials
});
var request = new ReportStateAndNotificationRequest
{
AgentUserId = googleCustomerId,
RequestId = requestId,
Payload = new StateAndNotificationPayload
{
Devices = new ReportStateAndNotificationDevice
{
States = deviceStateList
}
}
};
_log.Debug($"Sending to HomeGraph for devices: {deviceIdList} customer: {googleCustomerId} requestId: {requestId}");
DevicesResource.ReportStateAndNotificationRequest rp = service.Devices.ReportStateAndNotification(request);
ReportStateAndNotificationResponse resop = rp.Execute();
}
}
catch (Exception ex)
{
_log.Error($"Exception in ReportToHomeGraph for Customer: {googleCustomerId}. DeviceList: {deviceIdList}. JsonPath: {_appSettingsRetriever.PrivateGoogleAuthenticationFile} Exception: {ex}.");
}
}
Exception:
2021-09-24 14:16:13,547 [110] ERROR ReportToHomeGraph
Exception in ReportToHomeGraph for Customer: 05. DeviceList: (
fe965e6a-21ad-425f-b594-914bf63510a9,
1cc0ee97-a87f-44c5-a3e3-a39d159ee193,
618cdf94-2b31-434f-b91e-00837d155d4a
).
JsonPath: C:/myfile.json Exception: The service homegraph has thrown an exception:
Google.GoogleApiException: Google.Apis.Requests.RequestError
Requested entity was not found. [404]
Errors [
Message[Requested entity was not found.] Location[ - ] Reason[notFound] Domain[global]
]
at Google.Apis.Requests.ClientServiceRequest`1.<ParseResponse>d__35.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Google.Apis.Requests.ClientServiceRequest`1.Execute()
at BusinessLogic.GoogleAssistant.TokenService.HomeGraph.ReportToHomeGraph.Send(Dictionary`2 deviceStateList,
String requestId, String googleCustomerId) in C:\Repos\GoogleAssistant
.TokenService\HomeGraph\ReportToHomeGraph.cs:line 57.
When users add a new device, the first step you need to do is to issue a Request Sync to Google. This indicates the set of devices for that user has changed, and you need a new Sync request to update the data in homegraph. Google will follow this by delivering a Sync intent to your fulfillment endpoint, which you can respond with the updated set of devices.
Getting a 404 when calling Request Sync might indicate your Service Account Key might be invalid, or the agent user id you target might be wrong. Otherwise getting an error for your Sync Response might indicate it’s structured incorrectly. You can find out more about how to structure it in our examples.
I am using Nethereum to call a function called owner() in a BSC contract, which should return the contract owner's address, however when I use the CallAsync() method, I get an exception "No connection could be made because the target machine actively refused it"
My code (simplified):
using (var s = webRequest.GetResponse().GetResponseStream())
{
using (var sr = new StreamReader(s))
{
string contractABIstr = sr.ReadToEnd();
JObject contractABI = JObject.Parse(contractABIstr);
string contractResults = (string)contractABI.SelectToken("result");
var web3 = new Web3();
var contract = web3.Eth.GetContract(contractResults, address);
Nethereum.Contracts.Function = contract.GetFunction("owner");
string owner = "";
Task<string> task = function.CallAsync<string>();
owner = await task;
}
}
When calling the line owner = await task, I get an exception with message "Error occurred when trying to send rpc requests(s)", which has an inner exception with the message "An error occurred while sending the request.", which has its own inner exception with the message "Unable to connect to the remote server", and this one has its own inner exception saying "No connection could be made because the target machine actively refused it [IP address]"
Does anyone know what I'm doing wrong here? (I've simplified the code above but I do check that the function exists)
My problem turned out to be that I had initialised the Web3() with no parameters, so it defaults to using localhost as the RPC client. I fixed it by initialising it with the RPC client url that I needed, in this case:
string rpcClient = "https://bsc-dataseed.binance.org/";
Web3 web3 = new Web3(rpcClient);
I am trying to connect to my couchbase server (Enterprise ver 4.6.2) and open a bucket using the .Net SDK, the code is very simple but still fail to work:
var cluster = new Cluster(new ClientConfiguration
{
Servers = new List<Uri> { new Uri("https://10.0.0.54:8091/") }
});
try
{
var bucket = cluster.OpenBucket("default");
}
catch (Exception ex)
{
Console.WriteLine("Error getting bucket.");
Console.WriteLine(ex.Message);
}
The inner exception details I get:
Message
Object reference not set to an instance of an object
StackTrace
Couchbase.IO.Services.PooledIOService.CheckEnabledServerFeatures(IConnection connection)
at Couchbase.IO.Services.PooledIOService..ctor(IConnectionPool connectionPool)
at Couchbase.IO.IOServiceFactory.<>c__DisplayClass0_0.<GetFactory>b__0(IConnectionPool pool)
at Couchbase.Configuration.Server.Providers.CarrierPublication.CarrierPublicationProvider.GetConfig(String bucketName, String username, String password)
The main exception I see is:
Could not bootstrap - check inner exceptions for details.
And the stack trace:
at Couchbase.Core.ClusterController.CreateBucket(String bucketName, String username, String password, IAuthenticator authenticator)
at Couchbase.Core.ClusterController.CreateBucket(String bucketName, IAuthenticator authenticator)
at Couchbase.Cluster.OpenBucket(String bucketname)
at Couchbase.Program.Main(String[] args)
Any idea what the issue is?
I had the same problem with the Community edition. In my case I had not created the default bucket before trying to access it. Also, if your server/cluster or bucket require credentials you need to enter those as well.
Expose all required ports
docker run -d -p 8091-8096:8091-8096 -p 11210:11210 -p 11211:11211 couchbase
I've been working on an amazon EC2 project and when I login like this:
public bool Login(Credentials credentials, string ownerID, string region)
{
try
{
OwnerID = ownerID;
CurrentCredentials = credentials;
CurrentConfigs = new AmazonEC2Config() { RegionEndpoint = RegionEndpoint.GetBySystemName(region) };
EC2 = AWSClientFactory.CreateAmazonEC2Client(CurrentCredentials, CurrentConfigs);
}
catch
{
return false;
}
return true;
}
And then try to access like this:
DescribeInstancesRequest ec2Request = new DescribeInstancesRequest();
ec2Request.InstanceIds = new List<string>() { id };
DescribeInstancesResponse ec2Response = EC2.DescribeInstances(ec2Request);
return ec2Response.Reservations[0].Instances[0];
I get this exception:
EXCEPTION:
Encountered a WebException (NameResolutionFailure), the request cannot be retried. Either the maximum number of retries has been exceeded (4/4) or the request is using a non-seekable stream.
STACK TRACE:
at Amazon.Runtime.AmazonWebServiceClient.HandleHttpWebErrorResponse(AsyncResult asyncResult, WebException we)
at Amazon.Runtime.AmazonWebServiceClient.getRequestStreamCallback(IAsyncResult result)
at Amazon.Runtime.AmazonWebServiceClient.endOperation[T](IAsyncResult result)
at Amazon.EC2.AmazonEC2Client.DescribeInstances(DescribeInstancesRequest describeInstancesRequest)
at ...
EDIT:
This exception's inner exception is:
The remote name could not be resolved: 'ec2.sa-east-1a.amazonaws.com'
It's important to point out that it only gives me this error when the access is to region sa-east-1a. Another account I have in region us-west-2 works perfectly. Also, I have extracted the Credential and Region information from the web.config
Simple error, simple solution: The region is sa-east-1 not sa-east-1a. When logging in, you shouldn't specify which datacenter to use, just the region.
As an appendix to this, to anyone arriving here who wasn't specifying the datacenter. In my case using RegionEndpoint.GetBySystemName("USEast1") vs RegionEndpoint.GetBySystemName("US-East-1") was the difference maker.
I'm trying to create a utility to download file from the internet and upload it again to Azure blob storage.
Blob containers already created well; But for some reason i'm getting "Bad Request 400" exception when i tried to upload the file to storage ... Container name is created, small letters, so special characters. But I still do not know why i'm getting the exception!
Please help.
Note:
I'm not using any emulator ... Directly testing on the cloud.
All of my containers with "Public Container" access option.
Here is the exception:
An exception of type 'Microsoft.WindowsAzure.Storage.StorageException'
occurred in Microsoft.WindowsAzure.Storage.dll but was not handled in user code
Additional information: The remote server returned an error: (400) Bad Request.
And here is the code:
foreach (var obj in objectsList)
{
var containerName = obj.id.Replace("\"", "").Replace("_", "").Trim();
CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
if (blobContainer.Exists())
{
var fileNamesArr = obj.fileNames.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var sora in fileNamesArr)
{
int soraInt = int.Parse(sora.Replace("\"", ""));
String fileName = String.Format("{0}.mp3", soraInt.ToString("000"));
var url = String.Format("http://{0}/{1}/{2}", obj.hostName.Replace("\"", ""), obj.id.Replace("\"", ""), fileName.Replace("\"", "")).ToLower();
var tempFileName = "temp.mp3";
var downloadedFilePath = Path.Combine(Path.GetTempPath(), tempFileName).ToLower();
var webUtil = new WebUtils(url);
await webUtil.DownloadAsync(url, downloadedFilePath).ContinueWith(task =>
{
var blobRef = blobContainer.GetBlockBlobReference(fileName.ToLower());
blobRef.Properties.ContentType = GetMimeType(downloadedFilePath);
using (var fs = new FileStream(downloadedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
blobRef.UploadFromStream(fs); // <--- Exception
}
});
}
}
else
{
throw new Exception(obj.id.Replace("\"", "") + " Container not exist!");
}
}
Edit: The Storage Exception
Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (400) Bad Request. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand1 cmd, IRetryPolicy policy, OperationContext operationContext)
--- End of inner exception stack trace ---
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand1 cmd, IRetryPolicy policy, OperationContext operationContext)
at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStreamHelper(Stream source, Nullable`1 length, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStream(Stream source, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
at TelawatAzureUtility.StorageService.<>c__DisplayClass4.b__12(Task task) in \psf\Home\Documents\Visual Studio 14\Projects\Telawat Azure Utility\TelawatAzureUtility\StorageService.cs:line 128
Request Information
RequestID:
RequestDate:Sat, 28 Jun 2014 20:12:14 GMT
StatusMessage:Bad Request
Edit 2: Request Information:
Edit 3: The problem comes from WebUtils .. I replaced it with below code and it works! I will add weUtils code, maybe you can help to know what is the problem with it.
HttpClient client = new HttpClient();
var stream = await client.GetStreamAsync(url);
WebUtils Code:
public class WebUtils
{
private Lazy<IWebProxy> proxy;
public WebUtils(String url)
{
proxy = new Lazy<IWebProxy>(() => string.IsNullOrEmpty(url) ? null : new WebProxy {
Address = new Uri(url), UseDefaultCredentials = true });
}
public IWebProxy Proxy
{
get { return proxy.Value; }
}
public Task DownloadAsync(string requestUri, string filename)
{
if (requestUri == null)
throw new ArgumentNullException("requestUri is missing!");
return DownloadAsync(new Uri(requestUri), filename);
}
public async Task DownloadAsync(Uri requestUri, string filename)
{
if (filename == null)
throw new ArgumentNullException("filename is missing!");
if (Proxy != null)
{
WebRequest.DefaultWebProxy = Proxy;
}
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri))
{
using (Stream contentStream = await (await httpClient.SendAsync(request)).Content.ReadAsStreamAsync())
{
using (var stream = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
contentStream.CopyTo(stream);
stream.Flush();
stream.Close();
}
contentStream.Close();
}
}
}
}
}
Also when I tried this code ... the 'Wait' will never finish or completed!
webUtil.DownloadAsync(url, downloadedFilePath).Wait()
Have you tried creating a container manually on azure portal? It has some limitations on what name you can give containers.
For example: Container name cannot contain upper case letters.
If you request a container with an invalid name, it will result in (400) Bad Request, which you are getting. So check your "containerName" string.
I had the same problem. I resolved it by changing the TLS version in the configuration of the storage; the new TLS version (1.2) is not compatible with the older version of the storage client. I changed it to the 1.0 and it works.
The configuration of the storage is in the portal of Azure.
Storage -> Configuration -> TLS Version:
I also got this error with the Azure Storage Message Queues.
The Azure Storage Message Queue names must also be all lowercase.
ie: "newqueueitem" name in lowercase.
// Retrieve a reference to a queue.
CloudQueue queue = queueClient.GetQueueReference("newqueueitem");
// Create the queue if it doesn't already exist.
queue.CreateIfNotExists();
If you create a container with an invalid name, it will result in (400) Bad Request. There are some convention for creating container name as below:
Container names must start with a letter or number, and can contain only letters, numbers, and the dash (-) character.
Every dash (-) character must be immediately preceded and followed by a letter or number; consecutive dashes are not permitted in
container names.
All letters in a container name must be lowercase.
Container names must be from 3 through 63 characters long.
Source: https://learn.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata
I had a very different case of bad request message. Posting here for anyone else who may hit the same. In my case, I was just moving around resource across other resource groups . In that shuffling, a bug in azure allowed me to point my storage to location ("South East Asia") which was not available in my region. So all requests against the storage account returned the bad request message. It took me a while to figure this out because I then created another storage account to test, which when creating, azure did not allow me to pick "South East Asia" as a location of choice, so I picked another location ("East Asia") and then everything worked fine.
I know it's been a long time but I was able to fix it for TLS 1.2 with the following line of code:
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
It would be something like this :
public async Task DownloadAsync(Uri requestUri, string filename){
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
... your code...
}
I faced the same issue while creating the queue in Azure with one UpperCase Letter from my C# code. The error was with the Queue Name. All the characters should be lowercase. After changing all the characters to lowercase it worked! :)
//Retrieve a reference to a queue
CloudQueue queue = queueClient.GetQueueReference("myqueue");
//Create a queue if it alredy doen't exists
queue.CreateIfNotExists();