Domanda

I develop a wpf host app with plugins and want to share them SqlConnection, but when i try to pass object of SqlConnection via Remoting, i get an error:

remoting cannot find field 'objectid' on type 'system.data.sqlclient.sqlconnection'

Google and SO didn't help me and the only topic i found is with broken links :(

Can i somehow pass SqlConnection? The trouble is that i already have ado.net-based application and i don't have a possibility to rewrite them. And on the other hand, i need to provide different plugins access to same connection because they can interop via temporary tables.

Exception info:

(last error: Remoting cannot find field 'ObjectID' on type 'System.Data.SqlClient.SqlConnection'.). 
StackTrace:
Server stack trace: 
   at System.Object.GetFieldInfo(String typeName, String fieldName)
   at System.Object.FieldGetter(String typeName, String fieldName, Object& val)
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at System.Object.FieldGetter(String typeName, String fieldName, Object& val)
   at System.Data.SqlClient.SqlCommand.set_Connection(SqlConnection value)
   at LSSample.SampleUserControl.ExecuteSqlLocal() in c:\Users\voskresenskiy\Documents\Visual Studio 2013\Projects\UIContainer\WpfHost\LSSample\SampleUserControl.xaml.cs:line

I'll be gratefull for any help

È stato utile?

Soluzione

From the exception call stack and using Reflector I can see that SqlCommand.set_Connection is trying to access SqlConnection.ObjectID. SqlConnection.ObjectID is marked as internal, only public members are available via remoting.

Doesn't SqlConnection use pooling to minimize the number of database connections? SQL Server Connection Pooling

Could you have one process that talks to the database, then all of the other processes will request data from it, just by passing it the query string?

Altri suggerimenti

We recently ran into the same issue and I think the problem is creating the SqlCommand locally via calling it's constructor. When then assigning the connection to it it tries to clone the connection locally as well which fails due to some internal properties which are not available through remoting.

The workaround which did the trick for us was to call CreateCommand() on the connection like this

void MethodRunningInAppDomainB(SqlConnection connectionFromAppDomainA, SqlTransaction transactionFromAppDomainA)
{
    using (var cmd = connectionFromAppDomainA.CreateCommand())
    {
        cmd.CommandText = "this is my query";
        cmd.Transaction = transaction;

        cmd.Parameters.Add(...);
        ...

        cmd.ExecuteNonQuery();
    }
}

Bascially the connection and the transaction came from the main app which got passed to the plugin which is running in a different AppDomain (so it can be unloaded) which could execute some custom commands in the context of the main query.

My suspicion is that calling CreateCommand() on the connection remotes the call back and creates the command in the same app domain as the connection lives in and the created command you get back is probably a proxy to that and that's why this way works.

Not sure if my explanation of the problem is 100% correct but it sounds somewhat logical to me and in any case it resolved the problem for us.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top