DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > DynamicLocalhost NuGet Package for Windows Phone

DynamicLocalhost NuGet Package for Windows Phone

Matt Hidinger user avatar by
Matt Hidinger
·
Dec. 08, 11 · Mobile Zone · Interview
Like (0)
Save
Tweet
3.67K Views

Join the DZone community and get the full member experience.

Join For Free

Why?

Because I should be able to Get Latest and F5!

I think simplicity is a crucial measure of success in all software. I think being able to pull down the source code onto any developer machine and F5 is an important goal to strive for. Removing manual processes and streamlining the development workflow can yield huge productivity gains – especially when onboarding new developers. I believe databases should be provisioned and seeded automatically, all 3rd party libraries should be readily available in the repo, and any manual steps that must be memorized or documented should be avoided at all costs.

But this hasn’t been easy on a Windows Phone device

The aforementioned goals have been easily achievable in all of my projects except Windows Phone apps. A key component to this workflow is the magical variable known as localhost.

When I F5 one of my MVC apps the project launches a browser to http://localhost/MyProject.

When I F5 one of my Windows Phone apps in the emulator, it connects to my local IIS server at http://localhost/MyProject.

When I F5 one of my Winodws Phone apps on a real device, things go awry. Localhost on the phone does not point to my development box, naturally, it points back to itself. Thus the predicament.

Let’s spike out a theory

Last night I had an idea, and decided to spike it out. What if I could store the URL to my Local IIS web services in a simple .txt file inside the XAP package? Then I could read this file during AppLaunch and store it for later. This part was easy, now for the magic. What if I could come up with a PreBuild step in Visual Studio, which could modify said .txt file during compile, and replace it’s contents with the Environment.MachineName? After a little fumbling around with a new Console Project, I had it working.

NuGet, PowerShell, meet DynamicLocalhost

I’ve been a long-time fan of NuGet. I have seen some amazingly creative things done with it beyond just simple library dumps. People much smarter than me are using PowerShell to create impressive scaffolding frameworks. Inspired, I cracked open my Windows PowerShell and MSBuild books and got to work.

I ended up building the following:

  1. No assembly dependencies, only a single source file and a single .txt file get added to your project
  2. Created a MSBuild Task which will process the LocalHostname.txt file and fill it with the Environment.MachineName
  3. Modify the .csproj file to add my custom BeforeBuild target, which executes the Task above on each compile, thus ensuring the text file always has the correct machine name

Ok that’s great, but how do I use it?

First you have to install the package. Make sure NuGet is installed.

image

The rest is up to you, to use the DynamicLocalhost class however you see fit.

For me, in my WP7 projects I create a static ApplicationSettings class to use throughout the app. The comments below hopefully explain how it works. The only source code you ever check in says “localhost” – and will be dynamically replaced at compile-time to the actual developer’s machine name. This means that you are free to check in everything, get latest anytime you want, and never have to worry about someone accidently checking in their real machine’s URL, hampering your workflow.

public static class ApplicationSettings
{
    public static string WebServiceBaseUrl { get; private set; }
 
    static ApplicationSettings()
    {
#if DEBUG
        // Compiling this app on MY machine, WebServiceBaseUrl will become: http://MATT-PC/RealWorldWP7.Web/
        // The returned URL will be automatically determined on each compile
        // Therefore no issues checking it in and getting latest on any developer machine
        WebServiceBaseUrl = DynamicLocalhost.ReplaceLocalhost("http://localhost/RealWorldWP7.Web/");
#else
        WebServiceBaseUrl = "http://services.mydomain.com/v1/";
#endif
    }
}

Source Code, Feedback, Forking!

I would really, really love feedback on this. I scoured the internet trying to see if something similar already existed and turned up with nothing. If you find it useful, or have any suggestions, please be sure to let me know!

I’m also very new at PowerShell and would not be surprised if I wrote some of the worst .ps1 scripts ever. Any input by PowerShell guru’s would be most welcome.

Get the Source!

It’s using Mercurial, so fork it, and be sure to send me pull requests if you write something awesome!


Source: http://www.matthidinger.com/archive/2011/10/10/DynamicLocalhost-NuGet-Package-for-Windows-Phone.aspx

Windows Phone NuGet

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Major PostgreSQL Features You Should Know About
  • 5 Steps to Create a Successful Agile Release Plan
  • Python Class Attribute: Class Attribute vs. Instance Attribute
  • API Security Tools: What To Look For

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo