App.Config does cannot access section specified - c#

http://pastebin.com/QaCCM2Zv
I have a config file in the above link, every time i try to access the section 'InventoryFactory' using this code:
var config = (ObjectFactoryConfiguration)ConfigurationManager.GetSection("InventoryFactory");
it returns an error:
An exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.Configuration.dll but was not handled in user code
Additional information: An error occurred creating the configuration section handler for InventoryFactory: Could not load type 'WDG.ObjectFactory.ObjectFactoryConfiguration' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Any idea guys? Thanks :

If you haven't done so already you probably need to set up a custom configuration section handler to parse the "InventoryFactory" section into your "ObjectFactoryConfiguration" object. The error message indicates that the runtime is trying to load the "WDG.ObjectFactory.ObjectFactoryConfiguration" class from the "System.Configuration" assembly which wouldn't contain any of your custom classes.
...Also, you want to specify the assembly in the section, if you look at the line right above yours:<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> you can see at the end there the "log4net" assembly has been specified. Try putting the name of your assembly which contains the "ObjectFactoryConfiguration" class in the type attribute as well.

Related

Can't register a View and ViewModel between 2 assemblies (UWP, Splat)

Using the latest RxUI v8 preview and Splat 2.0, in a UWP project referencing a .Net Standard 2.0 library, I can't register my view and viewmodel unless they reside in the same assembly.
I have:
Locator.CurrentMutable.RegisterLazySingleton(() => new HomeView(), typeof(IViewFor<HomeViewModel>));
But Splat gives an error:
DefaultViewLocator: Failed to find type named 'RxUI.UWP.Core.Views.HomeView, RxUI.UWP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
DefaultViewLocator: Failed to resolve service for type 'ReactiveUI.IViewFor`1[[RxUI.UWP.Core.ViewModels.HomeViewModel, RxUI.UWP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
DefaultViewLocator: Failed to find type named 'ReactiveUI.IRoutableView, ReactiveUI, Version=8.0.0.0, Culture=neutral, PublicKeyToken=null'.
DefaultViewLocator: Failed to resolve service for type 'ReactiveUI.IViewFor`1[[ReactiveUI.IRoutableViewModel, ReactiveUI, Version=8.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
DefaultViewLocator: Failed to resolve view for view model type 'ReactiveUI.IRoutableViewModel'.
DefaultViewLocator: Failed to find type named 'RxUI.UWP.Core.Views.HomeView, RxUI.UWP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
So it's looking for the HomeView in the "Core" assembly, but it resides in the UWP project. Here's the structure...
I experienced the same issue with a similar environment as your. The issue resided in the DefaultViewLocator, since the view type is being renamed and resolved incorrectly to the viewmodel's assembly and namespace.
See lines 133-134 for how the view type name is determined:
var viewModelTypeName = viewModelType.AssemblyQualifiedName;
var proposedViewTypeName = this.ViewModelToViewFunc(viewModelTypeName);
Note: ViewModelToViewFunc is only a String.Replace that replaces "ViewModel" to "View" (see constructor).
To resolve this issue, my workaround was to create my own IViewLocator implementation, something like:
public class MyViewLocator : IViewLocator {
public MyViewModelLocator(Assembly viewAssembly, string viewNameSpace)
...
private IViewFor AttemptViewResolutionFor(Type viewModelType, string contract)
{
// proposed view type is now based on provided namespace + classname as modified by ViewModelToViewFunc
if (viewModelType == null) return null;
var viewModelTypeName = viewModelType.Name;
var proposedViewTypeName = _viewNamespace + "." + this.ViewModelToViewFunc(viewModelTypeName);
...
private IViewFor AttemptViewResolution(string viewTypeName, string contract)
{
try
{
// resolve view type in the assembly of the view, and not assembly of the viewmodel
var viewType = _viewAssembly.GetType(viewTypeName); // instead of Reflection.ReallyFindType(viewTypeName, throwOnFailure: false);
...
}
Finally, your custom viewlocator implementaiton must be registered with splat so that it overwrites DefaultViewLocator implementation:
Locator.CurrentMutable.RegisterConstant<IViewLocator>(new MyViewLocator(typeof(SplashView).Namespace, typeof(SplashView).Assembly));

WCF DataContractSerializer and XAML Namespaces

I am sending an object to a WCF service that contains a Windows Workflow definition, but the deserializer is faulting when trying to deserialize my custom activities.
This was previously working when I had the activities' namespaces defined in the form of:
xmlns:tta="clr-namespace:MyNamespace;assembly=MyAssembly"
but for maintainability reasons I have now mapped my activity namespaces to a XAML namespace using assembly attributes:
[assembly: XmlnsPrefix("http://schemas.product.com/activities/", "tta")]
[assembly: XmlnsDefinition("http://schemas.product.com/activities/", "MyNamespace")]
Thus my xaml namespace looks like: xmlns:tta="http://schemas.thacktech.com/activities/"
And my activity declared as: <tta:Naptime />
Because of this change, I now recieve NetDispatcherFaultException which reads:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:job. The InnerException message was 'Element 'http://schemas.datacontract.org/2004/07/System.Activities:Activity' contains data from a type that maps to the name 'Naptime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null:MyNamespace.Naptime'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'MyNamespace.Naptime' and namespace 'Naptime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.'. Please see InnerException for more details.
Questions:
Why does the deseralizer succeed with type resolution when using the clr-namespace syntax, but fail when using the url-style syntax for a namespace declaration?
It looks like the type resolver is completely misinterpreting the type, listing the class name as the namespace. Why would it do this?
How do I properly implement this?
Thanks!

Bootstrapper.AutoMapper and Profile registration order

I'm working on an ASP.NET MVC 3 application using AutoMapper 2.2.0. I have some AutoMapper profiles declared and when I initialize them manually everything works just fine.
AutoMapper.Mapper.Initialize(x =>
{
x.AddProfile<ProdutoToProdutoViewModel>();
x.AddProfile<IPagedListProdutoToIPagedListProdutoViewModel>();
x.AddProfile<ItemToItemViewModel>();
x.AddProfile<CarrinhoToCarrinhoViewModel>();
});
//This is working
But when I try to initialize them with Bootstrapper.AutoMapper 2.0.3.0...
Bootstrapper.With.AutoMapper().Start();
...a configuration exception is thrown:
The following property on eGuruShop.Web.ViewModels.ItemViewModel cannot be mapped:
Itens
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type eGuruShop.Web.ViewModels.ItemViewModel.
Context:
Mapping to property Itens from eGuruShop.Domain.CatalogoProdutos.Item to eGuruShop.Web.ViewModels.ItemViewModel
Mapping to property Itens from System.Collections.Generic.IList`1[[eGuruShop.Domain.CatalogoProdutos.Item, eGuruShop.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.IList`1[[eGuruShop.Web.ViewModels.ItemViewModel, eGuruShop.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Mapping from type eGuruShop.Domain.CatalogoProdutos.Carrinho to eGuruShop.Web.ViewModels.CarrinhoViewModel
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown
The CarrinhoToCarrinhoViewModel profile depends on the ItemToItemViewModel profile and when I change the initialization order to
AutoMapper.Mapper.Initialize(x =>
{
x.AddProfile<ProdutoToProdutoViewModel>();
x.AddProfile<IPagedListProdutoToIPagedListProdutoViewModel>();
x.AddProfile<CarrinhoToCarrinhoViewModel>();
x.AddProfile<ItemToItemViewModel>();
});
//Exception
I've got the same exception than before.
I suspect Bootstrapper is initializing the profiles in the wrong order, but I don't know how to solve it without abandoning Bootstrapper. Any suggestions or solutions to this problem?
Thanks

Custom ConfigurationSection type not loading correctly

Every time I do a ConfigurationManager.GetSection("registeredPlugIns") for this custom section I receive this error:
An error occurred creating the configuration section handler for registeredPlugIns:
Could not load type 'Engine.PlugInArch.PlugInConfigurationSection'
from assembly 'System.Configuration, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'.
Why is it trying to load the type from System.Configuration and not the assembly that I ask it to?
Here is my Section code:
namespace Engine.PlugInArch
{
public class PlugInConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("plugIns", IsDefaultCollection = false),
ConfigurationCollection(typeof(PlugInCollection), AddItemName = "addPlugin")]
public PlugInCollection PlugIns
{
get { return this["plugIns"] as PlugInCollection; }
}
}
}
And here is my app.config
<configuration>
<configSections>
<section name="registeredPlugIns" type="Engine.PlugInArch.PlugInConfigurationSection, Engine"/>
</configSections>
...
<registeredPlugIns>
<plugIns>
<addPlugIn DllName="ProcessorPlugin.dll"/>
</plugIns>
</registeredPlugIns>
</configuration>
Is your dll called Engine.dll? I think not and that is where the problem is.
OK, run procmon from sysinternals. Set the filter to your process name and also filter for result="NAME NOT FOUND". You will see entries where it is looking for Engine.dll or Engine.exe. See where it is looking for it and it is likely the file needs to be copied to the running folder.

Exception Due to Type Missing From Assembly (Revised)

I have made some progress on the problem I posted about yesterday, so I am rewriting the post.
My problem appears to be related to my use of generics. Here's the relevant part of App.config (formatted with whitespace for readability):
<configSections>
<section
name="NA5300ResolverSynchroDevices"
type="InfrastructureModule.DeviceConfiguration.DeviceConfigurationSection
<NA5300ResolverSynchroModule.NA5300ResolverSynchroConfigurationElement>,
NA5300ResolverSynchroModule">
</section>
</configSections>
<NA5300ResolverSynchroDevices>
<Device deviceName="AzResolverSynchro" busAddress="7"/>
<Device deviceName="ElResolverSynchro" busAddress="8"/>
</NA5300ResolverSynchroDevices>
Here's the class I'm trying to map to the configuration section:
namespace InfrastructureModule.DeviceConfiguration
{
public class DeviceConfigurationSection<T> : ConfigurationSection
where T : DeviceConfigurationElement, new()
{
[ConfigurationProperty("", IsDefaultCollection = true, IsKey = false)]
public DeviceConfigurationElementCollection<T> Devices
{
get { return (DeviceConfigurationElementCollection<T>) base[""]; }
set { base[""] = value; }
}
}
}
Here's the C# code that tries to access the config file:
DeviceConfigurationSection<NA5300ResolverSynchroConfigurationElement> devices =
ConfigurationManager.GetSection("NA5300ResolverSynchroDevices") as
DeviceConfigurationSection<NA5300ResolverSynchroConfigurationElement>;
Here's the exception text I'm getting:
An error occurred creating the configuration section handler for NA5300ResolverSynchroDevices: Could not load type 'InfrastructureModule.DeviceConfiguration.DeviceConfigurationSection<NA5300ResolverSynchroModule.NA5300ResolverSynchroConfigurationElement>' from assembly 'NA5300ResolverSynchroModule'.
I know that in C# generics are instantiated at runtime rather than at compile time (unlike C++). I do not yet know enough about generics to understand what assembly a runtime-generated type is considered to live in when the generic type and the instantiating type live in different assemblies. Above, I told the runtime to look for it in assembly NA5300ResolverSynchroModule. I've also tried telling it to look for it in assembly InfrastructureModule. Neither works.
I am attempting to use a genric type because I will have many config sections for which the corresponding ConfigurationSection-derived types will all be of the form shown above. I want to avoid code duplication.
Can anybody see why my approach is failing and how I can fix it?
Your problem is actually how you've referenced the generic type.
Instead of (shortened):
<section name="..."
type="InfrastructureModule.DeviceConfiguration.DeviceConfigurationSection
<NA5300ResolverSynchroModule.NA5300ResolverSynchroConfigurationElement>,
NA5300ResolverSynchroModule" />
Try
<section name="..."
type="InfrastructureModule.DeviceConfiguration.DeviceConfigurationSection`1[[NA5300ResolverSynchroModule.NA5300ResolverSynchroConfigurationElement, NA5300ResolverSynchroModule]],
NA5300ResolverSynchroModule" />
Note the `1[[...]] rather than <...> or <...> part for the generic type. The part inside the [[...]] can be a full type definition as well - like namespace.class,assembly,token.
The 1 is "generic type with one type parameter". If the type takes 2 "aka SomeType<T,V>", use2. Note that you should put "type, assembly" in the double square brackets, not just "type"
I believe my problem was rooted in the fact that the runtime-generated type I tried to map to a configuration section does not live in an assembly. So, I created a type that does live in an assmebly.
namespace NA5300ResolverSynchroModule
{
public class NA5300ResolverSynchroDeviceConfigurationSection :
DeviceConfigurationSection<NA5300ResolverSynchroConfigurationElement>
{
}
}
I can reference NA5300ResolverSynchroDeviceConfigurationSection just fine in App.config.

Categories