Why has my DirectoryEntry stopped working suddenly? - binding

I have been working with the DirectoryEntry objects in the DirectoryServices namespace and it has been going very well. But I made changes to one of my LDAP classes and all of a sudden it has stopped working on the 32 bit builds of my program.
I imported the ActiveDs reference into my project in order to convert the ADSI objects to their appropriate types. But since then my projects are failing to bind correctly when creating instances of the DirectoryEntry object.
I have tried binding with the LDAP AND WinNT provider but to no avail. The error I get is the 0x80005000 unknown error as it tries to bind.
Even this simple code fails (which is not surprising as binding is a critical part!)
static void Main(string[] args)
{
try
{
using(var de = new DirectoryEntry("LDAP://servername", "user", "password", AuthenticationType.Secure)
{
Console.WriteLine(de.Name)
}
}
catch(Exception ex)
{
Console.WriteLine(ex)
}
}
Is there any reason why this should suddenly stop working? Could a reference corrupt an already existing DLL on a 32 bit machine?
Note:
I have also tried querying LDAP using a VBS script but it just hangs.
Results from Daro's suggestion:
code
static void Main()
{
try
{
using (var ldap = new LdapConnection(new LdapDirectoryIdentifier("ad1")))
{
ldap.Bind();
using (var de = new DirectoryEntry("LDAP://" + ldap.SessionOptions.DomainName))
{
Console.WriteLine(de.Name);
}
}
}
catch(Exception ex)
{
using(var fs = new FileStream("C:\\error.log", FileMode.Create, FileAccess.Write))
{
using(var sw = new StreamWriter(fs))
{
sw.WriteLine(ex.Message);
sw.WriteLine(ex.StackTrace);
}
}
}
The log file produces:
Unknown error (0x80005000)
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_Name()
at Test.Program.Main() in C:\Users\#username#\Documents\Visual Studio 2010\Projects\#project#\Test\Program.cs:line 20

Does this work for you?
try
{
DirectoryEntry de = new DirectoryEntry("LDAP://server", "user", "password", AuthenticationTypes.Secure);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
I assume "Server" is the NetBios name? Try using the FQDN.
If your binding with your own credentials, you can use:
DirectoryEntry de = new DirectoryEntry("LDAP://server", null, null, AuthenticationTypes.Secure);
And if you are binding to your own domain, you could use:
DirectoryEntry de = new DirectoryEntry("LDAP://" + Environment.UserDomainName, null, null, AuthenticationTypes.Secure);
or simply:
DirectoryEntry de = new DirectoryEntry();
I hope that helps.

Related

Generic Data Reader causing memory leak

I created a generic method in reading sql statement, but I am having a memory leak whenever I do a select query and using while read.
Sample Query:
public CMItemPackagingType GetItemPackagingType(int itemID)
{
try
{
List<CommandParameter> param = new List<CommandParameter>();
StringBuilder sb = new StringBuilder();
using (BaseConnection db = new BaseConnection())
{
sb.Append("SELECT RATIO, PACKAGING_TYPE_CODE FROM ITEM_PACKAGING_TYPE WHERE ROUND_UP = 0.01 AND ITEM_ID = #itemID");
param.Add(new CommandParameter("#itemID", itemID));
using (var rs = db.ExecSQL(sb.ToString(), param.ToArray()))
{
CMItemPackagingType cmItemInfo = new CMItemPackagingType();
while (rs.Read())
{
CMItemPackagingType list = new CMItemPackagingType();
if (!rs.IsDBNull(0))
list.Ratio = Convert.ToInt32(rs.GetValue(0));
if (!rs.IsDBNull(1))
list.PackagingTypeCode = rs.GetValue(1).ToString();
cmItemInfo.ItemPackagingTypeList.Add(list);
}
return cmItemInfo;
}
}
}
catch (Exception ex)
{
GlobalFramework.HandleException(ex);
}
return null;
}
Generic Reader:
public DbDataReader ExecSQL(string sqlStmt, CommandParameter[] param)
{
List<MySqlParameter> p = ParameterMySql(param);
_mySqlConn = new MySqlConnection(szConnect);
if (_mySqlConn.State == ConnectionState.Open)
{
_mySqlConn.Close();
}
_mySqlConn.Open();
_mySqlComm = new MySqlCommand(sqlStmt, _mySqlConn);
_mySqlComm.Parameters.AddRange(p.ToArray());
MySqlDataReader reader = _mySqlComm.ExecuteReader();
return reader;
}
I'm assuming the BaseConnection is a wrapper around a SqlConnection and _mySqlConn is an instance of BaseConnection. I suspect the issue is that you are opening and closing the connection in ExecSQL and at the same time have a using statement around BaseConnection creating this leak. I would refactor your code with proper placement of using statements to ensure correct disposal of the objects and freeing of resources.
Example
var query = "YOUR QUERY";
using (var connection = new SqlConnection("YOUR CONNECTION STRING"))
{
using (var command = new SqlCommand(query, connection))
{
await connection.OpenAsync();
using (var reader = await command.ExecuteReaderAsync())
{
if (reader != null)
{
while (await reader.ReadAsync())
{
// your logic
}
}
} // reader closed and disposed up here
} // command disposed here
} //connection closed and disposed here
}
Also notice how I'm using the async versions of the ADO.NET methods. Async commands are critical in achieving scale, throughput, and latency.
I recommend you use Dapper over trying to develop a generic data reader and writing all the boilerplate ADO.NET code yourself.

Neo4j ServerPlugin Configuration

I'm building a Neo4J Server plugin. I'd like to have some configuration values that I can manually set in neo4j.properties or neo4j-server.properties and then the plugin can read and utilize these value. How can I access config values from a ServerPlugin?
Clarification:
I'd really like something that will work across future releases of Neo4J, so something that is part of the public API and is not deprecated would be best.
Using Neo4j's internal dependency mechanism, you can access a instance of Config (https://github.com/neo4j/neo4j/blob/master/community/kernel/src/main/java/org/neo4j/kernel/configuration/Config.java). This class gives you access to the configuration.
Take the following untested snippet as a guideline:
...
import org.neo4j.kernel.configuration.Config
...
#Description( "An extension to the Neo4j Server accessing config" )
public class ConfigAwarePlugin extends ServerPlugin
{
#Name( "config" )
#Description( "Do stuff with config" )
#PluginTarget( GraphDatabaseService.class )
public void sample( #Source GraphDatabaseService graphDb ) {
Config config = ((GraphDatabaseAPI)graphDb).getDependencyResolver().resolveDependency(Config.class);
// do stuff with config
}
}
I use Properties:
Properties props = new Properties();
try
{
FileInputStream in = new FileInputStream("./conf/neo4j.properties");
props.load(in);
}
catch (FileNotFoundException e)
{
try
{
FileInputStream in = new FileInputStream("./neo4j.properties");
props.load(in);
}
catch (FileNotFoundException e2)
{
logger.warn(e2.getMessage());
}
catch (IOException e2)
{
logger.warn(e2.getMessage());
}
}
catch (IOException e)
{
logger.warn(e.getMessage());
}
String myPropertyString = props.getProperty("myProperty");
if (myPropertyString != null)
{
myProperty = Integer.parseInt(myPropertyString);
}
else
{
myProperty = 100;
}
and in neo4j.properties I have:
...
# Enable shell server so that remote clients can connect via Neo4j shell.
#remote_shell_enabled=true
# Specify custom shell port (default is 1337).
#remote_shell_port=1234
myProperty=100

What is using connection in the code

What is the purpose of using (connection) in the code - please explain me
static void HasRows(SqlConnection connection)
{
using (connection)/// what is this line
{
SqlCommand command = new SqlCommand(
"SELECT CategoryID, CategoryName FROM Categories;",
connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
reader.GetString(1));
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
}
using (connection){
connection.Open();
}
makes sure that connection is closed when the application is done using it.
similar to a Try Catch.
try{
connection.Open();
}
catch{
}
finally{
connection.Dispose();
}
Disposing of the connection is another way of saying closing a connection. An open connection can leak memory and if you have too many it can slow down or freeze up whatever you are connecting to.
the using function closes the connection even after you return something from the class you are in. same as the try catch. it always closes the connection no matter what happens inside the brackets. even if there is an exception that breaks out of the class/application the connection still gets closed
Using
Quote from site:
The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.
I would probably re-write the method to something like the following:
static void HasRows(string connectionString)
{
using (var connection = new SqlConnection(connectionString))
using(var command = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories;",
connection))
{
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
reader.GetString(1));
}
}
else
{
Console.WriteLine("No rows found.");
}
}
}
}
In your original implementation the caller could be at the receiving end of an ObjectDisposedException exception, because the SqlConnection is passed in as a parameter.

Windows Application SqlDepedency Calling Onchange infinitely

I have console application in which I am doing sqldependency. My problem is when I set commandType as Text, it is working fine. But if I use commandType as StoredProcedure, onchange method is calling infinitely.
Please see the code below:
static DataSet myDataSet;
static SqlConnection connection;
static SqlCommand command;
static void Main(string[] args)
{
// Remove any existing dependency connection, then create a new one.
string connstr = "Data Source=XYZ;Initial Catalog=Dev;Integrated Security=True";
string ssql = #"[dbo].[SchedulerPendingControlRequestIDFetch]";
CanRequestNotifications();
SqlDependency.Stop(connstr);
SqlDependency.Start(connstr);
if (connection == null)
connection = new SqlConnection(connstr);
if (command == null)
command = new SqlCommand(ssql, connection);
command.CommandType = CommandType.StoredProcedure;
if (myDataSet == null)
myDataSet = new DataSet();
GetAdvtData();
System.Console.ReadKey();
connection.Close();
}
private static bool CanRequestNotifications()
{
SqlClientPermission permission =
new SqlClientPermission(
PermissionState.Unrestricted);
try
{
permission.Demand();
return true;
}
catch (System.Exception)
{
return false;
}
}
private static void GetAdvtData()
{
myDataSet.Clear();
// Ensure the command object does not have a notification object.
command.Notification = null;
// Create and bind the SqlDependency object to the command object.
SqlDependency dependency = new SqlDependency(command,null,100);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
adapter.Fill(myDataSet, "ControlRequest");
}
}
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
SqlDependency dependency =
(SqlDependency)sender;
dependency.OnChange -= dependency_OnChange;
Console.WriteLine(e.Info.ToString() + e.Source.ToString());
GetAdvtData();
}
My stored Procedure is:
IF OBJECT_ID('SchedulerSirasColcoDetailFetch') IS NOT NULL
DROP PROCEDURE SchedulerSirasColcoDetailFetch
Go
PRINT 'Creating stored procedure SchedulerSirasColcoDetailFetch'
Go
CREATE PROCEDURE [dbo].[SchedulerSirasColcoDetailFetch]
AS
BEGIN
SELECT Colco_Code AS 'CountryCode',Connection_String AS 'Url',Resend_Interval AS 'ResendInterval',
Default_Encoding AS 'Encoding' FROM dbo.SirasColcoDetail
END
If I copy the select statement inside stored procedure as my command text and set the commandType as Text, everything is working fine.
could you please let me know what the issue is????
Thanks a lot in advance.
Mahesh
You're supposed to check the values of the SqlNotificationEventArgs argument. Only if Type is Change and Source is Data where you notified for a data change.
You'll discover that you're not notified for data changes, but for incorrect settings or incorrect query. Your query and connection settings must comply with the requirements specified in Creating a Query for Notifications.

VSTO 3.0 Get/Change an excel 2007 workbook connection

I've struggling to find a way to get and change and excel 2007 workbook connection (Menu Data -> Existing Connections -> Connections on this Workbook).It's a connection (several actually) to a SQL Server and used in a pivot table.
I've tried using Application.ActiveWorkbook.Connections or Globals.ThisWorkbook.Connections but they both return always Null..I've tried in an sheet event as well as in a custom ribbon's button event as well.
The only way left I can think of is use to code a VBA method that does the work and then invoke it in my VSTO code, but it's not very elegant is it...
Existing connections in Excel (this works in 2007) are not active connections. You must connect using an existing connection before being able to acquire that connection (I did this manually before clicking a button that executed this code).
var application = Globals.ThisAddIn.Application;
//This must be an active connection otherwise handle exceptions
// such as 'Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))'
var connection = application.ActiveWorkbook.Connections["EXISTING_CONNECTION_NAME"];
var oledb = connection.OLEDBConnection;
var settings = oledb.Connection;
Here I adjust connection string of Excel Connections. Take in account that I have only ONE connection in Workbook.
public class WorkbookConnectionsManager
{
public static void AdjustConnectionToSqlConnectionString(Excel.WorkbookConnection connection, String connectionString)
{
char[] propertiesSeparator = new char[] { ';' };
char[] propertyValueSeparator = new char[] { '=' };
Excel.OLEDBConnection oleDbConn = connection.OLEDBConnection;
Dictionary<string, string> dictExcelConnStrProperties = GetConnStrDictionary(oleDbConn.Connection, propertiesSeparator, propertyValueSeparator);
Dictionary<string, string> dictActualConnStrProperties = GetConnStrDictionary(connectionString, propertiesSeparator, propertyValueSeparator);
string[] reggedPropertyies = new string[] { "Integrated Security", "Persist Security Info", "User ID", "Password", "Initial Catalog", "Data Source", "Workstation ID" };
foreach (string property in reggedPropertyies)
if (dictExcelConnStrProperties.ContainsKey(property) && dictActualConnStrProperties.ContainsKey(property)
&& null != dictActualConnStrProperties[property] && !String.IsNullOrEmpty(dictActualConnStrProperties[property].ToString()))
dictExcelConnStrProperties[property] = dictActualConnStrProperties[property];
string connStr = GetConnStrFromDict(dictExcelConnStrProperties, propertiesSeparator[0], propertyValueSeparator[0]);
oleDbConn.Connection = connStr;
}
private static string GetConnStrFromDict(Dictionary<string, string> dictConnStrProperties, char propertiesSeparator, char propertyValueSeparator)
{
StringBuilder connStrBuilder = new StringBuilder();
foreach (KeyValuePair<string, string> keyValuePair in dictConnStrProperties)
{
connStrBuilder.Append(keyValuePair.Key);
if (!String.IsNullOrEmpty(keyValuePair.Value))
{
connStrBuilder.Append(propertyValueSeparator);
connStrBuilder.Append(keyValuePair.Value);
}
connStrBuilder.Append(propertiesSeparator);
}
string connStr = String.Empty;
if (connStrBuilder.Length > 1)
{
connStr = connStrBuilder.ToString(0, connStrBuilder.Length - 1);
}
return connStr;
}
private static Dictionary<string, string> GetConnStrDictionary(string connString, char[] propertiesSeparator, char[] propertyValueSeparator)
{
string[] keyAndValue;
string[] arrayConnStrProperties = connString.Split(propertiesSeparator);
Dictionary<string, string> dictConnStrProperties = new Dictionary<string, string>();
foreach (string excelConnStrProperty in arrayConnStrProperties)
{
keyAndValue = excelConnStrProperty.Split(propertyValueSeparator);
if (keyAndValue.Length > 1)
{
dictConnStrProperties.Add(keyAndValue[0], keyAndValue[1]);
}
else if (keyAndValue.Length > 0)
{
//standalone attribute
dictConnStrProperties.Add(keyAndValue[0], String.Empty);
}
}
return dictConnStrProperties;
}
}
I can't remember where but I remember reading somewhere that the Connections collection was of limited use for writing ODBC type connections. It has several enum values for a "connection" but I'm not sure if some of those are readonly from that interface.
Regardless it should be quite easy to implement new connections and edit existing ones from VSTO. The best option would be to use COM interop to call the SQLConfigDataSource() function from the ODBCCP32.DLL win32 library.
Also check out the following addin which makes it easier to work with query tables in Excel.

Resources