Converting a PHP API to a C# implementation

by 11. January 2010 14:32

I have this web service I want to use. The supplier only provides a PHP example to use the API. It is not a standard SOAP or REST service, but a custom Http POST based command.  This means that you that you either have to write

http://www.asp.net/downloads/archived/migration-assistants/php-to-aspnet/

Because I had no Visual Studio installed, ready to be launched, i used the command line conversion. 

C:\Program Files\Microsoft Corporation\PHP to ASP.NET Migration Assistant>PHPConvert.exe "c:\source\PhpSource" /out "C:\source\AspDotNetVersion”

The port of the code was successful. Most of the code was converted to c#.  A lot of conversion warnings popped up, but most of them where not imposing any severe problems.

Code clean-up

A manual code clean-up is advised.  For example, this code comes clearly from an unmanaged environment:

  1. dpublic virtual void  useSecure(bool val)
  2.         {
  3.             if ((val == true) && (val.GetType() == true.GetType()))
  4.             {
  5.                 this.Secure = true;
  6.             }

Why should you check the type of “val” at runtime?  This is already done by the compiler.  A simple “ if (val== true) “  would be sufficient.  Or in this case even shorter:

  1. d        public virtual void useSecure(bool val)
  2.         {
  3.                 this.Secure = val;
  4.         }

Other things to clean up are links to magic-quotes, cURL and other specific PHP stuff.

Calling the API

Calling the API means in PHP that you do a socket write and read. For example, in PHP one would do:

$sock = fsockopen("ssl://".$this->ApiUrl["host"], 443, $errno, $errstr);

Because every effort is done to maintain the original architecture by the conversion tool.  The convertor tool added a PHP namespace containing extra support classes for duplicating the original (php) functionality. The previous line of code is thus converted to:

sock = PHP.NetworkSupport.OpenSocket("ssl://" + this.ApiUrl["host"], 443, errstr);

The OpenSocket function is a wrapper function with one line (with some additional simple error handling):

returnValue = (System.Net.HttpWebRequest)System.Net.WebRequest.Create((System.String)Target); // + ":" + System.Convert.ToInt32(Port));

Although this is neat, I like the native .Net HttpWebRequest and HttpWebResponse Classes.  So I took the code from my previous blog post: Http Post using C#, adapted it a bit and removed almost 80 lines of the converted code.

My problem with the wrapper functions created by the convertor tool, is that they use System.Object parameters and also return a System.Object.  So for readability and maintenance reasons, I started immediately converting this to some strongly typed code.

Conclusion

I was very pleased with this tool.  I saved literally hours typing, and it took me (only) a few hours of refactoring.   Refactoring meant:

  • removing unused functionality (magicQuotes, cURL, …)
  • removing unnecessary type checks using Relfection
  • removing calls to wrapper functions when I had decent managed .Net code available in my library

And just now I started thinking that it may have been faster implementing a custom WCF implementation. 

Tags: , , ,

.Net

Famous .Net websites found on the web (Part 2)

by 1. September 2009 11:30

Ok, I admit.  I know my blogging rhythm is very low this year, it's about the opposite of my 'normal' day-to-day life rhythm. But I prepared in the past months this blog post…

From time to time I stumble upon famous web sites using .Net (part 1).  I would like to add some sites to the list.

Let me know if know any other big web sites using .Net technology, so I can complete the list.

TeamViewer – Desktop Sharing over the internet
image
Intel Download Center
image
Hardrock Café’s Memorablia  using Silverlight technology, Deepzoom.
image

Have you ever tried zooming really deep?  Look at this post: How to really show of with deepzoom

CRDC.Be - Official Belgium Database for Number Portability
image

Stanford Engineering – Sub site of university

image

Rabobank.be – online bank (and know sponsor of cycling)
image
DoubleClick – online advertising
image
International Telecommunication Union
image

Tags:

.Net

How to really show off Deepzoom technology

by 31. August 2009 12:57

What is Deepzoom

Deepzoom is a product build on Silverlight technology.  It gives you the ability to smoothly zoom and pan on images.  The loading time is incredibly short because only parts of images are send over the network. This type of browsing can be compared with viewing imagery on google maps, live earth.

A nice example is the Memorablia of the HardRock café.  I searched for a guide on the internet, on how to get as deep as possible on the pictures.  This examples goes even a few levels deep.

If you search on the web for Deepzoom, you will find much more details and explanation.

Where to start

Go to http://memorabilia.hardrock.com/

and look for “Paul McCartney – The Beatles Letter from Paul and Beatles autographs”.  (Item number: 043311).

Finding this item, can be easily done by:

  • be clicking on “The Beatles” on the left side, or
  • use the search on the top right of the screen like this:
    image   or image

The item you are looking for is this one: 
 image

Go deeper

Have you already noticed the stamp on the left top of the envelop?  That is where you should go.  Use your mouse scroll wheel to enlarge this item.

As you will see this is not a normal stamp.  These are pictures of various buildings.  (Does anyone know which buildings these are?)

image

Zoom into the picture of the 3th row, 2nd column.  If I’am not mistaken, this is the Seattle hardrock Café.

image

If you thought this is cool, go to left below the middle of the image, you will see some white suites

image

image

Then zoom in on “The Beatles” Doll's image in the middle.

image

This last doll’s are available as a separate item  in Memorablilia: The Beatles “Bobb’n Head” dolls, and is available under Item number 051228.  The cool thing is, this contains some embedded video as shown below (click More Info – Video).

Do it yourself

You can make yourself Deepzoom content by using deepzoom composer.  Try it out, and let me know if you archived something spectacular.

Tags:

.Net

Managed Extensibility Framework Introduced By Bart

by 19. August 2009 18:54

I have been thinking several years about how to implement Extensibility in a decent manner for one of my applications.  The problem is that as a sole freelance developer, implementing extensibility is not something you NEED.  It is more a ‘nice to have’.  At the end of 2008, I found the MEF (Managed Extensibility Framework).  But due to a lack of time and lack of articles about it, never started investigating into it.  I admit, I do not always jump on the newest beta technologies right away.

Unfortunately, when you search for some information on the MEF CodePlex website on how it exactly works, it doesn’t really encourage you to read further…  Try to dive in the first to links on the topic “Where to go next”

  • First there is:  Learn more about MEF .  Completely correct, but a 100% cryptic message for dummies.  Telling someone you have to start with a container or a catalog without explaining what a catalog or container is, is not very convincing.
  • Or take the programming guide, just a few steps to do very complex things.  My problem was not “how to”, but I wanted to understand what I was doing.  Adding references adding class or method attributes can be done by my little sister, but would she know what she has done?  I Guess not, I even bet she doesn’t understand these terms.

So, I watch out for months about articles covering the Managed Extensibility Framework (MEF).  Articles explaining the basics. Telling me what it exactly is, or what the basics are.  Some sort of “Hello World” for advanced programmers, but “MEF dummies”.

Earlier this week I found the article an article, written by a Belgian fellow, now working in Microsoft Redmond.
His blog posts tend to be very technical material for advanced LINQ adepts or Expression Tree lovers. This time he covered “MEF for Dummies”, just what I needed.  I’ve written before about the mouse-hating code-lover called Bart De Smet. (See a previous post “Bart De Smet gone mad about extension methods”.) 

Bart proved his excellent writing capabilities in his blog post written earlier this month. If you are interested in MEF, then read  A Whirlwind Tour through the Managed Extensibility Framework.  I highly recommend it.

Congratulations Bart, it was easy to read, even humorous. And most of all it was very understandable.  Thank you very much!

Tags: ,

.Net

Dependency Matrix & Application metrics

by 29. December 2008 01:44

I got today back on an old controls assembly.  I am using DevExpress controls for more than a year.  But there are still some forms using my old control library.  It would have been a mess cleaning up this old directory, until i realized that this could be done very easily using NDepend.

I loaded the assemblies and used the dependency matrix to find out where the controls are used.

image

Especially the “Remove Empty… Rows and columns” image  gave me a quick overview of controls which still where used on some forms and should have been replaced by other ones.

Furtermore i looked – just for fun – at the “abstractness versus instability” graph created by NDepend.  I took a screenshot:

image 

As you can see, it ain’t looking good.  When i started developing this application, i never heard of abstractions and instability was my middlename. Well… None of my assemblies are in “zone of uselessness” (upper right corner), but i know the “zone of pain” very well.

The numbers aren’t lying:

Application Metrics

Number of IL instructions: 1006520
Number of lines of code: 121799
Number of lines of comment: 22251
Percentage comment: 15
Number of assemblies: 11
Number of classes: 849
Number of types: 896
Number of abstract classes: 2
Number of interfaces: 10
Number of value types: 4
Number of exception classes: 0
Number of attribute classes: 0
Number of delegate classes: 0
Number of enumerations classes: 33
Number of generic type definitions: 36
Number of generic method definitions: 5
Percentage of public types: 86,27%
Percentage of public methods: 80,31%
Percentage of classes with at least one public field: 2,23%

I know i can do better  :-)

Tags:

.Net

Big companies discovered .Net

by 8. September 2008 18:27

I was surfing today and I noticed that some web sites of big companies where using .Net.
This doesn't mean that .Net is there main technology, but the extension is at least used in the URL...

McAfee

They use it in the FAQ's... here

image

 

Fabricom GTI (Suez)

image

At their main page: http://www.fabricom-gti.com

Dell

image

At their main site...  http://www.dell.com

Yahoo

image

Probably they are not really using .Net, but it is a .Net file on there domain anyway...
On this survey link: http://surveylink.yahoo.com/wix/p0473306.aspx

Tags:

.Net

NDepend

by 21. June 2008 09:58

A few days ago I received my NDepend license. 

What is NDepend

On their web site (http://www.ndepend.com), it states that :

NDepend is a tool that simplifies managing a complex .NET code base.

So in other words, (also on the web site)

Architects and developers can analyze code structure, specify design rules, plan massive refactoring, do effective code reviews and master evolution by comparing different versions of the code.

Out of my experience, I know - I really know - that bad designed software is a nightmare to maintain.  Worsted of all is that most of the time is it horribly coded and badly documented.  I've done seen in the past, and I am still struggling today.  It's easy to guess where the problems are, or how you could make the application better.  Because guessing is most of the time not the proven to be good, scientists created something like "metrics". 

Metrics measure your software, code base or even your software design.  NDepend uses these metrics and also the dependencies between your assemblies to tell you more about your application.

More about NDepend later!

Tags: ,

.Net

Eval Statement and .Net Runtime Compilation (retry)

by 9. May 2008 12:44

Yes I come from a VBA environment.  Microsoft Access all the way.  Last years I am into .Net but I was still missing my "good old" Eval statement.  A colleague triggered my curiosity and I started digging for a solution.

We dive into 2 methods.  The first is using the Microsoft Script Control, the second is the Runtime Compilation.

[more]

The easiest solution is probably to take the "Good Old" eval method from where it is available.  This means that you will need to set a reference to: COM Microsoft Script Control 1.0

MSScriptControl.ScriptControlClass mScriptControl; mScriptControl = new MSScriptControl.ScriptControlClass(); mScriptControl.Language = "VBScript"; mScriptControl.AllowUI = false;  mScriptControl.Reset(); try {    retval = mScriptControl.Eval(s);     } catch{ /* ... */ } 

You have to admit, this isn't to difficult.
This is well described on vb-tips.com

Another solution would be Compilation at Runtime. 

The simplest example I found was at EggheadCafe: Build a Custom .NET "EVAL" Provider.  It is created for VB, but can easily be changed to C#.

For archiving purposes, here is the code...

VBCodeProvider c = new VBCodeProvider();ICodeCompiler icc = c.CreateCompiler();CompilerParameters cp = new CompilerParameters();cp.ReferencedAssemblies.Add("system.dll");cp.ReferencedAssemblies.Add("system.xml.dll");cp.ReferencedAssemblies.Add("system.data.dll");cp.CompilerOptions = "/t:library";cp.GenerateInMemory = true;StringBuilder sb = new StringBuilder("");Debug.WriteLine(sb.ToString());// look at this to debug your eval stringCompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString());System.Reflection.Assembly a = cr.CompiledAssembly;object o;MethodInfo mi;o = a.CreateInstance("PAB.PABLib");Type t = o.GetType();mi = t.GetMethod("EvalCode");object s;s = mi.Invoke(o, null);return s;

To compile some c# code at runtime I found the Runtime Compilation (A .NET eval statement) article on CodeProject.  This is a library which cuts out the compiler references etc. If you use this 'Evaluator' class, you can call a e.g. a StringChanger method as described below.  This code is also available on the mentioned page as example.

string myCode = "s =+ \" TEST\"; return s";

MethodResults stringChanger = null;StringBuilder source = new StringBuilder();source.Append("public string StringChanger(string s)");source.Append(Environment.NewLine);source.Append("{");source.Append(Environment.NewLine);source.Append(myCode);source.Append(Environment.NewLine);source.Append("}");try{ stringChanger = Eval.CreateVirtualMethod( new CSharpCodeProvider().CreateCompiler(), source.ToString(), "StringChanger", new CSharpLanguage(), false);}catch(CompilationException ce){ MessageBox.Show(this, "Compilation Errors: " + Environment.NewLine + ce.ToString()); return;}try{ output.Text = (string)stringChanger.Invoke(inputString.Text);}catch(System.Reflection.TargetInvocationException tie){ MessageBox.Show(this, "Method-Thrown Exception: " + Environment.NewLine + tie.InnerException.ToString()); return;}

We learned today that there are many possibilities with the System.Reflection and the System.CodeDom.Compiler namespaces.

Tags: , , , , ,

.Net | C#

Visual Studio 2005 Code Snippet Editor

by 21. March 2008 12:44

Ever wondered how to change or add code snippets to Visual Studio 2005?  As you probably found out, you have to change or create some XML.  On techRepublic you can find an article on "Creating custom Visual Studio 2005 Code Snippets".

But did you know codeplex is hosting a "Editor for creating and modifying Visual Studio 2005 Code Snippets".  Snippy - Visual Studio Code Snippet Editor

Tags: , , ,

.Net

Http POST to webserver using C#

by 6. March 2008 12:38

I 've written some time ago a HTML scraper to search the VIES VAT number Validation web site of the European Union.  The service offered no web service at that time, so I needed to use the HTTP POST method.

The code is not too difficult.

[more]

    private readonly string Url = "http://ec.europa.eu/taxation_customs/vies/cgi-bin/viesquer";
private readonly string Referer = "http://ec.europa.eu/taxation_customs/vies/en/vieshome.htm";
List<Exception> Exceptions = null;
     ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "Lang=EN";
postData += ("&VAT=" + VatNumber);
postData += ("&MS=" + CountryCode);
postData += ("&ISO=" + CountryCode);
byte[] data = encoding.GetBytes(postData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(this.Url);
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.Referer = Referer;
myRequest.ContentLength = data.Length;
myRequest.ProtocolVersion = HttpVersion.Version10;
myRequest.Method = "POST";
Stream newStream = myRequest.GetRequestStream();
// Send the data.
     newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse resp = null;
try
     {
resp = (HttpWebResponse)myRequest.GetResponse();
}
catch (Exception ex)
{
Exceptions.Add(ex);
}
     StreamWriter myWriter = null;
try
     {
myWriter = new StreamWriter(resp.GetResponseStream());
myWriter.Write(data);
}
catch (Exception e)
{
Exceptions.Add(e);
return VatCheckOk.NotChecked;
}
finally
     {
myWriter.Close();
}
HttpWebResponse objResponse = (HttpWebResponse)myRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
string result = sr.ReadToEnd();
// Close and clean up the StreamReader
         sr.Close();
}
If i remember well, i used the result string to check for the string: "Yes, Valid number".

Tags: , , ,

.Net | C#

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen