Posts Tagged ‘corgibytes’

Using log4net with IronRuby

Posted in Uncategorized on December 7th, 2009 by scott – Be the first to comment

Using log4net with IronRuby is something of a pain. This is for two reasons.

  1. log4net violates Microsoft’s API naming guidelines by naming the root namespace in the log4net assembly as ‘log4net’. A conforming name would look more like ‘Log4Net’.
  2. IronRuby ignores any namespaces that start with lowercase letter. It will flat out refuse to load them.

These two facts together lead to total suck, but I have found a work around. I wrote a wrapper class that invokes the log4net assembly via reflection. This lets you call log4net.Config.BasicConfigurator.Configure() so that log4net gets configured from the app.config file. The wrapper class also allows you to access named loggers and provides a way to output the log levels that are configured for the root logger.

Enjoy!

IronRuby and the Configuration (app.config or exe.config)

Posted in Uncategorized on December 4th, 2009 by scott – Be the first to comment

I was trying to write a quick little IronRuby application that talks to a third-party library that I am working with. I ran into some problems related to configuration files, and I thought I would share how I got around the problem.[2]

The library I am working with requires that some values exist in the application’s configuration file, which could be either the app.config file or the executable_name.exe.config file. But I have no way to specify these values, because IronRuby’s ir.exe[1] has it’s own configuration, ir.exe.config that sets up paths and other options for the Dynamic Language Runtime (DLR). Any application that you execute with IronRuby is run within the context of ir.exe, and so it inherits ir.exe‘s configuration.

I should mention I could have added the values directly to ir.exe.config, but I dismissed this solution as unacceptable. I am really a stubborn person.

During my extensive research into the issue I encountered several suggested solutions, but none of them worked. Most discussions that I came across ended with someone giving up and modifying ir.exe.config.

The .NET Framework provides no approved way to modify the configuration once it is loaded into memory. I imagine that this is due to security issues. You would not want malicious code to get access to the configuration file and change the values. My second of two attempts to solve this problem resulted in success.

First, I tried creating a new AppDomain with its own configuration. However, I was not able to use any IronRuby constructs to get code to execute within the context of the child AppDomain.

To do this I first tried creating a MarshalByRef descendant that contained the code requiring the configuration settings. However, the way IronRuby creates CLR versions of the Ruby types made this very difficult. It looks like the types are created in an in memory assembly, but I could not get a reference to that assembly that would let me load my custom type into a different AppDomain. I kept getting errors complaining that the assembly could not be found. After hours of trying and trying I gave up and decided to call AppDomain.DoCallBack instead.

Here I encountered an issue with IronRuby delegates. I created a proc with the code that I wanted to execute and passed it into the constructor for the delegate type that is expected by the DoCallBack method. However, I got a really strange error complaining about not being able to serialize the delegate into the new AppDomain. Strike two. At this point giving up is starting to look like a really good option.

Not knowing another way to solve the problem, I decided to hack my way to a solution. With my friendly companion, Reflector, I started deciphering the logic that reads configuration files into memory. I wanted to find out how to change the configuration file that the current AppDomain is using and then force the AppDomain to read from the new configuration file. The result is the ConfigurationSettingsHackery class. It uses reflection to dig into System.Configuration and change some key private members. After doing so, the AppDomain re-reads the configuration the next time that configuration information is requested.

I hope this helps someone. It would have really been nice to have this class two days ago. I should warn you, however, that this is a nasty, nasty hack. As such, it it most likely not work on the next version of the .NET Framework.

[1]: I am using IronRuby 0.9.2.
[2]: This discussion is also applicable to IronPython users that are trying to do the same thing, as it has the same issues and limitations.

Mono.Cecil and Type Forwarding

Posted in Uncategorized on November 23rd, 2009 by scott – 2 Comments

Just a quick note to help those that may be searching for the ability to use Mono.Cecil to create an assembly that forwards types to another assembly. I after trying several different ways to call the library to do what I wanted, I decided it was time to dive into the source and see what was going on. Well the answer to my frustrations was found after much searching. Take a peek at the source for Mono.Cecil.ReflectionWriter and search for TODO. You will find the VisitExternType method. It contains nothing but the comment, TODO. Oh, and the method is never called, so good luck trying to figure out how it is supposed to work.

I am going to try to get this to work with Microsoft’s CCI instead. I will report my findings in another post.

Importing an existing git repository into subversion

Posted in Uncategorized on November 12th, 2009 by scott – Be the first to comment

I been scouring the net for a way to take an existing local git repository and apply all of the commits to a subversion repository. I finally found the answer. I am going to rewrite the procedure here while I wait for my code to be commited.

Assume you have an existing git repository, and you are currently in that directory, run the following commands to link your git repository to the subversion repository.


  $ git svn init -s svn://my/svn/server
  $ git svn fetch

The result of the fetch command should display a series of revisions from the subversion repository.

Now run the following command and store the result somewhere.


  $ git show-ref trunk

This should yield a sha-1 hash for the remote repository.

Now we need to grab the hash for the local repository.


  $ git log --pretty=oneline master | tail -n1

Finally, we need to let git know that these two revisions should be “grafted” onto one another for that do the following.


  $ echo "<second value>  <first value>" >> .git/info/grafts

Running git log should reveal that the last commit from subversion now appears right before the first commit in your local repository. Perfect!

Now run the following command to push everything into the subversion repository.


  $ git svn dcommit

Sit back and watch the output scroll by. My commit is still running, even after typing this entire post. :)

Go, Go gadget Google!

Posted in Uncategorized on November 11th, 2009 by scott – Be the first to comment

Okay, so the title of this post needs some work, but I wanted to take a few moments to comment about the new programming language on the street today, Go.

Go was born out of one of Google’s famous 20% projects. I have been reading through the documentation on the project site, and I am starting to get a feel for the motivation behind the development of the language.

It appears that someone at Google was a really big fan of C. Such a big fan, that they designed a language with the same basic feel, but with some newer and improved syntax sugar.

With most of the sexy languages in the land being of the dynamic variety, it is interesting to see such an improvement in the static space. The Go language utilizes many features that are really popular in dynamic languages, but provides the advantages that come only as an after-thought with most dynamic languages.

It is going to be interesting to see how this language becomes adopted. I, for one, am not anxious to start using it. Mainly because I have been working with Ruby in my free time. (Ha! Free. right. More on that some other day.)

I am going to remember Go though for one particular use case. If I find myself unhappy with Ruby runtime performance, and I want to optimize by writing closer to the metal, then I am more likely to reach for Go than I am to reach for C or C++. Very interesting.

sudo, Ubuntu, and the PATH environment variable – a love story (of sorts)

Posted in Uncategorized on November 7th, 2009 by scott – 4 Comments

I just started setting up a Ubuntu Karmic Koala (9.10) server in the cloud, and I became very frustrated very quickly about the default behavior that is compiled into sudo. Since there is not much info laying around the net on how to solve this problem, I thought I would throw this post together. So if the big search engine in the sky brought you my way, then I hope this helps you.

Sudo on Ubuntu Karmic has been compiled with the –with-secure-path option. This causes sudo to ignore any changes to the path environment variable. And I do mean any changes. Changing the path in the user’s environment ala PATH=$PATH:/opt/other-bin sudo gem will not work. Neither will modifying the path variable in the /etc/environment file. And don’t try to modify the PATH in /etc/profile or /root/.profile or /root/.bashrc because none of those will work either.

If you want to see the path that sudo is using then take a peek at /usr/share/doc/sudo/OPTIONS. There you will see the exact path that was compiled into the sudo command.

This “secure path” can be modified. But before I tell you how, I should insert a word of caution. My research indicated that this was done for your protection. As with many things that are done for your protection, it is annoying as hell. But it evidently makes it harder for trojans to run commands as root. So make sure that you think twice before making changes to the “secure path” that sudo uses when it runs.

Thanks for patiently reading the disclaimer. Now for the juicy details. To modify sudo’s “secure path” you just need to add a line to the /etc/sudoers file. This file is best modified using the visudo command. So fire up visudo and add the following line.


  Defaults        secure_path=<your new path>

I highly recommend that you start with the value that sudo was compiled with and then append to it.

I hope that helps you.

It would have been really nice if this was documented better somewhere. I was only able to piece this solution together after reading a lot of confusing forum posts and after several head-scratching reads of the sudo man page.

Twitter Translated from Geek Into English

Posted in corgibytes, marketing on August 18th, 2009 by andrea – 1 Comment

twitter_logoTwo years ago I posted how twitter was a “complete waste of my time”. No one I knew used it. Searching was clunky. They didn’t even have a model to generate revenue (still don’t).

But the tipping point has arrived. It caught my eye in the form of an article from ClickZ titled “Twitter Surpasses Facebook as Top Link in Email.” Really? What! That silly little tweeting thing actually has value?

Indeed. That same article linked to a report that linked deep social engagement to revenue and profit. A big part of that “deep” social engagement is Twitter. All 4 case studies in the report (Starbucks, Toyota, SAP, and Dell) use Twitter as a key piece of their media strategy.

So it’s clear — there’s a benefit. But now what? What are some ways the average Joe/Jane can use Twitter…without feeling overwhelmed? As a self-professed “non-techie”, here are some ideas and resources for you, along with answers questions you probably have.

How does Twitter work?
You have 140 characters to answer the question “What are you doing right now?” Your response is called a “tweet.” If you provide interesting content, people will want to “follow” you. This means that whatever you post shows up on their homepage. Getting followers is a good thing.

What makes my content interesting?
Genuine content = interesting content. Being authentic = intriguing. If you’re too self promotional (or heaven forbid, a spammer) no one will want to follow you. In fact, you’ll be shunned. I find interesting content to be: 1) a link to an article or blog post (tinyurl.com will be your friend) , or 2) a peek into your expertise or personal life

Why do people care what I’m doing?
Two reasons. 1) Connection. People gravitate towards people who are like minded. Having stuff in common is the first step towards forming a connection.  2) Curiosity. It’s like when you’re traveling through a neighborhood and people have their windows open. You look. Not because it’s creepy but because you’re curious. Plus, the open window invites you. It gives you permission to glance, but not stalk.

Isn’t it just a bunch of nonsense and noise?
Nope. It’s a way to meet people who you might actually like, enjoy, do business with, get a job from, be entertained by, or gather information from.

Here’s an example:

My brother Brian (@GouletPens) makes Pens for a living. He went to a directory of twitter users called wefollow.com and typed “pens” to find people who were also passionate about pens. He started following @Dowdyism, a “pen addict extraordinaire”. @Dowdyism noticed Brian and started following him.

Now, you would think that these two pen fanatics would bond over their writing instrument obsession, but that’s just what got them in the door. They corresponded over Wii. Here are the screenshots:

Brian posted this to Twitter

Brian posted this to Twitter

and then, a few minutes later…

...and dowdyism (a potenital influencer for Brian) responded

...and dowdyism (a potenital influencer for Brian) responded

It’s a connection that probably wouldn’t have occurred if Brian had used DM, email, TV, radio, or another traditional marketing channel. But now, looks like he’s in.

What are all those crazy RT, @, and # things all over the page?

RT – This is a Retweet. It’s something that someone found on another twitter page. Let’s say Amy posts a really useful article. Ben sees it and knows his followers would really enjoy reading it, too. Ben copies the tweet, but inserts RT @Amy (or whatever her Twitter name is). Why do this? 1) It’s polite. It also shows transparency (which is a very good thing in social media). 2) It let’s Amy know her tweet was helpful to you so she is a) more likely to tweet more about that and b) sees who her biggest fans are.

@ – This links a twitter screen name.  If you start your post with @soandso, the person who has the screen name “soandso” will see your message, and it will show up as a tweet on your home page (but not on all your followers). You can also send someone a direct message (if you follow each other), which is kind of like a shortened form of email.

# - This is a hash tag. It’s sort of like a keyword so you can sort by categories. This helps you organize and find information more easily. According to Wild Apricot,

Hashtag etiquette is still evolving, so let good social manners be your guide. It is a rare “tweet” that deserves a hashtag, so tag only those updates that you feel will add significant value to the conversation. One hashtag is best — two are permissable — but three hashtags seem to be the absolute maximum, and risk raising the ire of the community. Tag sparingly, and with careful discretion.

How do I find people to follow?

Here are several ways:

  1. When you sign up for Twitter, it can automatically port your contacts from Gmail, Yahoo! or AOL. Mighty convenient and worth taking the extra 20 seconds during setup.
  2. You can also click “Find People” in the top right corner when you’re signed into twitter.
  3. Check out a twitter directory. There are several. If you need to find some, just google “twitter directory”. The best (to date) is wefollow.com.
  4. Click on who’s following you — right under your picture in the top right of the sidebar. Is there someone interesting?
  5. Click the @yourscreenname link just below that. This will show you tweeters who have mentioned you in one of their tweets.

Yikes, people I don’t know are following me….what should I do?

Step 1: Don’t freak out. You don’t have to “open the window” any more than you’re comfortable. If you don’t feel okay sharing something on Twitter, then don’t. It’s that simple.

Step 2: Remember your role. You’re publishing and sharing content for people to read. If you wrote for a magazine, people who you didn’t know would read your article and maybe write a letter to connect with you. Twitter lets you do the same thing. You publish content that you find valuable, and people who find it interesting read it, subscribe so they can read more, and might even contact you to comment on it.

Step 3: Adjust your privacy settings. If you don’t want people who you don’t know to follow you, go to Settings and click “Protect My Tweets”.

Step 4: Block spammers. If someone is spamming you, or you don’t want them to follow you, block them. Show ‘em who’s boss.

How often should I tweet?
Tweet when you have something you want to share or say. Don’t just tweet to hear yourself type. Remember, this about connecting with people, not shouting about how great you are. You can ask a question, link to an article, or type a musing…it’s up to you. Expert tweeters post anywhere from 1 – 20 times per day. More than that, and you’ll start clogging your followers feeds. That annoys them.

Resources:

Here are some places I went to learn what I posted here  (thanks Sean, Carmen & Marvin for helping me find these!):

http://business.twitter.com/twitter101

http://www.businesspundit.com/10-essential-twitter-tools-for-business/

http://www.cio.com/article/492019/Twitter_Bible_Everything_You_Need_To_Know_About_Twitter

http://followontwitterlists.com/

Happy tweeting!

Oh, you can follow me if you’d like. @andreagoulet

Working with C# Anonymous objects

Posted in Uncategorized on June 8th, 2009 by scott – Be the first to comment

Anonymous objects in C# are very handy, especially given the way they are supported by the ASP.NET MVC framework.

I recently ran into a case where I wanted to interact with an anonymous object. Specifically, I was testing the data that I provided a JsonResult. I handed the JsonResult a pretty complicated anonymous object with several layers of nesting. This is a great use of anonymous objects because in code they look a lot like JSON. So, how do I make sure that the JsonResult is getting the correct data? The answer is reflection. But like with all things, there is a hard way and an easy way.

First the hard way.

var example = new {
  stringData = "string data",
  integerData = 12,
  booleanData = true
}

Given the block of code above, if we want to retrieve one of the values from the example instance we have to do the following.

string value = (string) example.GetType().
  InvokeMember(
    "stringData",
    BindingFlags.GetProperty,
    null,
    example,
    new object[] { });

Replace “stringData” with the name of any of the fields and there you go. The trouble is that this block of code is seven kinds of ugly (yes, I counted) and it is not the kind of thing that you want to type over and over. Wouldn’t it be nice if there was an easier way?

What if we use the following extension class. Also available as a gist on my github account.

static class ObjectExtensions
{
    public static T Property<t>(this object target, string name)
    {
        return (T)target.GetType().InvokeMember(
            name,
            BindingFlags.GetProperty,
            null,
            target,
            new object[] { });
    }
}

Now we can access fields from our sample class by writing the following code.

string value = example.Property<string>("stringData");
int otherValue = example.Property<int>("integerData");
bool yetAnother = example.Property<bool>("booleanData");

I think that this looks much better. I hope this helps you as much as it has helped me.

Generating an ASP.NET MVC Menu from a SiteMap

Posted in Uncategorized on May 27th, 2009 by scott – Be the first to comment

I needed an MVC helper method that generated the same markup that the WebForms Menu control does. I am not sure that it is 100% complete, but you can take a look at my first cut. I welcome any improvements or suggestions.

Storing Cucumber scenarios inside TFS

Posted in Uncategorized on April 23rd, 2009 by scott – Be the first to comment

Ever since seeing it demo at Agile 2008, I have fallen in love with cucumber. Yesterday, I posted the source code for CucumberTFS. Read on for more information about what it does.

I have been trying to introduce some Behavior Driven Development to the project that I am working on. We recently received a large batch of requirements/stories. Our goal was to develop a better way to communicate functionality with our testing team. I suggested using the cucumber given, when, then format to describe all of our scenarios.

We stored these scenarios in our Team Foundation Server (TFS) instance. This lets us track code changes against the scenarios, assign them to different people and anything else that can be done with a TFS work item. I got the idea to retrieve the scenarios from TFS and format them so that they could be run through cucumber. Enter CucumberTFS.

CucumberTFS’s first iteration attempted to wrap the call to cucumber directly. Given the problems I had with redirecting cucumber’s output so that it appeared to come from CucumberTFS, I decided to modify the tool to just generate a single feature file that contains the contents of all the scenarios in TFS.

The tool still needs some work before it can be used by the masses. I want to create a binary release with an installer, for example. And there needs to be more control over the name of the file that is generated and more control over the set of TFS work items that are retrieved.

So, if you are using TFS and want to integrate it with cucumber, then check out CucumberTFS. Head over to github, fork the project and make your own changes.