Thursday, June 9, 2011

MongoDB C# Driver, Query multiple tags that are required

Tried hard to find the solution to querying multiple tags through the MongoDB C# driver.
Many of the solutions that I found were about using OR to put together the tags but I needed to AND the tags. I.e. all tags supplied were required to be present on in the result.
The solution was quite simple actually. Just my mind that thought it would require some special solution.
string[] tags = new string[] {"foo", "bar"};
var sort = SortBy.Descending("created");
var query = MongoDB.Driver.Builders.Query.All("tags", BsonArray.Create(tags));
var dbPages = pages.Find(query)
.SetSortOrder(sort)
.SetLimit(limit)
.SetSkip(offset);

  1. Create a string array with the tags that you want to query with
  2. Add sorting if needed
  3. Call MongoDB.Driver.Builders.Query to build your query
  4. Use find on your MongoCollection together with your query.
  5. Add Limit and Offset if needed.
Enjoy : )

SGEN: Could not load file or assembly 'xxx' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)

Always nice to see our old friend HRESULT in an error message.

Ran into this today when converting some .NET3.5 projects to .NET4.0 and at the same time upgrading some 3rd party libraries.

Seems that downloaded files get a blocked attribute set on them that prevents the usage of them. In Visual Studio 2010, the HRESULT: 0x80131515 is shown when linking to dll’s that are blocked.

To come around this, open Windows Explorer, navigate to the folder hosting the dll. Right click on it and select properties. There should be a Unblock button in the dialog window. Click on it and SGEN should stop complaining.

Hope this helps : )

Tuesday, December 14, 2010

Decrypting SSL traffic with Wireshark: "ssl_load_key: can't import pem data"


One reason for the "ssl_load_key: can't import pem data" can be:
Open your PEM Key file.
It should read
-----BEGIN RSA PRIVATE KEY-----
Base64 encoded key
-----END RSA PRIVATE KEY-----
Occasionally you will end up with a key file in the following format, hence the "ssl_load_key: can't import pem data" error,
-----BEGIN PRIVATE KEY-----
Bse64 encoded key
-----END PRIVATE KEY-----
To convert it to the correct format that can be read by Wireshark you will need OpenSSL,
Enter the following commands into a console window:
> openssl pkcs8 -topk8 -in key.pem -out temp.pem
Enter a temporary password when prompted
And then to RSA format:
>openssl rsa -in temp.pem -out rsa.pem
Enter the temporary password from previous step when prompted.
Use rsa.pem in Wireshark