Sunday, January 20, 2013

My blog has a new home at baulig.net

This weekend, I signed up for AWS and create an EC2 micro instance running Wordpress.  After playing around with it, I started to really like Wordpress because of its great plugin system - especially Crayon Syntax Highlighter.

So I got myself a domain name from GoDaddy and moved my blog to its new home at http://baulig.net/.

Crayon Syntax Highlighter makes it so much easier to quickly cut&paste some C# or XML into a blog post, without worrying about any HTML escaping or anything like that.  And it also looks much better - for instance, compare my latest WCF Config post on the old and the new blog.

Saturday, January 12, 2013

WCF Configuration without Configuration

Before Christmas, I worked on automatically creating WCF configuration from a WSDL document.

I got until the point where I can successfully create a ServiceEndpoint and its Bindig from the WSDL (ServiceContractGenerator.cs:843 in my baulig/work-wsdl-config2), but run into several problems when creating the .config file.

The first issue I ran into was merging the new configuration items that were generated from the WSDL into an existing app.config file. The main problem is distinguishing elements that the user manually put into the app.config, new items from the WSDL file and items from the WSDL file that were previously added automatically (running "Update Service Reference" twice in MonoDevelop shouldn't add configuration twice). Visual Studio uses the configuration.svcinfo / configuration91.svcinfo files to cache the elements that it added to the app.config, but their format is undocumented (it's XML, but there's a checksum element, I've no idea how to generate that and didn't find any information on the internet).

The second big issue is automatically configuring a WCF client without System.Configuration, so it can be used in MonoTouch.

Microsoft uses some .xml inside a .zip file for Silverlight (but I also couldn't find any specification for the format) and Mono is using the SilverlightClientConfigLoader class to parse an file. But this class is incomplete, only handles the very basic cases and doesn't provide any useful error reporting to the user.

And the third big issue is that our System.Configuration implementation is buggy and the many of the System.ServiceModel.Configuration APIs unimplemented.

Requirements

So I was thinking about a solution which satisfies the following requirements:
  1. Does not use Reflection or System.Configuration, so we can use it for the mobile platform.
  2. Allows automatic configuration of a WCF client from a WSDL document - so you simply do "Add Service Reference" in MonoDevelop and it sets you up just like Visual Studio does, without any manual editing of configuration files.
  3. Is at least somewhat compatible with the app.config format.

The Solution

The solution I finally came up with was a custom XML file format.

It is very similar to the section in the app.config file, but not identical. The main difference is that the name of each XML element is unique and doesn't depend on the context it's used in.

For instance, this is the app.config file format:

<bindings>
  <basichttpbinding>
    <binding allowcookies="True" closetimeout="00:15:00" />
  </basichttpbinding>
  <custombinding>
    <binding name="myCustomBinding" />
  </custombinding>
</bindings>
As you can see, the <binding> element has a different meaning depending on which section it's used in. I'm generating this:
<bindings>
  <basichttpbinding allowcookies="true" closetimeout="00:15:00">
  <custombinding name="myCustomBinding">
    <elements>
      <textmessageencoding messageversion="Soap12WSAddressingAugust2004" />
      <httptransport />
    </elements>
  </custombinding>
</bindings>
So the attribute names and their values are mostly the same, but I'm using different names for the elements to make them unique.

In addition to a reader and writer for this XML format, I'm also generating an XML Schema for it.

Basic Usage

The module has three modes of operation:
  1. Generating an XML document from a Collection. This would normally be done automatically from a tool which uses WsdlImporter.ImportAllEndpoints() to read the endpoints from a WSDL document.
  2. Reading a Collection back from the XML file. In MonoTouch, we’ll use this automatically from within the ChannelFactory initialization to create all the endpoints and bindings.
  3. Generate an XSD schema file. This is not strictly required, but I added some and constraints to sanity-check binding and endpoint names, so validating the XML document against the schema will catch some common configuration problems.

Sunday, December 23, 2012

Running a MonoMac application with a custom Mono Runtime

One situation that sometimes comes up during development is running a MonoMac application on a custom Mono Runtime.

When I did my last batch of improvements to MonoDevelop's "Add Web Reference", I had to do this a lot. Since I'm currently working on a few improvements in that area, the problem came up again, so I decided to share the magic that I'm using with you.

A MonoMac application always uses the runtime that's currently installed in /Library/Frameworks/Mono.framework/Versions/Current - and this path is hard-coded in the launcher script that it's using.

Well, I really didn't feel like manually compiling gtk-sharp and all that stuff just to quickly test something - and building and installing a new .dmg each time I changed one line of code would also be very tedious task.  All I really need to replace is
System.ServiceModel.dll, but just copying the dll for testing over the installed packages would be really ugly.

One great thing about Apple's .app's is that it bundles everything in one place, so you can easily make a backup copy of your 
/Applications/MonoDevelop.app or copy it to some other place.  So after backing it up, copied the newly compiled Add-In over /Contents/MacOS/lib/monodevelop/AddIns/MonoDevelop.WebReferences/.

That was the easy part, now I just had to make the installed /Applications/MonoDevelop.app with a different Mono runtime, which contains my System.ServiceModel.dll changes - and I didn't feel like messing without anything outside that Add-In directory.

So I took the launcher scrip from monodevelop/main/build/MacOSX/monostub.m and added some command-line arguments to run it outside the .app bundle and use an arbitrary Mono runtime.  I also setup the environment variables in a way that it still loads stuff like gtk-sharp from the installed Mono, so you don't have to manually compile all that stuff.

I put the result of this little magic into my workspace module on github: baulig/Provcon-Faust/tools/monomac-launcher.m.

You can run it like this:

monomac-launcher --mono=/Workspace/INSTALL --app=/Applications/MonoDevelop.app --exe=/Applications/MonoDevelop.app/Contents/MacOS/lib/monodevelop/bin/MonoDevelop.exe

Wednesday, December 5, 2012

WsdlExporter improvements

After last week's WsdlImporter Improvements, I have now finished another batch of changes improving the WsdlExporter.

  • We now export simple , currently supporting only those elements that WsdlImporter can import.
  • Implement  MetadataExporter.Errors and use it for error reporting.
  • Fixed WsdlExporter's caching to correclty deal with duplicate ExportContract() and ExportEndpoint() calls.
  • Emit depending on the binding's MessageVersion
    (FIXME: I didn't implement soap faults yet).
  • MetadataTests: Check whether the policy elements are correct.
  • MetadataTests: Add test containing simple [OperationContract] method for http and net.tcp; checks the /  elements.
  • New test to check WsdlExporter's behavior regarding duplicate contracts and endpoints.
  • ConnectionOrientedTransportBindingElement.cs, CustomBindingTests.cs, HttpTransportBindingElementTest.cs: Added test for custom message encodings.


  As a next step, I'll implement configuration file generation.  The idea is to make MonoDevelop's "Add Service Reference" automatically generate a configuration file, just like Visual Studio does.

 

Wednesday, November 28, 2012

Running the same NUnit tests multiple times, using different profiles

One thing I really miss in NUnit is an easy way of running the same tests multiple times using different profiles.  Ideally, there should be some way to create an abstract TestContext helper class and adding an instance of this class either as property on your [TestFixture] class or as an argument on your [Test] method.  Then, you would provide multiple implementations of the TestContext and have NUnit automatically run your tests with each of these implementations.

However, I found I cool workaround for this: you can make your test fixture class abstract and put the [TestFixture] attribute on each implementation.

I'm using this technique in Mono's WCF metadata unit tests: mcs/class/System.ServiceModel/Test/MetadataTests/ImportTests.cs is the main test fixture class and the tests inside it are run three times using a different TestContext:

  1. ImportTests_CreateMetadata.cs creates a new MetadataSet programmatically
  2. ImportTests_LoadMetadata.cs loads it from an XML file
  3. ImportTests_RoundTrip.cs creates a new MetadataSet programmatically, exports it into an XML string, then imports it back from that string
You can also make your [Test] methods virtual in the base class, then override it and add [Category] or other nunit attributes to it.

Monday, November 26, 2012

WSDL Import Improvements

A couple of weeks ago, I stumbled upon a post on stackoverflow where some guy has a problem importing a web reference with Mono.

In theory, it should be a simple problem: add a Web Reference in MonoDevelop, then copy an existing app.config file from some Windows box that's already successfully talking to that same service.  And then it should all work out of the box .... or not?

The reality is that this actually does not work.  See my answer on that stackoverflow post for the detailed technical reason.  In short, Visual Studio and Mono disagree about how to name and reference an endpoint in the app.config file.  You actually have to edit the app.config file and use the fully qualified name of your service contract interface to get this work.  Well, this sucks ...

I got busy with other things, but then picked it up again last week and investigated what needs to be done to do this cleanly in Mono.

The biggest missing piece is importing bindings and endpoints from a WSDL file, which previously was not supported at all.  Once you got that, generating the configuration is not that difficult anymore, considering that configuration support already exists in Mono.

After about a week of work, I just finished implementing some of the missing pieces in Mono's WsdlImporter.  We can now import the most basic and most commonly used bindings and endpoints from WSDL: BasicHttpBinding, NetTcpBinding and the new BasicHttpsBinding that was introduced in .NET 4.5.  One huge task was supporting WS-Policy - I only implemented the simple things here.

There are a bunch of new metadata samples and nunit tests in mcs/class/System.ServiceModel/Test/MetadataTests/, see the main commit message to find out which of these are already working.

Tuesday, November 13, 2012

Wish List for a Bug Tracking System

I don't want to say Bugzilla sucks. It has many great features and is probably one of the best Bug Tracking Systems out there.

However, there are a few things the Bugzilla developers could implement in a future version to make it even better:
  1. Auto-saved drafts: Nothing sucks more than to accidentally close your browser tab while you're writing a long bug report or comment and then all your progress is lost and you have to start over again.

    Often, you also have to lookup some external sources while writing your bug report, like looking up some reference links or generating code samples, test cases, lookup a commit id, etc.  Most of the time when I'm working with bugzilla, I have at least 3+ other tabs open in my browser and am also using at least one shell window.

    Being able to save a bug report / comment and edit it later would be really helpful here.
  2. Markup and previews: The current text-mode input really doesn't fit into the 21st century anymore.  There are many great sites such as github or stackoverflow which should how a good markup and preview could look like.

    Especially for a bug tracking system, it would be great if you could easily paste code samples and have them just look great.
  3. Editing: How often do you write a comment, then realize it has some typo in it - or you want to add something, for instance a commit id on github or something that you overlooked.
  4. Tracking sub-issues: Sometimes, additional problems that are related to the one you're currently fixing appear - either you discover them yourself or the bug reporter when he's testing your fix.  So people add comments to the bug report.  There are bugs with a lot of such comments, making it hard to keep track of which of these sub-issues are already fixed and which are not.

    And easy solution for this would be to make the initial bug report editable, allowing people to add / update sub-issues.