DirectoryEntry Timeout - active-directory

I am having an issue with the DirectoryEntry object where it's taking a long time trying to connect to to a dead AD server and eventually failing. Is it possible to set a timeout so that if its not able to connect within a specific time, it just comes out to try the next one?

There is no timeout option for DirectoryEntry directly.
You can use DirectorySearcher and set the ClientTimeout (even if you're only looking for one object by path). Or do your directory operation on a new thread or BackgroundWorker and control your own timeout.

I suggest you create your own LdapConnection to the server. This will allow you to specify a timeout and finely control which method you are using.
Also note that without going to this lower level, the .NET classes will attempt to use LDAP+SSL, then Kerberos, and finally RPC. You may be experiencing delays/timeouts during this process.

Related

Slow first NpgsqlConnection in YugabyteDB YSQL

[Question posted by a user on YugabyteDB Community Slack]
It seems that connecting to a cluster is “slow” the first time through NpgsqlConnection.
Is this normal PostgreSQL behaviour or is this related to YugabyteDB?
We are using the latest 6.0.0-preview7 nuget to use the multi host connecting string for load balancing.
According to Allow extensibility in type loading and database capabilities · Issue #1486 · npgsql/npgsql, extra calls are made to system tables to map types when the first connection is created. In order to avoid the initial call, you can set connStringBuilder.ServerCompatibilityMode = ServerCompatibilityMode.NoTypeLoading; on the connection string. When setting this property the first call is not made and the connection will be faster.

Close SQL connections cleanly when connection dropped

Just wondering if there is a way that I can close all SQL connections and commands in the cleanest possible way when a connection is lost in VB.NET (can also be in C#.NET).
What I'm trying to achieve:
Using the System.Net.NetworkInformation.NetworkAvailabilityChanged event, I'm monitoring whether the connection has been lost. If it has been lost, I'm disabling all user input - works great. Network comes back on, UI is enabled. (thumbs up)
Now, however, comes my predicament. If an SQL query is executing before the connection drops and then the network is lost, then the query returns a null value as expected, however if that was mid population of datatables/fields, then I get NullReferenceExceptions.
My question is:
Is there anyway to cleanly exit a sub after the connection has dropped? I've tried Application.ExitThread, but that doesn't seem to quite cut it. Do I need to put dropped connection handlers within my objects, so that when the connection is dropped, the respective object won't return or try to assign null data?
Any help is greatly appreciated. Not asking for plain code, need explanations if at all possible. Cheers.
If you follow the "using" best practice, like:
Using cn As New SqlConnection(connectionString)
...
End Using
Then the compiler will generate code that cleans up the connection when an exception is thrown.

Entity framework, how to increase limits?

We are building an application which makes every week a very large amount of request over the database, concurrently.
We have ~15-20 threads which query the database concurrently.
We are actually encountering a lot of problems:
On the database side(not enough RAM): being resolved.
But on the client too. We have Exception when trying to get a connection or execute commands. Those commande are mades through entity framework.
The application has two part: one website and one console application.
So can anyone tell me how to increase the following values?
Connection Timeout
Command Timeout
Connection pool size
I think that there several things that have to be done on the server side(SQL Server or IIS), but I can't find where?
Command timeout can be set on ObjectContext instance. Connect timeout and connection pool size is configured in connection string but if you have only 15-20 threads your problem will most probably be somewhere else because default connection pool size is 100.
Enclose your objectContext in a using block so the context disposes after you have done your work.
you can make a method to pass in thread which uses your entity context to do the work you want and then dispose the connection after the work is finished, you can use the stateinfo object variable to pass in different parameters to use during the life of your method.
void DoContextWork(object stateInfo)
{
// wrap your context in a using clause
using(var objectContext = new YourEntity()
{
// Do work here
}
}
you can have multiple threads call this method and each time your connection gets called you can do your work on your DB without getting the issues you mentioned above.

Can I use the connection from Entity Framework or should I create a new one?

I have a SQLite Database that I access using System.Data.SQLite. The database is read into my Entity Framework Objectcontext. However, I also need direct access to the database in certain cases.
Can I / Should I use the same connection that my Entity Framework object context is using? I.e. ObjectContext.Connection?
If yes, how can I access it? Casting Entities.Connection to SQLiteConnection results in "Unable to cast object of type 'System.Data.EntityClient.EntityConnection' to type 'System.Data.SQLite.SQLiteConnection'."
If not, should I create a seperate connection, open it and keep it open for the application duration, or should I open and close the connection every time I want to access the database with it?
ObjectContext.Connection is an EntityConnection. You can get the store connection via:
var sc = ((EntityConnection)ObjectContext.Connection).StoreConnection;
You can cast that to the SQLiteConnection.
It's OK to use this connection if you need it.
But, as #Chris mentioned, you can just use ObjectContext.ExecuteStoreQuery, etc., if that will do for you.
If you're using the DBContext API you can actually execute a direct query by using this: aDBContext.Database.SqlQuery
I wouldn't be surprised if something similar exists for the ObjectContext API, but I barely used that one and thus can't tell you what it is.
Now if for some reason you can't (or don't want) to do that, I'd create a new connection for your custom queries. Put them in a Using and close it as soon as you're done with it.
Entity Framework has built in methods for executing raw SQL queries if that's what you need to do.

How can I detect whether my WPF client application is offline?

In WPF (in code behind) is there a way to detect whether the computer that the client application is running on has access to the internet?
I'm not sure if there is a simple dedicated method / property to check this or whether I would have to try an HttpGet or similar to determine this.
Is there a property or method for this purpose?
-- Lee
It depends on what you consider "access to the internet"
Safest is to add a 'ping' service to the server you wish to connect to and poll that service.
Trying to keep track of the network status is much harder because it is hard to find out whether it is an intranet other type of network.
See this post too
I don't think there's a simple dedicated method or property.
The simplest solution would be to try to connect to your server and then check the error state.

Resources