PolicyInjection & RemotingException

Topics: Policy Injection Application Block
Jul 27, 2007 at 2:13 AM
Dear all,

I have tried to wrap a SqlConnection with PolicyInjection to make sure the some store procedure will be executed after the connection is opened.
However, when I assigned the wrapped connection to SqlCommand.Connection, it throws a RemotingException. Wonder whether it is because the SqlCommand and DbCommand defined a property Connection with type SqlConnection and DbConnection respectively. If so, how we can fix the problem?

{
Failures:
1) XXX.TestGetSqlConnectionWithAppRoleViaPolicyInjection : System.Runtime.Remoting.RemotingException : Remoting cannot find field 'ObjectID' on type 'System.Data.SqlClient.SqlConnection'.

Server stack trace:
at System.Object.GetFieldInfo(String typeName, String fieldName)
at System.Object.FieldGetter(String typeName, String fieldName, Object& val)

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 XXX.TestGetSqlConnectionWithAppRoleViaPolicyInjection() in C:\XXX\IOT.Framework.Data.Test\ConnectionFactoryTest.cs:line 182
}

Thanks and regards,
William
Jul 27, 2007 at 2:07 PM
Hi,

I don't think this will work. PIAB relies on remoting proxies, and AFAIK they don't work with nonpublic fields like 'ObjectID' on SqlConnection accessed by the SqlCommand constructor.
You will need to try a different approach to implement this feature.

Regards,
Fernando
Jul 28, 2007 at 4:31 AM
Hi Fernando,

Do you any suggestion if we want to have AOP functionality for SqlConnection to make sure it will execute a store procedure upon its opening? If the problem is coming from the limitation of "remoting proxies", do you think we can implement a PolicyInjector with CodeEmit? Thanks a lot for your kindly suggestion.

Regards,
William
Jul 30, 2007 at 3:19 PM
Hi William,

I'm sorry I don't currently have any suggestion on how to deal with this case. I sent a few inquiries on this so I'll post any information I get.

I don't think CodeEmit will help here; the SqlCommand is reading a field, not invoking a method or reading a property. But this is really not my area of expertise, so it may be worth giving it a shot.

Regards,
Fernando

Jul 31, 2007 at 1:49 AM
Hi Fernando,

So, do you mean we should not wrap any class with Policy Injection if the class directly accesses a field of other class? I wonder how we can know this until we hit the problem if the class to be wrapped is developed by someone else, like Microsoft.

Thanks and regards,
William


Jul 31, 2007 at 1:35 PM
Hi,

The issue is not with the wrapped class itself but with other classes that access the the non-public fields of the wrapped class, so the suitability of a class for wrapping is not really an intrinsic property but depends on its usage.

Regards,
Fernando