9

I would like to run this function under mono ( my current version is 4.0.2)

 public Object GetConnectionProperty(SqlConnection _conn, string prop)
    {
        if (_conn.State == ConnectionState.Closed &
            prop == "ServerVersion")
            return string.Empty;

        if (prop == "ClientConnectionId")
        {
            Guid guid = _conn.ClientConnectionId;
            return guid.ToString();
        }
        return _conn.GetType().GetProperty(prop).GetValue(_conn);
    }

But it fails with the error :

error CS1061: Type `System.Data.SqlClient.SqlConnection' does not contain a 
definition for `ClientConnectionId' and no extension method 
`ClientConnectionId' of type `System.Data.SqlClient.SqlConnection' could be 
found. Are you missing an assembly reference?

What is the Mono equivalent of ClientConnectionId? Or how can I fix it?

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • 2
    From the [source code](https://github.com/mono/mono/blob/master/mcs/class/System.Data/System.Data.SqlClient/SqlConnection.cs), `ClientConnectionId` is not implemented. Why do you need it? (to see if there is a workaround) – manji Jul 13 '15 at 18:33
  • @manji good to know. I want to know if there is a workaround ffor exampel chech for the OS system or check for the compiler.. – agstudy Jul 13 '15 at 20:27

1 Answers1

1

ClientConnectionId is not implemented in mono SqlConnection class. If you really want to have some unique identifier for each instance, you can do it yourself as having an id constructed from the hashcode for example:

public static class SqlClientExtensions {
#if __MonoCS__
    private static Dictionary<int, string> _connIds = new Dictionary<int, string>();
#endif

    public static string GetClientConnectionId(this SqlConnection conn) {
        if(conn == null) {
            return Guid.Empty.ToString();
        }

#if __MonoCS__
        if(!connIds.ContainsKey(conn.GetHashCode())) {
            connIds.Add(conn.GetHashCode(), Guid.NewGuid().ToString());
        }

        return connIds[conn.GetHashCode()];
#else
        return conn.ClientConnectionId.ToString();
#endif
    }
}

then you can use it in your method:

if (prop == "ClientConnectionId")
{
    return _conn.GetClientConnectionId();
}

P.S.:

The hash code may be repeated for 2 different instances at different points in time.

P.S.(2):

__MonoCS__ is defined only by the mono compiler. The portion calling the ClientConnectionId property wil not be seen by mono compiler and vice versa for the other portion and .net compiler.

P.S.(3):

Another solution would be to subclass SqlConnection and implement ClientConnectionId but it's sealed... And also that would require subclassing some other classes that instantiate the SqlConnection class internally.

manji
  • 47,442
  • 5
  • 96
  • 103
  • Excellent answer ! this will be part of [rsqlserver](https://github.com/agstudy/rsqlserver/commit/408ff64d4d46c693a34f34fdc181295c29badc0e) R package to connect from R to MS SQL Server. – agstudy Jul 14 '15 at 14:20
  • Hash codes, by definition, are not guaranteed to be unique. http://stackoverflow.com/questions/7425142/what-is-hashcode-used-for-is-it-unique – sstan Mar 20 '17 at 15:31