Getting "TimeoutException" when trying to retrieve data from MongoDB - c#

I'm using C# to connect to a MongoDB server using the official MongoDB.Driver with version 2.2.24.26
My code looks like this:
internal BsonArray Find(string ConnectionString, string collection, string filter)
{
Uri u = new Uri(ConnectionString);
string database = u.LocalPath.Trim(new char[] { '/' });
IMongoDatabase _db = new MongoClient(ConnectionString).GetDatabase(database);
IMongoCollection<BsonDocument> col = _db.GetCollection<BsonDocument>(collection);
return BsonArray.Create(col.Find(BsonDocument.Parse(filter)).ToList());
}
It works like charm (it finishes within less than 0.5 seconds) if the connection string is like
mongodb://localhost:27017/my_db
As soon as I want to use authentication, I always encounter a timeout.
mongodb://user:password#localhost:27017/my_db
The operation consuming all the time is the "ToList()". The list in my tests does have 136 entries. Am I missing something?
Edit: Sorry for the wrong topic in the first place. I don't know how a topic from a totally unrelated issue did appear here...

I found the problem. MongoDB seems to give back an TimeoutException if the credentials are invalid. I accidentally added the user I wanted to use to the wrong database and hence I could not log into the original database with it. The exception made me search for the issue in a totally wrong direction :/
Looks like a strange behavior when using wrong credentials, but that is just my opinion.

Related

DocumentClient.CreateDocumentQuery throws a "not an instance" error when running a query

I'm writing a .NET web service that takes documents from a cosmosDB database. I writing a function in another project (that is supposed to be as generic as possible) to retrieve a document, so that the WS will use it.
I'm using DocumentClients function CreateDocumentQuery like this:
public static async Task<T1> GetMyDocumentAsync<T1>(eDataBase databaseName, eCollection collectionName, eFields key, string val)
{
var option = new FeedOptions { EnableCrossPartitionQuery = true };
Uri uri = UriFactory.CreateDocumentCollectionUri(databaseName.ToString(), collectionName.ToString());
IQueryable<T1> res = Client.CreateDocumentQuery<T1>(uri, option);
document = res.AsEnumerable<T1>().FirstOrDefault();
}
This works fine, returning the first document in the database. But changing the usage of CreateDocumentQuery to this:
IQueryable<T1> res = Client.CreateDocumentQuery<T1>(uri, "SELECT * FROM c",option);
And wrapped inside a try-catch block throws an exception that has an InnerException that states:
Object not set to an instance of an object.
Also some of the exception fields are:
Data = {System.Collections.ListDictionaryInternal}
and
Error = {{ "code": "BadRequest",
"message": "\r\nActivityId: SOMEGUID" }}
The query works on that collection from the azure portal.
The client properties I'm using are:
ConnectionMode = Gateway and Protocol = Https.
Any ideas as to why this usage works and the other using an explicit query string won't? I also tried using a SqlQuerySpec object and the result was the same. I don't want to use the Where() function since I want to use generic types. The async keyword was meant for future use.
So I solved it.
When creating the collection I tried to query in the question, I noticed that a partition key is mandatory. All my other collections up in cosmos did not have any. So I figured that some of the settings should be different for collections that do have partition keys.
The part that throw the null exception was enumerating the document query. The function usage looked OK according to the documentation so I thought that the problem was with the CreateDocumentQuery. That is because it created a "faulty" object, with some null fields and it might be due to the change in collection partition keys.
So I tried updating the package Microsoft.Azure.DocumentDB for my entire solution to the lastest stable version (2.2.0 at the time being) and then it just worked.

How do you connect from ODP.NET to Oracle (12G+) by proxy user with no password

There seems to be no answer online as to how you can use Oracle Data Provider for .NET (ODP.NET) to connect to Oracle (12G and later) in a very specific scenario:
User is identified externally on a database
User is granted access to another schema (application user) by proxy connect
User has been set up like this:
CREATE USER user_in_question
IDENTIFIED EXTERNALLY
-- etc.
And connect by proxy has been set up like this:
ALTER USER specified_app_user GRANT CONNECT THROUGH user_in_question
The logical approach when creating the ODP.NET OracleConnection string would be something like this (using the user friendly OracleConnectionStringBuilder):
var connBuilder = new OracleConnectionStringBuilder
{
UserID = "/", // External login using the user running the program
ProxyUserId = "specified_app_user",
DataSource = "database",
};
This does not work. Nor does providing blank "Password" or blank "Proxy Password". Nor does removing the UserId.
So how do you connect using ODP.NET in these circumstances?
The answer (which I spend an hour searching for without any luck) is actually really simple, yet not very user friendly:
var connBuilder = new OracleConnectionStringBuilder
{
UserID = "[specified_app_user]",
DataSource = "database",
};
//connBuilder.ToString() output:
//"USER ID=[specified_app_user];DATA SOURCE=database"
This works in .NET 4.5+ on Oracle 12G+, but probably also on earlier platforms of .NET/Oracle/ODP.NET. I did not test it in ASP.NET, but it should work there too.
This way the UserId actually functions just like the ProxyUserId, just enclosed within brackets, just as you would normally log in on the Oracle Database using, say, Toad or SQlPlus.
It might also be possible using this format (but in my case the connection string had to be compatible with the OraOLEDB format so that did not work):
//Without the use of the conn string builder class, just for the fun of it...
var connString = "User Id=specified_app_user;Data Source=database;Proxy User Id=/";
EDITED 2nd March 2017: The line above does not seem to work in certain cases. Added comment about it and here is the code that IS working:
USER ID=[specified_app_user];DATA SOURCE=database
This info does not seem to exist anywhere - else I overlooked it, and in that case PLEASE do correct me.

Azure Notification Hub installation not updating tags

I am attempting to update the tags of an installation within Azure Notification Hub after registration. I am following several guides for this, notably here and here.
Both of these guides suggest that the following code should work however it is plainly not; the tag never gets updated. There are no errors, and I can guarantee that the installationId is correct. I am guessing I am setting the path/value of the tag incorrectly.
// in constructor:
var _notificationHub = NotificationHubClient.CreateClientFromConnectionString(Settings.ConnectionStrings.NotificationHub, Settings.Defaults.NotificationHubName);
// in WebApi endpoint:
var installationUpdates = new List<PartialUpdateOperation>();
var userDetail = _userDetailRepo.Get(id);
installationUpdates.Add(new PartialUpdateOperation
{
Operation = UpdateOperationType.Replace,
Path = "/tags/interestedin", // is this incorrect?
Value = interestedIn.ToUpper()
});
userDetail.InterestedIn = interestedIn;
await Task.WhenAll(
_userDetailRepo.InsertOrReplace(userDetail),
_notificationHub.PatchInstallationAsync(installationId, installationUpdates));
Here is the installation object's tags, as per VS:
I also tried hardcoding the path to Path = "/tags/interestedin:W" but it made no difference.
Can someone tell me if I am doing something wrong here, and if so how I should amend my code. Thanks.
Unfortunately, Path = "/tags/interestedin" is not going to work as of now. We are currently working on wildcards' support. Once it is done, something like "/tags/interestedin*" will work fine for you.
While Path = "/tags/interestedin:W" should be OK. If you could provide namespace name, hub name, and a timeframe, then I'll take a look at logs to check what is going on there.

Android - Refit throws UnknownHostException unexpectedly

I'm trying to implement Refit using Xamarin and for some reason it throws a UnknownHostException (UHExc) if I was previously not connected to wifi while the app was open. This doesn't happen consistently though.
I have these two calls to Refit's instantiation of my "Refit-interface": PostLoginAsync and GetDataAsync, as shown below (the guide I've been following is here):
public async Task<SomeClass> PostLogin(string user, string password)
{
SomeClass response = null;
var loginTask = apiService.UserInitiated.PostLoginAsync(new RequestBody(user: user, password: password));
response = await FireWebTask(loginTask);
return response;
}
and
private async Task<List<Data>> GetRemoteDataAsync(string args)
{
List<Data> list = null;
var getDataTask = apiService.UserInitiated.GetDataAsync(args);
list = await FireWebTask(getDataTask);
return list;
}
The "Refit-interface" looks somewhat like this:
...
[Post("/relative/url/to/login")]
Task<SomeClass> PostLoginAsync([Body(BodySerializationMethod.Json)] RequestBody requestBody);
[Get("/relative/url/to/data")]
Task<List<Data>> GetDataAsync([Header("SomeHeader")] string args);
...
When I open the app with no connection to the internet and try to send the PostLogin-request, I get an UHExc as expected. If I then turn on the wifi and try again (without closing the app) I get the UHExc again, only this time with almost no delay as the first time (as if the exception was cached??). Restarting the app and trying again without disconnecting the wifi works fine.
If I do the exact same thing with the second request (GetData) I first get an UHExc (obviously) but when reconnecting the wifi it works flawlessly. So it seems to me like the POST-request caches the exception or something and throws it repeatedly without trying to connect at all. How can I solve this (whatever the problem actually is)?
I also had this problem and after some testing discovered that the issue is with the Fusillade library. Now the initial problem is that the fixing changes are not reflected in the NuGet packages so you need to download the latest source from the Github repo and reference the newer dlls.
Looks like the underlying issue is due to the failed requests getting enqueued and played back even though the request resulted in a WebException.
I included the latest code into my project and confirmed that they are working.

Finding Connection by UserId in SignalR

I have a webpage that uses ajax polling to get stock market updates from the server. I'd like to use SignalR instead, but I'm having trouble understanding how/if it would work.
ok, it's not really stock market updates, but the analogy works.
The SignalR examples I've seen send messages to either the current connection, all connections, or groups. In my example the stock updates happen outside of the current connection, so there's no such thing as the 'current connection'. And a user's account is associated with a few stocks, so sending a stock notification to all connections or to groups doesn't work either. I need to be able to find a connection associated with a certain userId.
Here's a fake code example:
foreach(var stock in StockService.GetStocksWithBigNews())
{
var userIds = UserService.GetUserIdsThatCareAboutStock(stock);
var connections = /* find connections associated with user ids */;
foreach(var connection in connections)
{
connection.Send(...);
}
}
In this question on filtering connections, they mention that I could keep current connections in memory but (1) it's bad for scaling and (2) it's bad for multi node websites. Both of these points are critically important to our current application. That makes me think I'd have to send a message out to all nodes to find users connected to each node >> my brain explodes in confusion.
THE QUESTION
How do I find a connection for a specific user that is scalable? Am I thinking about this the wrong way?
I created a little project last night to learn this also. I used 1.0 alpha and it was Straight forward. I created a Hub and from there on it just worked :)
I my project i have N Compute Units(some servers processing work), when they start up they invoke the ComputeUnitRegister.
await HubProxy.Invoke("ComputeUnitReqisted", _ComputeGuid);
and every time they do something they call
HubProxy.Invoke("Running", _ComputeGuid);
where HubProxy is :
HubConnection Hub = new HubConnection(RoleEnvironment.IsAvailable ?
RoleEnvironment.GetConfigurationSettingValue("SignalREndPoint"):
"http://taskqueue.cloudapp.net/");
IHubProxy HubProxy = Hub.CreateHubProxy("ComputeUnits");
I used RoleEnviroment.IsAvailable because i can now run this as a Azure Role , a Console App or what ever in .NET 4.5. The Hub is placed in a MVC4 Website project and is started like this:
GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(50);
RouteTable.Routes.MapHubs();
public class ComputeUnits : Hub
{
public Task Running(Guid MyGuid)
{
return Clients.Group(MyGuid.ToString()).ComputeUnitHeartBeat(MyGuid,
DateTime.UtcNow.ToEpochMilliseconds());
}
public Task ComputeUnitReqister(Guid MyGuid)
{
Groups.Add(Context.ConnectionId, "ComputeUnits").Wait();
return Clients.Others.ComputeUnitCameOnline(new { Guid = MyGuid,
HeartBeat = DateTime.UtcNow.ToEpochMilliseconds() });
}
public void SubscribeToHeartBeats(Guid MyGuid)
{
Groups.Add(Context.ConnectionId, MyGuid.ToString());
}
}
My clients are Javascript clients, that have methods for(let me know if you need to see the code for this also). But basicly they listhen for the ComputeUnitCameOnline and when its run they call on the server SubscribeToHeartBeats. This means that whenever the server compute unit is doing some work it will call Running, which will trigger a ComputeUnitHeartBeat on javascript clients.
I hope you can use this to see how Groups and Connections can be used. And last, its also scaled out over multiply azure roles by adding a few lines of code:
GlobalHost.HubPipeline.EnableAutoRejoiningGroups();
GlobalHost.DependencyResolver.UseServiceBus(
serviceBusConnectionString,
2,
3,
GetRoleInstanceNumber(),
topicPathPrefix /* the prefix applied to the name of each topic used */
);
You can get the connection string on the servicebus on azure, remember the Provider=SharedSecret. But when adding the nuget packaged the connectionstring syntax is also pasted into your web.config.
2 is how many topics to split it about. Topics can contain 1Gb of data, so depending on performance you can increase it.
3 is the number of nodes to split it out on. I used 3 because i have 2 Azure Instances, and my localhost. You can get the RoleNumber like this (note that i hard coded my localhost to 2).
private static int GetRoleInstanceNumber()
{
if (!RoleEnvironment.IsAvailable)
return 2;
var roleInstanceId = RoleEnvironment.CurrentRoleInstance.Id;
var li1 = roleInstanceId.LastIndexOf(".");
var li2 = roleInstanceId.LastIndexOf("_");
var roleInstanceNo = roleInstanceId.Substring(Math.Max(li1, li2) + 1);
return Int32.Parse(roleInstanceNo);
}
You can see it all live at : http://taskqueue.cloudapp.net/#/compute-units
When using SignalR, after a client has connected to the server they are served up a Connection ID (this is essential to providing real time communication). Yes this is stored in memory but SignalR also can be used in multi-node environments. You can use the Redis or even Sql Server backplane (more to come) for example. So long story short, we take care of your scale-out scenarios for you via backplanes/service bus' without you having to worry about it.

Categories