ContentPage constructor called before AppStart - xamarin.forms

I thought AppStart would be a good place to put some app initialization, particularly for HttpClient, so that it would execute before any app views were created.
However, in the debugger, I see that my view ContentPage constructor is called before AppStart.
Where should I be doing global initialization, such as this?

Related

Prism Xamarin dependency from app.xaml.cs and accessing to parent

I have a few question related to Xamarin Prism dependency, could you please help me?
In the App.xmal.cs I've registered a service called UserService
protected override void RegisterTypes()
{
Container.RegisterType<IUserService, UserService>(); ....
And this works perfectly via ViewModels as in numerous examples on the Internet.
But in my UC on application load I need to call a method from UserService to check some login details.
At the moment on OnInitialized I redirect a user to some dummy page in order to use dependency instanced UserService.
Is it possible to get an instance of UserService directly in the App.xaml.cs via Prism dependency injection (outside of a model)
Rather easy:
Container.Resolve<IUserservice>();
...not before the registration, of course...
...and not nice, too, try not to use the container directly, but in this case, it's your only option.

Synclock in Module Constructor?

Coming from Java where Static Block are immediately called. In VB.NET (ASP.NET) a Module Constructor isn't called until the first method is called. So, this begs the question, if I'm performing initialization within my Module's constructor do I need to wrap it in a Synclock?
Modules are a VB.NET programming nicety. Once compiled, they are the same as C# static classes, therefore a module constructor is the same as a C# static constructor. A C# static constructor is presumably exactly the same as a static block in Java. The documentation states that a C# static constructor is never executed more than once, so that would suggest that no synchronisation is necessary.

MVP Pattern : Separating the database dependency from the presenter?

Whenever I try to actually unit test a presenter and a mocked view, I end up running into too many database dependencies
public EditAccount(IAccountEditPage _view, ISession _session, IResponse _response)
{
}
public void view_SaveUser()
{
//Class that takes the view's data and persists it to DB
}
Obviously I can't write unit tests for this presenter because I have a concretion of using my model class that has a strong database dependency.
How am I supposed to removed the dependency on the database without constructor injecting every class that touches the database in my presenter? I don't want to do this every time in every view I have.
I'm using moq, if it helps.
Edit : Also I should mention that the code in "view_SaveUser" is very lean and isn't direct database access or anything like that. It's usually only a few lines. I'm not overstepping the scope of the presenter, AFAIK.
If you don't want to inject the instances on the constructor another option you have is using a setter injection using a IoC framework as Spring.Net or Castle Windsor to inject the dependencies.
Doing this, you would only need to specify on the framework configuration which classes are used for real code and for test project, dependencies would be automatically injected and you would avoid having to use the contructor.

How do I access my public Global.asx.cs property from a business class?

I have a property on my Global.asax.cs class that I need to access from a business class, i.e. using HttpContext.Current.
How do I do this?
Global.asax.cs (in a web project)
public partial class Global : System.Web.HttpApplication
{
public static ProxyGenerator Generator = new ProxyGenerator();
Business class (in a separate business project)
var generator = ((Sei.Osp.Web.Global)HttpContext.Current.ApplicationInstance)
This obviously doesn't work and I don't want to reference the whole web project in the business project as it will create a circular reference (the business project is already referenced in the web project)
UPDATE:
To clarify - the property I'm creating holds an instance of the Castle Dynamic Proxy Generator class. I've read that you shouldn't just create this all over the place.
So I thought I'd create it in my Global.asax.cs and then just use that instance wherever I need to create a proxy class (I'm using it to do AOP)
Is there a better way of doing this?
A better technique would be to create a class (possibly static) with the public static property you want, then reference it from both Global.asax.cs and your business class.
The class could be in your business project, or in a separate project referenced by both your business project and the web project.
Your business classes need to be independent of your web site. You need to reevaluate your reasons for doing it this way. Either the business class doesn't really need access to the property, or the property doesn't need to be in global.asax.

How to create a System.Web.UI.Page object in a winform application?

I have a class which inherits from the System.Web.UI.Page class, like mysystem.
When I try to create an instance of mysystem in a winform appplication it throws an HttpException:
Session state can only be used when
enableSessionState is set to true,
either in a configuration file or in
the Page directive. Please also make
sure that
System.Web.SessionStateModule or a
custom session state module is
included in the
<configuration>\<system.web>\<httpModules>
section in the application
configuration.
I then googled this problem and try to enable the session state, but all are not affected.
So, can anybody tell me how to solve this case?
Any class that inherits from System.Web.UI.Page will attempt to utilise parts of the asp.net runtime when it's instantiated, so when it's used in a winforms application, it just won't work.
If there are methods that you need inside this class from another DLL, I'd suggest refactoring them into a separate class as:
If you want to use them in a winforms app, they're almost certainly not specific to the page so should be shared anyway
There's no good reason to try and use the asp.net runtime (other than, perhaps, caching) inside a winforms application

Resources