unitycontainer / registration-by-convention

Unity.RegistrationByConvention package (Requires Core Implementation. Any contributors?)
Apache License 2.0
4 stars 12 forks source link

TypeLoadException in v2.1.6 #8

Closed ChipsetSV closed 5 years ago

ChipsetSV commented 5 years ago

Hello. I catched exception, when I use RegisterTypes with v2.1.6: Inheritance security rules violated while overriding member: "Unity.RegistrationByConvention.Exceptions.DuplicateTypeMappingException.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)". Security accessibility of the overriding method must match the security accessibility of the method being overriden. I use package with Net.Framework 4.6.1.

kassim-shaikh commented 5 years ago

Hello, I too facing the exact error. any help would be highly appreciated.

ENikS commented 5 years ago

Please provide unit test so I could reproduce the issue

ChipsetSV commented 5 years ago

Enough 1 line: Container.RegisterTypes(new[] { typeof(DummyType) }, WithMappings.FromAllInterfaces, WithName.Default, WithLifetime.ContainerControlled);. Exception throws on Net.Framework 4.6.1 project (and also tried on 4.5.2, exception throws too).

s-bauer commented 5 years ago

Facing the same error after updating to version 2.1.6. I'm using Unity in a Plugin for Microsoft Dynamics 365, so I'm running in a sandbox with a limited PermissionSet!

ENikS commented 5 years ago

Gentlemen, if you want me to fix this issue you need to be a bit more helpful. You want me to spend my time but too busy to write one Unit Test?

GreenKn1ght commented 5 years ago

Your unit tests cover this scenario, but this PR dropped support of .NET Framework for them. My commit brings support for all frameworks and now you can see this: image

kalebpederson commented 5 years ago

I ran into this exact same error today. @ENikS, what can I do to help get this merged in?

ENikS commented 5 years ago

Send me a PR that fixes this issue?

s-bauer commented 5 years ago

Uhm I think it should be pretty straight forward. Take a look at https://msdn.microsoft.com/en-us/library/ms182315(v=vs.80).aspx

The GetObjectData override in DuplicateTypeMappingException is missing [SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]

I'll take a look at this myself once Visual Studio is updated :)

ENikS commented 5 years ago

@s-bauer

Excellent. If it is strait forward you, @kalebpederson or perhaps someone else should be able to fix it and submit PR.

I have to admit, this particular library is not on my priority list at the moment. So, unless someone sends a PR this will have to wait until Unity 5.9.0 is released.

s-bauer commented 5 years ago

@ENikS I will in a few minutes / hours. I just had to update my Visual Studio installation first.

GreenKn1ght commented 5 years ago

@s-bauer nice to hear it. Also please include my changes to prevent similar situations in the future.

kalebpederson commented 5 years ago

Thank you all! I'll help and/or test as needed, so please let me know.

s-bauer commented 5 years ago

I created https://github.com/unitycontainer/registration-by-convention/pull/9

ENikS commented 5 years ago

@kalebpederson @GreenKn1ght @ChipsetSV

Please let me know if you are OK with the PR. If no complains I'll merge and release it.

ChipsetSV commented 5 years ago

@ENikS, I approved these changes. @s-bauer, thanks ))

ENikS commented 5 years ago

Posted few questions for you.

ENikS commented 5 years ago

Released 2.1.9

NeoXtreem commented 5 years ago

It appears that the Unity build is broken. Is this why the fix is not getting into the NuGet release for Unity?

s-bauer commented 5 years ago

@NeoXtreem The fix is already on Nugget: https://www.nuget.org/packages/Unity.RegistrationByConvention/

It's a separate package!

NeoXtreem commented 5 years ago

@s-bauer I see that, but isn't it pulled in by projects that only reference the Unity package because, according to here, Unity is a 'composite' of all the other packages including Unity.RegistrationByConvention? Therefore, my understanding is that, if the build is working, then the composite Unity package would be released and include the updated Unity.RegistrationByConvention package, right?

ENikS commented 5 years ago

@NeoXtreem Technically you right but Unity package is released on different schedule so if you want this change now you might want to link to individual package instead.

NeoXtreem commented 5 years ago

@ENikS I tried this and I got the following error:

System.IO.FileLoadException: 'Could not load file or assembly 'Unity.RegistrationByConvention, Version=2.1.7.0, Culture=neutral, PublicKeyToken=489b6accfaf20ef0' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'

I think the best solution for people trying to get their projects working without having to mess around with references too much is to downgrade to Unity 5.8.6 until it is updated. But this does result in version conflicts that I have been unable to resolve as I have a mixture of .NET Framework class libraries using NuGet PackageReference and ASP.NET websites using NuGet packages.config. So, downgrading Unity.Container to 5.8.6 also and Unity.Abstractions to 3.3.0 on the ASP.NET websites didn't cut it unfortunately.

Any idea when the Unity package will be updated to 5.9.0?

ENikS commented 5 years ago

@s-bauer I am assuming it works for you? Could you be so kind and help @NeoXtreem with proper setup?

s-bauer commented 5 years ago

Take a look at this very basic example: https://github.com/s-bauer/UnitySample You just need Unity.Abstractions, Unity.Container and Unity.RegistrationByConvention as dependencies. Do not add the Unity package as a NuGet refernce.