Friday, January 30, 2015

Explicit operator

According to MSDN, explicit operator keyword declares a user-defined type conversion operator that must be invoked with a cast. This is super handy if you are doing a lot of conversion/translation in your code.

Here is an converting between two separate classes, Person and Contact - which have translatable properties between them.

public class Contact {       
        public string Name { get; set; }
        public string PrimaryPhone { get; set; }
        public string PrimaryEmail { get; set; }

public class Person{
        public int PersonId { get; set; }        
        public string FirstName { get; set; } 
        public string LastName { get; set; }
        public string HomePhone { get; set; }
        public string HomeEmail { get; set; }
        public string WorkPhone { get; set; }
        public string WorkEmail { get; set; }
        public string CellPhone { get; set; }
Then, you can create an explicit conversion operator such as this:
public static explicit operator Contact (Person person)
    return new Contact 
          Name = person.FirstName + " " + person.LastName,
          PrimaryPhone = (String.IsNullOrEmpty(person.CellPhone) ? 
                  (String.IsNullOrEmpty(person.WorkPhone) ? 
                  person.HomePhone : person.WorkPhone) : person.CellPhone),
          PrimaryEmail = (String.IsNullOrEmpty(person.WorkEmail) ? person.HomeEmail : person.WorkEmail)
Here is how you would use it:
Contact contact = (Contact)person;
-- read more and comment ...

Tuesday, January 20, 2015

Enabling IIS Express to Accept External Request

I am doing a web development project where the web application needs to look good on all browsers (on Mac and PC - Win7 and Win8) and mobile devices (iPad, Windows Phone, Android, etc). So most of my testing can be done in my development machine - either by running an emulator from Visual Studio, or letting the browser (i.e. Safari) emulate iPad/iPhone resolution. This probably solved 95% of the issues and problems. But, then I still want to do a real device test - where I actually run the web application and browse to it from an iPad, or a Mac, from an Android phone, etc.

Of course - the ideal solution is just to put the web application somewhere on the internet (i.e. and let the testing begin. But unfortunately, I do not have the luxury of a QA or test site. So I just have to run it locally.

I can set it up on my IIS, but that seems to be a hassle - removing/stopping my current site running in IIS, setting up this new site, creating all the permissions, app pool, etc. Too much overhead for simple testing. Can I just hit F5, or run the IIS Express and make it accepting external request - so I can hit the site from outside of my dev machine? YES!

Allowing everyone to access http://mydevmachine:80

Open up an admin elevated command prompt and type in this command:
      netsh http add urlacl url=http://mydevmachine:80/ user=everyone

Open up firewall

Now, we need to open up the Windows Firewall for allow traffic to go through. In the same command prompt from above, type in this command:
      netsh firewall add portopening TCP 80 IISExpressWeb enable ALL

Configure IIS Express

Lastly, we need to add our machine name into the IIS Express configuration. You can find the config file located in C:\Users\\Documents\IISExpress\config. Open the file using any text editor and find the web application configuration. My web application is named "Domain.Site.Mvc" in this example.
<site name="Domain.Site.Mvc" id="4">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="c:\Projects\Domain.Site\Domain.Site.Mvc" />
        <binding protocol="http" bindingInformation="*:12555:localhost" />
        <binding protocol="http" bindingInformation="*:80:mydevmachine" />

Now, I can start my site on my dev machine and then browse to it from separate machines, Mac machine, phone, tablets, etc by just going to http://mydevmachine/. With the exception of an iPad. For whatever reason, iPad does not allow browsing to a local machine name - it has to be a fully qualified domain name. I will write a solution to this on a separate blog post.
-- read more and comment ...