Showing posts with label Coding. Show all posts
Showing posts with label Coding. Show all posts

Tuesday, February 21, 2012

Consuming XML-RPC web services with C# - Part 3 - let others do the work

At Part 1 and Part 2 I have shown the raw way and the object oriented approach to consume xml-rpc web services.

This time, we take a look at a helper to simplify the process of developing and consuming XML-RPC web services.

Wednesday, November 24, 2010

Store data from async web service call with Silverlight

Did you ever tried to store data on Silverlight from an async web service call using SaveFileDialog - and get told it doesn’t work?
But that is only half the truth. Here it how it works!

Monday, November 1, 2010

Visual Studio, TFS and WinMerge

WinMerge is a great and handy tool for comparing and merging source code and other textual file types within a team development environment.

I use it since a few years and therefore I also want to use it with VS2010 and TFS and it’s very simple.

Just go through

Tools > Options > Source Control > Visual Studio Team Foundation Server > Hit <Configure Tools>

Here you can setup custom tools for comparing and merging.

Add a new tool and provide the installation path to WinMerge and some command line arguments like (explanation follows):

Zwischenablage01

Hit <OK> and do the same with merge.

Command line switches for WinMerge are (copied from the WinMerge documentation):

  • /r compares all files in all subfolders (recursive compare).

  • /e enables you to close WinMerge with a single Esc key press.

  • /f applies a specified filter to restrict the comparison. The filter can be a filemask or the name of a file filter

  • /x closes WinMerge (after displaying an information dialog) when you start a comparison of identical files.

  • /s limits WinMerge windows to a single instance.

  • /ul prevents WinMerge from adding the left path to the Most Recently Used (MRU) list.

  • /ur prevents WinMerge from adding the right path to the Most Recently Used (MRU) list.

  • /u prevents WinMerge from adding either path (left or right) to the Most Recently Used (MRU) list.

  • /wl opens the left side as read-only.

  • /wr opens the right side as read-only.

  • /minimize starts WinMerge as a minimized window.

  • /maximize starts WinMerge as a maximized window.

  • /dl specifies a description in the left side title bar, overriding the default folder or filename text.

  • /dr specifies a description in the right side title bar, just like /dl.

  • leftpath specifies the folder, file or project file to open on the left side.

  • rightpath specifies the folder, file or project file to open on the right side.

  • outputpath Specifies an optional output folder where you want merged result files to be saved.

  • conflictfile Specifies a conflict file, typically generated by a Version control system.

Resources:
flurfunk.sdx-ag.de
www.prowebconsult.com
Winmerge manual

Sunday, October 31, 2010

Auto checkout with VS2010 and TFS2010 not working

On my project “Simple Proxy Switch” I decided to access codeplex version control system through VS2010 TFS integration.

As I mainly use Subversion I like that you can edit files without locking them. As I arrived on TFS I missed this a lot. Searching for other options I found the ability of TFS to check out files on edit.

image

Nevertheless it doesn’t work for me. Files still are not getting checked out on edit.

Fast search at the web discovers some helpful links mainly again in the Microsoft Developer Network.

The missing setting was the binding of the solution to TFS. This can be achieved on Selecting solution and than go through

File –> Source Control –> Change Source Control

select solution and hit <Bind>.

Et voilà. Now VS2010 checks out files on edit.

Thursday, October 21, 2010

Detect current network setting with C#

In cause of my latest project Simple Proxy Switch I had to face the challenge of detecting the setting of the current network the computer is connected to.

Now here’s my snippet how do I retrieve the current network setting:

// zero conf ip address
IPAddress zeroConf = new IPAddress(0);
// get current assigned addresses
IPAddress[] hostAddresses = Dns.GetHostAddresses(Dns.GetHostName());

var networkData = NetworkInterface.GetAllNetworkInterfaces()
  // filter running network interfaces
  .Where(network => network.OperationalStatus == OperationalStatus.Up)
  // filter unknown interfaces
  .Where(network => network.NetworkInterfaceType != NetworkInterfaceType.Unknown)
  // filter loopback interfaces
  .Where(network => network.NetworkInterfaceType != NetworkInterfaceType.Loopback)
  // get the properties
  .Select(network => network.GetIPProperties())
  // filter initialized gateways
  .Where(ipProps => ipProps.GatewayAddresses.All(gateway => gateway.Address != zeroConf))
  // filter and get ip addresses
  .SelectMany(ipProps => ipProps.UnicastAddresses.Where(ucAddress => hostAddresses.Contains(ucAddress.Address)))
  .Where(ucAddress => hostAddresses.Contains(ucAddress.Address))
  // simply use the first
  .FirstOrDefault();

Be the force with you!

Monday, September 27, 2010

Network drives unavailable while running programs under Admin privileges

Since the installation of VS 2010 I discovered that some network shares are unavailable to access with Visual Studio 2010. This is disturbing because my shared settings living on a network share.
I guessed this was caused because of VS2010
%VSINSTALLDIR%\Common7\IDE\devenv.exe
is treated to be executed as administrator also the VSLauncer.exe located in
%CommonProgramFiles%\microsoft shared\MSEnv\VSLauncher.exe
Yesterday I spent now 2 min with google and found a solution.
The problem is caused by using different access tokens for user currently logged in and program that runs with admin privileges. Further information can be found here.
This problem can be solved by enable sharing of access tokens between current (filtered) and admin access. This can be done by creating registry key
EnableLinkedConnections
located under
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System
and setting it’s DWORD value to ‘1’. Detailed information can be found here.

Have fun.

Wednesday, August 11, 2010

ReSharper shortcuts for VS2010 to improve TDD productivity

ReSharper is a powerful tool that helps you to produce better code.
One very nice feature is to run your NUnit-Test within the integrated Testrunner. But it’s a little bit annoying to click on the play button every time you want to run a test.
The easiest thing to handle this is to set up keyboard shortcuts to run you tests. Visual Studio supports setting shortcuts for a lot of actions, so let’s go straight ahead and set up shortcuts.
Simple open the options dialog for keyboard settings via Tools –> Customize –> hit “Keyboard” button and then search for
ReSharper.ReSharper_UnitTest_

As result you get something like the following:
image

Now you can set the shortcut for the desired operation.

I have chosen combinations of

Ctrl + Shift + Alt & '<'

A short explanation of the shown possibilities:
  • ContextRun runs the current selected test
  • ContextDebug debugs the current selected test
  • RunCurrentSession reruns a previous test session
  • RunSolution runs all test within the solution
Have fun.

Thursday, June 24, 2010

Remote Certificate validation

Sooner or later every developer has to deal on retrieving information from some resource accessible through HTTP protocol.

While it is easy to get this information from unsecured HTTP resources it's often the case that such a resource is only available for secured access through HTTP.

In that case and to ensure that this connection is trusted you have to check the server certificate for that connection.
Some sample piece of code to access and download data over http can look like this one:
string data = String.Empty;
using (WebClient webClient = new WebClient())
{
   byte[] rawData = webClient.DownloadData(new Uri(/* path to resource */));

   using (MemoryStream memStream = new MemoryStream(rawData))
   {
      using (StreamReader reader = new StreamReader(memStream, true))
      {
         data = reader.ReadToEnd();
      }
   }
}

This code works well for connections through HTTP.

Now you have to access the same resource over HTTPS and this is where you have to face a new challenge:

Validation of Certificates.

In the case of .NET there is the ServicePointManager that let you intercept the mechanism of validation.
It defines the ServerCertificateValidationCallback method which is called on secure resource access.

The adjusted code is now something like this:
string data = String.Empty;
using (WebClient webClient = new WebClient())
{
   RemoteCertificateValidationCallback validationCallback =
      (sender, cert, chain, errors) =>
      {
         return true;
      };

   ServicePointManager.ServerCertificateValidationCallback += validationCallback;

   try
   {
      byte[] rawData = webClient.DownloadData(new Uri(/* path to resource */));

      using (MemoryStream memStream = new MemoryStream(rawData))
      {
         using (StreamReader reader = new StreamReader(memStream, true))
         {
            data = reader.ReadToEnd();
         }
      }
   }
   finally
   {
      ServicePointManager.ServerCertificateValidationCallback -= validationCallback;
   }
}

This code simply declares all certificates as valid which is some kind of stupid.

Let's focus on the validation of the certificate. Feel free to implement you own code of validation or just simply use .NET integrated features.

Something I did at bugtraqplugin is to use the default validation chain which depends on certificates stored at the Windows certification storage.

My simple approach:
RemoteCertificateValidationCallback validationCallback =
   (sender, cert, chain, errors) =>
   {
      return (certificate as X509Certificate2 ?? new X509Certificate2(certificate)).Verify();
   };

All code put together can be available at source code of bugtraqplugin.

Thursday, April 8, 2010

Consuming XML-RPC web services with C# - Part 2 - the OOP approach

At Part 1 I described a basic way to call and consume data from xml-rpc web services based on XML string operations.

This can be only the first approach. It's unhandy and not easy to maintain.

The next level for me would be to cast this into an object oriented data structure that could be easily maintained and customized for special needs.

At this post I try to show my object oriented approach to consume such services.

Friday, March 19, 2010

Consuming XML-RPC web services with C# - Part 1 - the rough way


While dealing with integration of Bugzilla as data provider within BugtraqPlugin (which I described here) I encountered that bugzilla has an XML-RPC interface. Not this bad, but

How to perform XML-RPC calls with c# and .NET?

It's a simple task to get access on soap web services because it provides a description of it's own interface through a wsdl definition, so you easily can generate proxy classes for such a case.

But whats about XML-RPC web services? Here is just a sample practiced on bugzilla web service.

Monday, February 15, 2010

Customizing Visual Studio code templates to fit your needs

Did you ever get rid of that the default template on project context menu > add > class misses you prefered regions, default method overrides, properties or constructors?

Here is a guide how do you extend this template.

Tuesday, February 9, 2010

Tortoise SVN Plugin for issue tracking system

In the field of software development it is enssential that source code is managed through a version control system. Without that, you loose an easy way to track changes and versions of your project.
(But discuss that is not the intention of this blog post, so skip comments about that.)

On the other hand, a version control system is not the right tool to manage project dependent things, feature requests and to track bugs. So professional teams use also a project management tools in combinition with bug tracking systems.
(To discuss the pro an cons of different systems is also not the intention of this post, so ...)

And now, one question:  
What it will be, if you combine these applications and let them communicate in two ways?

Thursday, December 10, 2009

Wednesday, December 9, 2009

Printing numeric values as HEX

I sometimes need to print integer numeric values as a hexadecimal string for better reading and everytime I have to search about it with google.

So here is a collection of printing values as hexadecimal string.

The simpliest way is to use the IFormattable interface:
   1:  String.Format("{0:X02}", int.MaxValue);

Other ways with support of differend radix is to user Convert.ToString().
Radix could be 2, 8, 10, or 16.
   1:  Convert.ToString(int.MaxValue, 2);

From:
MSDN: Convert.ToString()
mikrocontroller.net


Are there any other ways? So let me know!

Monday, December 7, 2009

Advanced Stringreplace

While handling string data it's often needed to replace or cut a partial string. String.Replace is such a simple method to replace values in C#.

But what about if you have to deal with patterns to replace or manipulate partial string?

Here you have to go beyond String.Replace and deal with regular expressions.
If you have't heared about Regular Expressions, I suggest to read this articles about it:
and some tools to check you patterns against data:

And now, time for some excamples.

Assume you have a string like
   1:  string data = "52/00000012340056";
and have to replace the zeros after the slash.

Your regex could be the following
   1:  Regex regex = new Regex(@"0+");

but take care about the zeros within the numbers. So you have to extend you regex like this:
   1:  Regex regex = new Regex(@"^\d+/0+");

If you run this expression over the sample data, the result looks like this:
   1:  string result = regex.Replace(data, String.Empty);
   2:  // result == "12340056";

Missing the prefix?

Extend the expressions with some named groups and use the matching values:
   1:  Regex regex = new Regex(@"^(?<prefix>\d+/)(0+)");
   2:  string result = regex.Replace(data, "${prefix}"); 
   3:  // result == "52/12340056";


That's it!

Thursday, November 19, 2009

WANTED - Dead or Alive

What devolpers hunting for:

Thursday, November 5, 2009

MetalScroll

Just got a hint from rschu@twitter / blogpost @ kodierer.blogspot.com about a MetalScroll extension for Visual Studio.

Here some features:
  • scrollbar extension (shows file structure with highlighted position of booksmarks and breakpoints)
  • split editor into two panes
  • highlighting all occurrences of a word within a file

Check it out:
MetalScroll @ code.google.com
MetalScroll in der Visual Studio Gallery

XML Validation

Next post in the line of XML processing hints with C#. This time we want to validate a xml file.

Assume a sample:
You've got an XML-File books.xml as follows:

   1:  <books xmlns="urn:books">
   2:   <book>
   3:    <author>It's just me</author>
   4:    <publisheddate>2009-09-25T09:06</publisheddate>
   5:   </book>
   6:  </books>

And a xml schmema (books.xsd) like this:

   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <xs:schema targetNamespace="urn:books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   3:   <xs:element name="books">
   4:    <xs:complexType>
   5:     <xs:sequence>
   6:      <xs:element name="book">
   7:       <xs:complexType>
   8:        <xs:sequence>
   9:         <xs:element name="author" type="xs:string" />
  10:         <xs:element name="publisheddate" type="xs:dateTime" />
  11:        </xs:sequence>
  12:       </xs:complexType>
  13:      </xs:element>
  14:     </xs:sequence>
  15:    </xs:complexType>
  16:   </xs:element> 
  17:  </xs:schema>

Now there is more than one way to validate the xml file.

The first way I want to show is to load the whole xml file into a XmlDocument instance like.

   1:  // load xml
   2:  XmlDocument xmlDoc = new XmlDocument();
   3:  xmlDoc.Load("books.xml");

Now we need the schema to validate against ...

   1:  // load schema XmlSchema schema; using(XmlReader schemaReader = XmlReader.Create("books.xsd")) schema = XmlSchema.Read(schemaReader, (p, q) => if(q.Exception != null) throw q.Exception); 

Add this to the schemas of the XmlDocument to provide it for validation. Be carefull, that the schema is added for the namespace of the document nodes.

   1:  // add schema xmlDoc.Schemas.Add(xmlDoc.DocumentElement.NamespaceUri, schema);

Now the document could be validated. (Just providing a lamda expression as ValidationEventHandler.)

   1:  // validate document
   2:  xmlDoc.Validate((p, q) => if(q.Exception != null) throw q.Exception);

There is only one handycap: Validation Warnings are not passed to the ValidationEventHandler.

The other way to validate a xml file is to use a XmlReader (or just one of it's derived classes). This is the favored way for large xml files.

First let us initialize such an reader. To use validation functionality of the reader we must pass in some XmlReaderSettings to configure that reader.

   1:  // loading schema
   2:  XmlSchemaSet schemaSet = new XmlSchemaSet();
   3:  schemaSet.Add("urn:books", "books.xsd");
   4:  XmlReaderSettings settings = new XmlReaderSettings();
   5:  settings.ValidationType = ValidationType.Schema;
   6:  settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
   7:  settings.Schemas = schemaSet;
   8:  settings.ValidationEventHandler += (p, q) =>
   9:  {
  10:     if (args.Severity==XmlSeverityType.Warning)
  11:        Console.WriteLine("Warning: Matching schema not found. No validation occurred." + args.Message);
  12:     else if(q.Exception != null)
  13:        throw q.Exception;
  14:     else
  15:        Console.WriteLine("\tValidation error: " + args.Message);
  16:  };
  17:  // create reader
  18:  using(XmlReader reader = XmlReader.Create("books.xml", settings))
  19:  {
  20:     // validate
  21:     while(reader.Read());
  22:  }

This is it, now you receive validation errors and warnings.

Monday, September 28, 2009

Just a prefix issue

Everybody who has to deal with XML and XPath in .NET just Run into an issue of accessing elements without having any prefix for the default namespace.

Assume a sample:
You've got an XML-File books.xml as follows:
<books xmlns="urn:books">
 <book>
  <author>It's just me</author>
  <publisheddate>2009-09-25T09:06</publisheddate>
 </book>
</books>

And now? How to access a node?

First attempt:
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
 
// just build xPath for testing
StringBuilder xPath = new StringBuilder();
XmlNode someNode = doc.DocumentElement.FirstChild.FirstChild;
 
do
{
   xPath.Insert(0, someNode.LocalName);
   xPath.Insert(0, "/");
   someNode = someNode.ParentNode;
}
while (someNode != null && someNode.NodeType != XmlNodeType.Document);
 
// xPath == "/books/book/author"
XmlNode author = doc.DocumentElement.SelectSingleNode(xPath.ToString());

What is the value of author? Right, it's null, hu.

Next attempt, try using XmlNamespaceManager.
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
// try to access with XmlNamespaceManager
XmlNode author = doc.DocumentElement.SelectSingleNode(xPath.ToString(), manager);

What is the value of author? Right, it's still null, hu.

And now?

Try to explain: The Namespace and the Prefix of the used namespace is not defined so xpath could not be resolved.

So try this:
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
// try to access with XmlNamespaceManager
XmlNode author = doc.DocumentElement.SelectSingleNode(xPath.ToString(), manager);

So try this:
string prefix = "mySample";
if (String.IsNullOrEmpty(manager.LookupPrefix(doc.DocumentElement.NamespaceURI)))
{
   // namespace not added
   manager.AddNamespace(prefix, doc.DocumentElement.NamespaceURI);
}
 
// injecting the namespace
xPath.Replace("/", String.Format("/{0}:", prefix));
 
// xPath is now  "/{mySample}:books/{mySample}:book/{mySample}:author"
author = doc.DocumentElement.SelectSingleNode(xPath.ToString(), manager);

And now, author sould be the selected node.

Thursday, September 10, 2009

Keep clean with temporary files

Every developer runs into a situation where temporary files are needed. In cause of an error, many developers forget to clean up this garbage within the temporary files directory. And I HATE that.

So therefore here is an hint for C# developers:
Just use the
FileOptions.DeleteOnClose
value as parameter of the
FileStream
constructor, this will keep you handles clean.

Heres comes an excample:
using (FileStream fStream = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.DeleteOnClose)))
{
// do work
}

Good luck.