Marcos Placona Blog

Programming, technology and the taming of the web.

Category: .Net (page 1 of 2)

OSX Pro Tip for .NET Environment Variables

Reading time: 2 – 3 minutes

vs-mac-slide

I use environment variables in all my apps to make sure none of my secret keys end up in GitHub. In C#, if you want to get environment variables in your app you just need to use the Environment class and call the method GetEnvironmentVariable() passing the name of the variable you’ve already defined.

I have recently switched from building and running apps directly on the terminal and Visual Studio Code to using Visual Studio for Mac. I opened up an application I had already built and knew worked and tried it out. I started to get some weird errors where it would claim to not find my environment variables.

I went ahead and run my application from terminal and it worked as expected.

A bit of Googling and I landed on this StackOverflow thread. But here’s the gist of it:

OS-X GUI apps will not inherit your private/custom env vars that are defined via a shell (bash, zsh, etc…) if they are launched from Finder/Spotlight

The solution to that is to instead of starting Visual Studio for Mac from finder, you can start it from terminal and it will then inherit your environment variables. You can start it like this:

But remembering that will be hard, and you will end up tabbing through your terminal until you get to it every single time. So here’s an alias:

If you run that on your terminal, you can then just run vs afterwards, and Visual Studio for Mac will start inheriting your environment variables.

This will disappear every time you restart your terminal though, so add it to your .bash_profile or equivalent if you wanna make it permanent.

Multi-Line C# Strings

Reading time: 2 – 3 minutes

I’ve seen this question being asked on StackOverflow so many times I event thought about writing a bot to automatically reply to it.

The answers vary slightly according to the experience of each developer but the question is always the same. How do you create multi-line C# strings?

Assigning multi-line strings to a variable

Say you’re trying to assign an XML string to a variable and want your code to still look presentable. The correct way to do that would be as follows:

Notice all I had to do was add an @ before the speechmarks, and this gives multi-line capabilities to my string.

But what if I wanted to have dynamic values inside my string? Say values coming from variables for example.

Assigning dynamic multi-line strings to a variable

What most people will do here is make the assumption you can use concatenation to inject variables in by doing something like this.

That is a horrible way of doing this though. Every time you add a new variable, you need to use an @ again to tell the compiler you’re starting a new multi-line string.

To assign dynamic values to a multi-line string you can use interpolation by adding the variables with curly braces within the string and then using string.Format() to tell it to replace those with the values I provide.

So much more elegant right? My string remains the same but I can now replace values in it in compile time and just keep increasing the number within the curly braces.

Let’s kick it up a notch and say your string has attributes. We’d now be looking at adding speechmarks in each of them as such:

Unfortunately it would also mean your code is now broken and you will start getting something like CS1002: ; expected. To go around that just make sure you double the speechmarks wherever you need them within your string.

And that will make your code work again and best of all – look really neat!

New TeamCity agents the right way

Reading time: 2 – 2 minutes

TeamCity Logo

At work, I’m gradually moving our CI server from Hudson to TeamCity.

Nothing against Hudson really, but I feel that TeamCity is a much more robust CI Server when it comes to integrating with .Net. It allows you to publish artifacts from your builds, and has a killer integration between developers IDE’s and itself, which is amazingly helpful to help developrs make sure they’re not going to break the build… well before they break it.

But anyway, one thing that was slightly annoying me with TeamCity, is the fact that the build agents would often get disconnect, and all my builds would stay in a queue until I went and manually restarted the agents.

The “Build Agent Disconnected” quickly became very annoying, and by quickly looking up on Google, I found lots of people had the same issue, and while there were lots of responses or people claiming they found a solution to it, I never actually found anything of much use other than the screenshot this guy posted.

When you add build agents on TeamCity, you get the option of adding them as a windows service, or simply as an agent that runs with TeamCity. I had tried to add multiple build agents as windows services before, but for a very strange reason, I would always end up with only one agent no matter what I did. TeamCity’s documentation wasn’t much help to be honest, and I ended up figuring this out after a couple of hours of trial and error. So here’s how you do it properly.

Continue reading

Installing Visual Studio on a different drive

Reading time: 4 – 6 minutes

I’ve got a pro license of Visual Studio 2012, and decided to install it instead of my existing VS 2010 Express install.

Because I didn’t actually have anything very important on my current install, I decided to uninstall my current VS 2010. When I originally installed it, I made sure it got installed on my E:\ drive. To give you a bit of context on my setup, I’ve got an SSD as a boot drive (C:\) and a specific drive (E:\) which I use to install all my programs. I find it helps me backing up my stuff, as well as “separating concerns”, since my boot drive is only used for… well booting 🙂

I then set off about installing VS2012, and the first screen I was presented with was where you actually define where your install is going to go. So I just change the drive letter to be E:\ instead of C:\.

To my surprise, this is what I saw in the next screen:

VS2012 Install screen

VS2012 Installation screen

“No good!”, it still wants to use over 2Gb of my C:\ drive, even though I told it to only use my E:\ drive.

After a bit of Googling, it it turns out more people have had this same problem, and there’s even been a blog post on MSDN about it. While there’s been some conversation on the comments in the blog post aforementioned, none one really seems to have come up with a decent (or at least temporarily decent) solution to the problem.

So here’s a “temporarily decent” solution to this problem:

Simply create a SymLink form the folder where VS “wants” to be installed, to the folder where you want to install it. This way, you “trick” the installer to think it’s being installed on the system drive, but is is in reality installing it to the drive you defined on your symlink.

Think of it as a shortcut to a folder deeply nested within your file system. You can have it sitting on your desktop, but when you actually open it, you will find that the path to it is something like <letter>\my\deeply\nested\directory.

So in order to do that, all you will need to do is the following:

  1. Open command line as an administrator (you can type “cmd” and right click on it to run as admin)
  2. Enter mklink /J “C:\Program Files (x86)\Microsoft Visual Studio 11.0” “E:\Program Files (x86)\Microsoft Visual Studio 11.0”
    1. Where the J flag indicates you’re creating a directory junction (see more here)
    2. The first path is where VS is trying to install itself
    3. The second path is where you want it to be installed
  3. Carry on and let it install

You will now notice the whole of it (except for things it adds to the registry) will have been installed on your preferred drive.

Q & A

Q: Why are you calling your solution “temporarily decent”? It seems perfect.
A: It turns out that when you install any updates to VS, they will end up in your system drive, since VS updates don’t “understand” symlinks, and in fact remove them.

Q: And you only say that now? What do I do then?
A:A comment added to this answer on StackOverflow seem to imply that you can do the update, and then merge your files manually by copying them into your other drive. You will need to re-create the symlink after that, but considering you don’t get udpates every day, it’s probably worth.

Q: Will this be a problem for ever?
A: By the looks of it, yes! If you read the blog post I linked above, MS seems to think that’s the way to go now, and you should have bigger drives if you wanted to have to avoid this whole workaround.

Enterprise queuing applications

Reading time: 2 – 4 minutes

At work, we’ve decided to start using enterprise queuing applications for ease of communication between our ColdFusion and .Net projects.

For those who don’t know how queues work, if I had to summarize it I would say it’s like a database that stores all the messages sent from diverse systems (even in different clusters), and awaits until a consumer (queue subscriber)  picks them up.

Queues accept pretty much any kind of string you throw at them, so you could for example give it JSON or XML if you wanted to store anything a bit more complex than an ID for example.

You then write specific consumers that only listen to certain queues, and once they have received and acknowledged the message, they then move on to pick up the next item in the queue.

The queues we will be seeing in this post are FIFO type queues, which means the first item you chuck into the queue, will be theoretically the first to come out. I say “theoretically” because you could tinker with this and prioritise the order your queue serves stuff.

A simple queue with multiple consumers could be represented the following way:

Queue with two consumers

What is important to notice here, is that items will be picked up on a round-robin fashion, meaning no one item could be picked up by two consumers, and no one consumer would be “greedy” and pick up more items than the others.

To illustrate this, I will use an email queue as an example. So image the following scenario:

  • You have an emailing system, and all it does is… well send emails.
  • Your online store sends a confirmation email to every client after they have purchased an item. And this step is important, as it provides the customer with information about their purchase.
  • Your application does not need to know it sends emails, therefore all it needs to do is say: “Hey, someone’s made an order and I’ve processed it”

What the last point it trying to say, is that an application doesn’t necessarily need to have things that aren’t related to its main purpose (selling products in this case). Obviously this solution won’t be suitable for every single case, but in a similar occasion, it came to be really suitable at the company I currently work for.

Continue reading

Older posts