VB Object becomes nothing randomly - asp.net-mvc

We were getting a NullReferenceException from line 135 until I added the if statement on line 134.
It shouldn’t have been necessary because of the check on line 124.
I couldn’t reproduce it locally. But it happened every time on live for certain scenarios. This extension method is called from the SearchResultsViewModel constructor, but I wouldn’t think that would matter.
Am I missing something obvious?
This is how I call the extension method
ParseParentCategory(SearchKey)
This is how the SearchResultsViewModel is defined
Public Class SearchResultsViewModel
Inherits ViewModelBase
Public ReadOnly _ConstructedProperly As Boolean
Public Property SearchKey As String
Public Property ParentCategoryId As Integer = -1
Public Property ParentCategoryName As String
Public Sub New(Request As HttpRequestBase, pPartiallyPopulatedStupidInputModel As SearchResultsViewModel, pSearchBy As IProductsSearch.SearchBy)

Related

e4 dependency reinjection order: field vs. method

I was quite surprised to see that there is no deterministic behavior for the order in which objects get reinjected.
public class Test {
#Inject private Boolean testBool;
#Inject
public void checkNewObject(Boolean testBoolNew) {
if (!testBoolNew.equals(this.testBool)) {
System.out.println("Out of sync!");
} else {
System.out.println("In sync!");
}
}
}
And this is how I use the class:
context.set(Boolean.class, new Boolean(true));
Test test = ContextInjectionFactory.make(Test.class, context);
context.set(Boolean.class, new Boolean(false));
So, sometimes I get the output:
In sync!
In sync!
And sometimes I get:
In sync!
Out of sync!
Is this really non deterministic or am I just overseeing something?
The documentation clearly states that the injection order should be:
Constructor injection: the public or protected constructor annotated with #Inject with the greatest number of resolvable arguments is selected
Field injection: values are injected into fields annotated with #Inject and that have a satisfying type
Method injection: values are injected into methods annotated with #Inject and that have satisfying arguments
See: https://wiki.eclipse.org/Eclipse4/RCP/Dependency_Injection#Injection_Order
I'm not sure, why this doesn't work as expected in your case.
How is equals() implemented in MyContent?
Is MyContent annotated with #Creatable and or #Singleton?
As a side note: Is this a practical or just an academic problem? Why is it necessary to inject the same instance into a field and into a method on the same target-instance? If you want to have a field variable to cache the value, you can set this from the method.
If you feel this is a bug, please file it here: https://bugs.eclipse.org/bugs/enter_bug.cgi?product=Platform

MVC 5 Identity 2 - customer UserStore methods not being called

I am trying to implement a custom UserStore for my MVC 5 application so that I can delegate the database commands to my existing database project.
The tables in my database use integer keys, so I created a custom UserManager that inherits UserManager<User, int> instead of just UserManager<User>. For that reason, I also created a custom UserStore that implements IUserPasswordStore<User, int> instead of just IUserPasswordStore<User>.
Briefly:
public class CustomUserManager : UserManager<User, int>
{
public CustomUserManager(CustomUserStore store) : base(store)
{
}
}
public class CustomUserStore : IUserPasswordStore<User, int>
{
private readonly DbContext _db;
public CustomUserStore(DbContext db)
{
_db = db;
}
public Task<User> FindByIdAsync(int userId)
{
return _db.users.SingleOrDefaultAsync(u => u.Id == userId);
}
public Task UpdateAsync(User user)
{
_db.Entry(user).State = System.Data.Entity.EntityState.Modified;
return _db.SaveChangesAsync();
}
}
Now, it seems that when I call UpdateAsync on CustomUserManager, it does not go through my custom CustomUserStore, but rather through some default implementation or something. The problem is evident because CustomUserManager.UpdateAsync() returns Task<IdentityResult>, while my implementation in CustomUserStore returns just Task. Hence there are no errors or anything, but the method is not being called. I think IUserPasswordStore<User, int> should have some method UpdateAsync that returns Task<IdentityResult> but it doesn't...
On the other hand, the FindByIdAsync method does work fine and calls the method in my CustomUserStore.
I am quite sure the problem is due to inheriting the classes with the custom key type (int in my case). Any example of a custom UserStore I can find online just uses the string keys and does not inherit IUserStore<User, int> but just IUserStore<User>.
However I can't figure out my problem. Perhaps I can just override all the methods in my CustomUserManager but that seems like a work-around rather than a solution. Is this a bug in the framework perhaps, I think the custom key types is still relatively new or even in alpha?
I know that this is an old question but I struggled yesterday with the same issue and after some time spent on it, I decided to take a look in the Identity source code and what I've found, almost made me smashed my head against the wall...
Simple, if you call UserManager.UpdateAsync, in the base class you will see that before the UserStore.UpdateAsync there is a validation that takes place, which, in my case, failed (doesn't matter the reason). So, please, be so kind and check in your class (controller class) the result of UpdateAsync which is an IdentityResult and more then sure the Succes result is false.
Evdin

How do I tell MEF to instantiate a single object for a given type per application?

Lets say I have a class called FooController in which I have a property called Bar of type IBar (interface). I need to initialize Bar via MEF. However I need MEF to create only one instance of IBar type for the duration of the application (despite multiple calls to initialize it due to multiple requests) and make it available to all requests concurrently. Note that IBar implementations can be assumed thread safe.
i.e.
public interface IBar
{
string Method();
}
[Export(typeof(IBar))]
public class MyBar: IBar
{
public string dateTimeCreated;
public MyBar()
{
System.Threading.Thread.Sleep(1000);
dateTimeCreated = DateTime.Now.ToLongTimeString() + " ";
}
public string Method()
{
return dateTimeCreated;
}
}
public class FooController : ApiController
{
[Import(typeof(IBar), RequiredCreationPolicy = CreationPolicy.Shared)]
public IBar Bar { get; set; }
public FooController()
{
//Assume CompositionContainer.ComposeParts call here
}
public string Get()
{
return Bar.Method();
}
}
The problem is each time I call Get() on FooController, the returned time value changes. This means the MyBar object is being reinstantiated for each call. I basically need it to return the same value meaning I need to tell MEF to create only one instance of IBar in my application despite multiple requests.
Thanks in advance.
You need to specify the PartCreationPolicy attribute on your MyBar export. Like this:
[Export(typeof(IBar))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class MyBar : IBar
{
// ...
}
That also means that you don't need to specify the creation policy on your import:
[Import]
public IBar Bar { get; set; }
The ASP.NET MVC integration of MEF interprets CreationPolicy.Any and CreationPolicy.Shared as single instance per HTTP request. You need to apply the ApplicationShared attribute to the part to share it between HTTP requests.
Update:
The ApplicationSharedAttribute can be found in the System.ComponentModel.Composition.Web.Mvc assembly. Unfortunately this is not distributed with Framework 4.5. It can be found at the Using MEF with ASP.NET MVC 3 Sample Code example in the lib folder. The drawback is that you will have to reference the composition assemblies found it that sample and not the latest ones.
If you do not want to do that then start with this very simple approach:
Add a CompositionContainer in your MvcApplication class as a public property.
On the MvcApplication constructor create the container and add some catalogs.
On the controller get the application from the HttpContext and use one of the GetExport/GetExportedValue/GetExportedValues methods of the CompositionContainer. No need to call ComposeParts on the container.
There are a lot of other approaches that are more elaborate but this should get you started.

Map string to enum with Automapper

My problem is hydrating a Viewmodel from a Linq2Sql object that has been returned from the database. We have done this in a few areas and have a nice layered pattern worked up for it but the latest item calls for some enums to be used and this has caused headaches all round. Currently we pull back from the database then use Automapper to hydrate (or flatten) into our Viewmodels but having the enums in the model seems to be causing issues with Automapper. I've tried to create custom resovlers which have sufficed for all my other mapping requirements but it doesn't work in this instance.
A sample of the code looks like:
public class CustomerBillingTabView{
public string PaymentMethod {get; set;}
...other details
}
public class BillingViewModel{
public PaymentMethodType PaymentMethod {get; set;}
...other details
}
public enum PaymentMethodType {
Invoice, DirectDebit, CreditCard, Other
}
public class PaymentMethodTypeResolver : ValueResolver<CustomerBillingTabView, PaymentMethodType>
{
protected override PaymentMethodType ResolveCore(CustomerBillingTabView source)
{
if (string.IsNullOrWhiteSpace(source.PaymentMethod))
{
source.PaymentMethod = source.PaymentMethod.Replace(" ", "");
return (PaymentMethodType)Enum.Parse(typeof(PaymentMethodType), source.PaymentMethod, true);
}
return PaymentMethodType.Other;
}
}
CreateMap<CustomerBillingTabView, CustomerBillingViewModel>()
.ForMember(c => c.CollectionMethod, opt => opt.ResolveUsing<PaymentMethodTypeResolver>())
I get the following error
[ArgumentException: Type provided must be an Enum.
Parameter name: enumType]
System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult) +9626766
System.Enum.Parse(Type enumType, String value, Boolean ignoreCase) +80
AutoMapper.Mappers.EnumMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) +231
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +720
I'd like to stick with Automapper for all of our mapping actions but I've seen a lot of people say that it doesn't do this type of mappings so I'm starting to wonder if I'm using it in the wrong way? Also, I've seen a few mentions of ValueInjecter - is this an alternative to Automapper, or will it be useful to just plug the holes in Automapper for the hydration of models and use Automapper for flattening?
Yes I could just use a string in my ViewModel, but I'm not a fan of magic strings, and this particular item is used by helpers to perform some logic in a number of places.
This is an issue with the AutoMapper documentation. If you download the AutoMapper source there are examples in there. The code you want will look like this:
public class PaymentMethodTypeResolver : ValueResolver<CustomerBillingTabView, PaymentMethodType>
{
protected override PaymentMethodType ResolveCore(CustomerBillingTabView source)
{
string paymentMethod = source.Context.SourceValue as string;
if (string.IsNullOrWhiteSpace(paymentMethod))
{
paymentMethod = paymentMethod.Replace(" ", "");
return source.New((PaymentMethodType)Enum.Parse(typeof(PaymentMethodType), paymentMethod, true));
}
return source.New(PaymentMethodType.Other);
}
}
here's a solution with the ValueInjecter:
since you already solved the problem I'm just going to point you to something similar:
AutoMapper strings to enum descriptions
in this question the requirements were a bit more than just doing from string to enum, but it includes this conversion also
about the ValueInjecter being an alternative: yes, it does stuff more generic no configuration for every little thing required, and build whatever convention you can imagine

Dependency injection into custom ViewPage generates weird error

i'm trying to inject stuff into a custom ViewPage (ModelViewPage, from MvcContrib)
public class ValidatedModelViewPage<T> : ModelViewPage<T> where T : class
{
public ValidatedModelViewPage(IEnumerable<IBehavior<IMemberElement>> elementBehaviors)
: base(elementBehaviors.ToArray()) { }
}
and my Autofac registrations look like this:
builder.RegisterCollection<IBehavior<IMemberElement>>().As<IEnumerable<IBehavior<IMemberElement>>>();
builder.Register<NotNullBehavior>().MemberOf<IEnumerable<IBehavior<IMemberElement>>>();
builder.Register<StringLenghBehavior>().MemberOf<IEnumerable<IBehavior<IMemberElement>>>();
builder.RegisterGeneric(typeof(ValidatedModelViewPage<>));
but i get this error when i try to access a view:
Compiler Error Message: CS1729: 'UKFS.Web.Views.ValidatedModelViewPage<UKFS.Data.Entities.Skadeanmälan>' does not contain a constructor that takes '0' arguments
Source Error:
Line 194: private static object #__fileDependencies;
Line 195:
Line 196: [System.Diagnostics.DebuggerNonUserCodeAttribute()]
Line 197: public views_skadeanmälan_edit_aspx() {
Line 198: string[] dependencies;
Source File: c:\Users\Carl\AppData\Local\Temp\Temporary ASP.NET Files\root\be9ddc15\a84d5058\App_Web_edit.aspx.b2d4184a.thgwih90.0.cs Line: 196
i were clueless, but then i looked at App_Web_edit.aspx.b2d4184a.thgwih90.0.cs and found this:
Line 190: public class views_skadeanmälan_edit_aspx : UKFS.Web.Views.ValidatedModelViewPage<Skadeanmälan>, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler {
Line 191:
Line 192: private static bool #__initialized;
Line 193:
Line 194: private static object #__fileDependencies;
Line 195:
Line 196: [System.Diagnostics.DebuggerNonUserCodeAttribute()]
Line 197: public views_skadeanmälan_edit_aspx() {
of course, the generated class views_skadeanmälan_edit_aspx inheritates from my UKFS.Web.Views.ValidatedModelViewPage, and when it tries to instance it with the default construct.. so can you solve it?
You won't be able to constructor inject into ViewPages because the aspx compiler generates an empty ctor and as it derives from your base-class, your base-class must also have a empty ctor.
I'd look for property injection instead, otherwise try to accomplish the following:
Find the place where viewpages are instantiated, and get the Autofact there to instantiate the page
get the aspx compiler to not generate the empty ctor
I don't know how to do those things, so I'd aim for property injection instead

Resources