Saturday, December 26, 2009

When your unit test is not implemented...

@YaronNaveh

When your unit test is not implemented yet you want to make sure everybody knows about it. One bad way is to throw an exception in the test first line:


[Test]
public void MyNewTest()
{
   throw new NotImplementedException();
}


The reason is that when you see in the report this failure you can never tell if the error comes from the unit test or the application under test...

The good solution is to use a build in feature of your unit test framework. For example, the Ignore attribute in NUnit. If for some reason you have to throw an exception (so the test will be red) at least throw some better error class like "new UnitTestNotImplementedException()".

@YaronNaveh

What's next? get this blog rss updates or register for mail updates!

Unit Test Legitimic Failures

@YaronNaveh

Our unit tests are expected to pass every time. When they fail we are expected to fix the regression. But in some cases it is ok for a test to temporarily not pass. For example, if the test is a small regression in a minor feature and we are currently employed with other critical missions. But while the test is failing we should at least make sure it will not cause any noise like spamming out the team.

When we use NUnit, the solution is the Ignore attribute:


[Test]
[Ignore]
public void SomeTest()
{
//...
}


The test is not red any more:

@YaronNaveh

What's next? get this blog rss updates or register for mail updates!

The XmlReader state should be Interactive

@YaronNaveh

Recently I was parsing Xml in c# from an XmlReader to an XElement:


MemoryStream m = GetMemoryStream();
XmlReader r = XmlReader.Create(m);
XElement e = XElement.ReadFrom(r) as XElement;


The last line threw this exception:


System.InvalidOperationException
The XmlReader state should be Interactive.


In MSDN ReadState.Interactive state is described as:


The Read method has been called. Additional methods may be called on the reader.


But why should I call read? It is the XElement responsibility to do it.

The solution is pretty simple though:


MemoryStream m = GetMemoryStream();
XmlReader r = XmlReader.Create(m);
//This is how we make the XmlReader interactive
r.MoveToContent();

XElement e = XElement.ReadFrom(r) as XElement;

@YaronNaveh

What's next? get this blog rss updates or register for mail updates!

Wednesday, December 9, 2009

Not all errors are red

@YaronNaveh

Here's some WCF service trace log. Is there a problem with the service the log belongs too?



Of course, we see an error in the red line.

What about this service?




Well, nothing red here. Let's drill down into the "warning" details:



It seems WCF errors come in more than one color.

@YaronNaveh

What's next? get this blog rss updates or register for mail updates!

Friday, November 20, 2009

Axis 2 WS-Security (rampart) FAQ

@YaronNaveh

Rampart is the WS-Security module of Axis2.
Prabath summarizes all the current posts that were published in the rampart FAQ blog.

@YaronNaveh

What's next? get this blog rss updates or register for mail updates!

Thursday, November 19, 2009

Generating meaningful names from a url

@YaronNaveh

A common programming task is to extract some meaningful name from a url.
One example is saving the contents of a url to a file name on disk - we need to have the name for the file.
Bellow there is a c# method that I have written that extracts this name + the results it produces on a few urls.


http://www.myServer.com/Services/Sanity.asmx?WSDL - Sanity
http://www.myServer.com/Services/Sanity.asmx?wsdl=1&xsd=2 - Sanity
http://www.myServer.com/Services/Sanity12345678901234567890123456789012345678901234567890123456789012.asmx - Sanity1234567890123456789012345678901234567
http://www.myServer.com/Services/Sanity.asmx - Sanity
http://www.myServer.com/Services/Sanity/ - Sanity
http://www.myServer.com/Services/Sanity - Sanity
http://www.myServer.com/Services/Sanity?wsdl - Sanity_wsdl
http://www.myServer.com/Services/Sanity/?wsdl - Sanity





public static string GetSuggestedNameFromUrl(string url, string defaultValue)
{
  const int MaxChars = 50;

  string res = Path.GetFileNameWithoutExtension(url);

  //check if there is no file name, i.e. just folder name + query string
  if (String.IsNullOrEmpty(res) || IsNameOnlyQueryString(res))
  {
    res = Path.GetDirectoryName(url);
    res = Path.GetFileName(res);

    if (String.IsNullOrEmpty(res))
       res = defaultValue;
  }

  res = ReplaceInvalidCharacters(res);

  if (res.Length > MaxChars)
     res = res.Substring(0, MaxChars);

  return res;
}


private static string ReplaceInvalidCharacters(string res)
{
  return Regex.Replace(res, @"[^\w]", "_", RegexOptions.Singleline);
}


private static bool IsNameOnlyQueryString(string res)
{
  return !String.IsNullOrEmpty(res) && res[0]=='?';
}

@YaronNaveh

What's next? get this blog rss updates or register for mail updates!

Saturday, August 22, 2009

BindingBox: Convert WCF Bindings

@YaronNaveh



The WCF BindingBox is here!

What is it?
BindingBox is an online application that converts WCF bindings to a customBinding configuration.

Why we need it?
WCF bindings capture a general use case and allow us to customize it. For example, basicHttpBinding is good for interoperability with older soap stacks while WSHttpBinding fits WS-* based communication. However, there are cases where we can not use the out of the box bindings. For example:

  • We want to further customize the binding behind what it exposes. For example, we might want WSHttpBinding not to send a timestamp (for interoperability reasons) but this is not a built in option.

  • We have a special use case which is not captured by any of the out-of-the-box bindings. For example, we want to use binary encoding over http.

    In such cases we need to convert our binding into a custom binding. This is not a trivial process. In particular, some security settings can be very frustrating to translate.

    For this reason I have written the WCF BindingBox. This is an online application which automatically converts any binding to a customBinding.



    How to use it - Tutorial

    Step 1 - Get your current binding

    Just open your web.config or app.config file and navigate to the "<bindings>" element. Then copy its content to the clipboard. Be sure to copy the wrapping "<bindings>" element as well:


    <bindings>
       <wsHttpBinding>
         <binding name="MyBinding">
           <security>
            <message clientCredentialType="UserName" />
           </security>
         </binding>
        </wsHttpBinding>
    </bindings>


    Step 2 - Convert your binding

    Just navigate to the BindingBox and paste your binding from step 1. Then click on the "Convert to CustomBinding" button and copy to the clipboard your new binding. It may look like this:


    <customBinding>
       <binding name="NewBinding0">
         <transactionFlow />
         <security authenticationMode="SecureConversation" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10">
           <secureConversationBootstrap authenticationMode="UserNameForSslNegotiated" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" />
         </security>
         <textMessageEncoding />
         <httpTransport />
       </binding>
    </customBinding>


    Step 3 - Use the custom binding

    Basically, you now just need to use the BindingBox result as your binding configuration.

    In practice you would do it in one of the following ways:

  • In your .config file manually configure your endpoint to use a custom binding and set its configuration.
  • Use the WCF configuration editor to configure your endpoint to use a CustomBinding. then in your .config override the default customBinding configuraiton with the BindingBox result.


    Currently supported bindings

    BindingBox currently supports these bindings:

  • WSHttpBinding
  • WS2007HttpBinding
  • BasicHttpBinding


    More to follow

    Stay tuned for these:

  • NetTcpBinding
  • NetNamedPipeBinding
  • WSFederationHttpBinding
  • WS2007FederationHttpBinding


    Known issues

  • DefaultAlgorithmSuite is not converted
  • ReaderQuotas are not converted

    Please report any bug you find. Also feel free to submit an enhancement request.

    There is also a nice story behind BindingBox: It uses cutting edge technologies such as Windows Azure and MEF. More to come on this...

    @YaronNaveh

    What's next? get this blog rss updates or register for mail updates!
  •