App.Config woes

Dec 21, 2011 at 4:18 PM
Edited Dec 21, 2011 at 4:19 PM

Having trouble getting types to register in App.Config.  Works with runtime registration though

I have an interface called IValidator(of T)

Public Interface IValidator(Of T)
    Sub SetResults(ByRef results As ValidationResult)
    Function ValidateProperty(ByVal propertyName As String, ByVal obj As T) As Boolean
    Function Validate(ByVal obj As T) As Boolean
End Interface

I have a class which implements IValidator where T is of type Accident.  It is called AccidentValidator

I Register that type with Unity at runtime:

_Ioc.RegisterType(Of IValidator(Of Accident), AccidentValidator)("Accident")

I then call:

_Ioc.Resolve(Of Accident)("Accident")

Validator starts up...the world spins normally.  This works flawlessly for all other validators as well!

Try and put that in the Config file instead of at runtime and almost everything I do gives me this error:

The current type, ValidationTest.IValidator`1[ValidationTest.Accident], is an interface and cannot be constructed. Are you missing a type mapping?

Can someone help?  Entlib 5.0, VS 2010

Dec 21, 2011 at 4:29 PM
Edited Dec 21, 2011 at 4:29 PM

Can you post your config?  Thanks.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com

Dec 21, 2011 at 4:37 PM
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias  alias="AccidentVal" type ="ValidationTest.AccidentValidator, ValidationTest"  />
    
    <container>
      <register type ="IValidator(of Accident)"  mapTo ="AccidentVal"  name="Accident" />
    </container>
    
  </unity>

 

This is just one of them that doesn't work.  :)

Dec 21, 2011 at 5:27 PM
Edited Dec 21, 2011 at 5:28 PM

Try:

 

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <namespace name="ValidationTest" />
    <assembly name="ValidationTest" />
    <alias  alias="AccidentVal" type ="ValidationTest.AccidentValidator, ValidationTest"  />

    <container>
      <register type ="IValidator[Accident]"  mapTo ="AccidentVal"  name="Accident" />
    </container>

  </unity>

See Specifying Types in the Configuration File for the generic syntax.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com

Dec 21, 2011 at 7:04 PM

Same thing

Read up on the link you provided and tried a few more things including the `1

 

No luck

Dec 21, 2011 at 7:21 PM
Edited Dec 24, 2011 at 5:02 AM

It works fine on my end...are you loading the container?  Along with the config above this code runs fine:

Dim container = New UnityContainer()
Dim section = ConfigurationManager.GetSection("unity")

section.Containers.Default.Configure(container)

Dim accident = container.Resolve(Of Accident)()
Dim validator = container.Resolve(Of IValidator(Of Accident))("Accident")
validator.Validate(accident)

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com

Dec 21, 2011 at 7:37 PM
Edited Dec 21, 2011 at 7:43 PM

Another way to configure would be to create an alias for the generic interface.  This also works:

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="AccidentVal" type ="ValidationTest.AccidentValidator, ValidationTest"  />
    <alias alias="IValidator"  type="ValidationTest.IValidator`1, ValidationTest"/>

    <container>
      <register type="IValidator" mapTo="AccidentVal" name="Accident" />
    </container>

  </unity>

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com

Dec 21, 2011 at 7:54 PM

Oh CUSS.

lol...Thanks Randy.  Forgot Unity doesn't automatically read unity section.

I very much avoid posting anywhere because I don't want to look like I didn't RTFM.  But for some reason I just couldn't get past this.  I was so overwhelmed by it that I forgot the elementary bits.

That's torqued me off just a bit.  So I'm done for the day in protest.

 

Thank you for your help.  Very quick!  Surprisingly quick.

Dec 21, 2011 at 7:57 PM

No worries.  I think everyone's been bitten by that one at least once!  Especially if you are using Enterprise Library which does automatically load the config.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com