MongoClient ignores the connection string - c#

I just started to use MongoDB(4.4) and its C# driver. I set up my MongoDB with the default option, localhost:27017. Then I turned on the authorization, created a superuser with root permission, like this:
db.createUser(
{
user: "superuser",
pwd: "123",
roles: [ "root" ]
}
)
I tested it on both Mongo shell and Compass, it all worked as expected, connected with correct password, and denied with the wrong one.
Then I created a C# windows form app, use NuGet to install all required packages such as MongoDB.Driver for C#(v2.11.0) and its related packages such as MongoDB.Bson, etc
After that, I used the following code to create a MongoClient:
MongoClient client = new MongoClient( "mongodb://superuser:12#localhost:27017" );
So I expected it should throw an exception because I used the wrong password, "12" in this case. But it didn't. Then I tried to list database names with:
client.ListDatabaseNames();
It threw a timeout exception: "A timeout occured after 30000ms selecting a server using CompositeServerSelector"
Even I used the correct password or turned off the authorization, and just go with "mongodb://localhost:27017" or ""mongodb://127.0.0.1:27017", it still threw the same timeout exception.
It feels like something wrong with that client it created which caused the timeout issue later on. But I couldn't figure out what I am missing.
Thank you for your help!
Edit:
The same code works perfectly in a console app, just not a windows form app, which really confuses me.

After trial and error for 2 days, I finally found a work around for this issue, still don't know why this works though.
So basically I have to split the MongoClient creation and its following function calls separately. I can't do anything related to the MongoClient right after its creation. For example, the following throws the time out exception:
MongoClient client = new MongoClient( "mongodb://localhost:27017" ); //I turned off authorization
client.ListDatabaseNames(); //Throw time out exception here!!!
I have to split them separately in 2 functions call, like one in a "Connect" button event, another one in a "ListDatabaseNames" button event.
Once I did that, everything works fine.

mongodb://superuser:12#localhost:27017?authMechanism=SCRAM-SHA-1
because dotNet not support SCRAM-SHA-256 yet

Creating a client does not perform any network operations like connecting to your MongoDB deployment - that is done in background. Hence incorrect credentials won't make client creation fail.
mongo shell works differently and compass probably performs some queries that would fail if credentials aren't correct.
Why you are getting a timeout error - my guess is it's an ipv4/ipv6 difference. Try 127.0.0.1 instead of localhost. If this doesn't help enable debug information in your driver.

Related

ErrorMessage while open connection to Denodo via npgsql in c#

sorry, if this is question is (to) easy for stackoverflow community.
I am trying to connect to Denodo (Version 7), via c# code.
I installed npqsql and created a connection string with: {host} {port} {username} {passwd} {database} and the SSL-Mode as required.
(user and pwd combination was checked via Launch Pad)
For trying I use a simple select statement.
If I call Open() on my NpqsqlConnection object I get the error message {"Received unexpected backend message ParseComplete. Please file a bug."}
For beeing sure I tried to ping the host, which works fine. And if I try without SSl, the error message indicates that the SSl is required, therefor I think the correct backend is found.
Could someone give me a hint?
Thanks
Jessi
I checked the Denodo Community, and the given examples: https://community.denodo.com/docs/html/browse/6.0/vdp/developer/access_through_an_ado.net_data_provider/access_through_an_ado.net_data_provider
https://community.denodo.com/answers/question/details?questionId=90670000000XcbkAAC&title=How+to+execute+C%23+program+using+vdp-clients-ADO.NET

SQL Access problems from within Application Page

I have a SharePoint-Feature that connects to an SQL-Database. From within that (Site-)feature I can access the DBMS and even create a new Database without problems (using Linq2sql)
But inside the Application Page within the SAME Project I cannot even do a simple LinQ-Query on the just created Database.
I receive the error "
Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'
"
When debugging the code I can see that the credentials (SPWeb.CurrentUser) are both the same (and not anonymous at all)
The Feature Event Receiver as well as the Application Page use the same Connection String (with Integrated Security=true)
From other posts here I know that using an SQL-Login instead of integrated Security might help, but I am curious why on the Event Receiver everything works perfect, but on the Application Page nothing works?
I've had that issue before and typically I can just run my SQL code in a SPSecurity.RunWithElevatedPrivileges { block and it works fine.
Forgive me for not getting the language/reason precisely correct, but I believe the reason it runs fine in your eventreceiver code is because that code is executed a in a different more trusted context than an application page. Someone else might be able to explain more comprehensively/eloquently than me.

Converting Microsoft EWS StreamingNotification Example to a service

I've been working to try and convert Microsoft's EWS Streaming Notification Example to a service
( MS source http://www.microsoft.com/en-us/download/details.aspx?id=27154).
I tested it as a console app. I then used a generic service template and got it to the point it would compile, install, and start. It stops after about 10 seconds with the ubiquitous "the service on local computer started and then stopped."
So I went back in and upgraded to C# 2013 express and used NLog to put a bunch of log trace commands to so I could see where it was when it exited.
The last place I can find it is in the example code, SynchronizationChanges function,
public static void SynchronizeChanges(FolderId folderId)
{
logger.Trace("Entering SynchronizeChanges");
bool moreChangesAvailable;
do
{
logger.Trace("Synchronizing changes...");
//Console.WriteLine("Synchronizing changes...");
// Get all changes since the last call. The synchronization cookie is stored in the
// _SynchronizationState field.
// Only the the ids are requested. Additional properties should be fetched via GetItem
//calls.
logger.Trace("Getting changes into var changes.");
var changes = _ExchangeService.SyncFolderItems(folderId, PropertySet.IdOnly, null, 512,
SyncFolderItemsScope.NormalItems,
_SynchronizationState);
// Update the synchronization cookie
logger.Trace("Updating _SynchronizationState");
the log file shows the trace message ""Getting changes into var changes." but not the "Updating _SynchronizationState" message.
so it never gets past var changes = _ExchangeService.SyncFolderItems
I cannot for the life figure out why its just exiting. There are many examples of EWS streaming notifications. I have 3 that compile and run just fine but nobody as far as I can tell has posted an example of it done as a service.
If you don't see the "Updating..." message it's likely the sync threw an exception. Wrap it in a try/catch.
OK, so now that I see the error, this looks like your garden-variety permissions problem. When you ran this as a console app, you likely presented the default credentials to Exchange, which were for your login ID. For a Windows service, if you're running the service with one of the built-in accounts (e.g. Local System), your default credentials will not have access to Exchange.
To rectify, either (1) run the service under the account you did the console app with, or (2) add those credentials to the Exchange Service object.

RavenDB BulkInsert() fails with 403 Forbidden

I've recently migrated my environment and now a process that uses BulkInsert is not working. It is resulting in a 403 Forbidden response from the db server.
The previous environment had the database running as a Windows service on the same server as my application and access was set up differently. Now I'm using RavenHQ on a remote host, using an API Key. I'm sure the privileges have changed so I'm wondering if that's why BulkInsert is resulting in 403 error.
I've searched but I cannot find anything that explicitly states what privileges are required for the various raven operations. My understanding from this post is that there are at least some that require higher privileges. Can anybody point me to documentation on specifics? Or otherwise shine some light on the subject? Thanks.
I had this same problem and I think it's because the example connection string RavenHQ gives you containing your API key does not actually include your database name.
Try adding ";Database=[myDb]" to the end of your connection string URL or passing your database name explicitly as the first parameter to bulkInsert()

Mathematica .Net/Link in an Asp.Net application

I am using the Mathematica .Net/Link platform to create a web service to format and calculate math problems. However I am unable to get it working.
I create it using this code:
_Log.IpDebug("Starting the Kernel Link");
if (string.IsNullOrEmpty(_MathLinkArguments))
_InternelKernel = MathLinkFactory.CreateKernelLink();
else
_InternelKernel = MathLinkFactory.CreateKernelLink(_MathLinkArguments);
_Log.IpDebug("Kernel Link Started");
_InternelKernel.WaitAndDiscardAnswer();
The value of _MathLinkArguments is -linkmode launch -linkname \"C:\\Program Files\\Wolfram Research\\Mathematica\\7.0\\Math.exe\".
This piece of code is called from the Application_Start method of the global.asax.cs file.
When it gets to the WaitAndDiscardAnswer() call it gives the server error:
Error code: 11. Connected MathLink program has closed the link, but there might still be data underway.
Note: The SampleCode given with the .NET/Link package (both a console app and a WinForms app) works.
Edit:
I copied the console app sample code given with Mathematica into an asp.net page and it gave me the same error the first load and then on subsequent loads it gave me:
Error code: 1. MathLink connection was lost.
Edit2:
I forgot to mention that when I have procmon and task manager open while running my app, I can tell that Math.exe starts but it immediately exits, which makes those error code make complete sense...but doesn't explain why that happened.
To allow the .Net/Link to work in Asp.net (at least in IIS 7.5) you need to enable the property loadUserProfile on the app pool for the web site.
I am not entirely sure why this is the case, but from what I found while trying to debug this, there are some things that are gotten from the user's profile. I know for a fact that the default location of the kernel is, which explains why I couldn't use it with no arguments, and so I can only assume that other things are needed as well and without the profile it couldn't determine that.
But whatever the reason is this is required, it is, or at least it is a fix if you are getting similar problems like this in your own application.
I got the same error in a .Net WinForm application.
mathKernel = new MathKernel();
mathKernel.Compute("<< XYZ`XYZGraphs`");
The error occurred on loading the package straight after instantiating the MathKernel.
To resolve it you can wait a couple of seconds and then instantiating the MathKernel works fine. During this state where there might still be data underway the following conditions are both false:
if (!MathKernel.IsConnected)
{
MathKernel.Connect();
}
if (MathKernel.IsComputing)
{
MathKernel.Abort();
}
Edit:
I've recieved the error again and this time was able to determine the problem.
Using a command line open the MathKernel.exe and view the error message:

Categories