Problem with PolicyInjection.Create Method

Topics: Policy Injection Application Block
May 30, 2007 at 6:14 PM
I try to use this method with a target class that implement an Interface in this way:

Private _proxy As IServicio = PolicyInjection.Create(Of Servicio, IServicio)()

where Servicio is a class that implements interface IServicio and has these five constructors:



Public Sub New()
MyBase.New
End Sub

Public Sub New(ByVal endpointConfigurationName As String)
MyBase.New(endpointConfigurationName)
End Sub

Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As String)
MyBase.New(endpointConfigurationName, remoteAddress)
End Sub

Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
MyBase.New(endpointConfigurationName, remoteAddress)
End Sub

Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
MyBase.New(binding, remoteAddress)
End Sub


When I call the create method above I get this exception:

KeyNotFoundException: The given key was not present in the dictionary.]
System.ThrowHelper.ThrowKeyNotFoundException() +28
System.Collections.Generic.Dictionary`2.get_Item(TKey key) +2636020
Microsoft.Practices.EnterpriseLibrary.PolicyInjection.RemotingInterception.InterceptingRealProxy.AddHandlersForInterface(Type targetType, Type itf) +98
Microsoft.Practices.EnterpriseLibrary.PolicyInjection.RemotingInterception.InterceptingRealProxy..ctor(Object target, Type classToProxy, PolicySet policies) +215
Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjector.DoWrap(Object instance, Type typeToReturn, PolicySet policiesForThisType) +63
Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjector.Create(Object[] args) +167
Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjection.Create(Object[] args) +65
Model..ctor() in C:\Projects\Prueba\Model.vb:24

I have tried PIAB and Create method with classes that implementes an interface and do not have explicit constructors and worked fine but when I tried with this specific class and interface I couldn´t make it work.
Could someone help me ?
Thanks in advance.

May 31, 2007 at 4:05 PM
This should work and target the parameterless constructor:

IServicio servicio = PolicyInjection.Create<Servicio, IServicio>();

You might try wrapping as a test just to make sure the type is being created successfully:

IServicio servicio = new Servicio();
PolicyInjection.Wrap<IServicio>(servicio);

Regards,

Dave

_________________________

David Hayden
Microsoft MVP C#

May 31, 2007 at 5:56 PM
Thanks David, I´ll try it.
Just in case, could the problem be related to the use of WCF ?
I´m asking this because the class "servicio" in the example is a
proxy WCF generated with svcutil.
Thanks again.
Jun 2, 2007 at 7:08 AM
Was this using 3.0 or 3.1? That stack trace looks quite similar to one caused by a bug that was fixed in 3.1.

-Chris
Jun 4, 2007 at 12:37 PM
This was using 3.0. Could it be solved with version 3.1 ?
Thanks, Javier.
Jun 4, 2007 at 3:02 PM
I made new tests, now without using WCF, and the problem persists.
Which could be the problem when the AddHandlersForInterface executes ?
Thanks.
Jun 4, 2007 at 8:47 PM
If you can give 3.1 a try, please do. Like I said, that particular error looks a lot like a bug that was fixed in 3.1.