OWASP O2 Platform Blog

Removing C# class from C# file in multiple WSDL files

Today I had a problem which although sounds simple can be quite time consuming and hard to automate.

Basically I was automating the creation of multiple wsdl files (each creating a unique C# file). The problem happens when there is a shared class in those webservices and each of the created wsdl-based-c#-files has a unique version of that class. In my case, the problem was an ‘Credentials’ class which (ironically) is used to authenticate and authorize those webservices.

First I tried the /sharetypes option from wsdl.exe but that wasn’t working, so my solution was to remove that Credentials class from all but one of the created wsdl-c# files.

Since O2 Static-Analysis-Engine has the ability to load , parse and rewrite c# files, with a little bit of code I was able to achive that goal and create a method that is able to remove any class from a C# file:

var topPanel = panel.clear().add_Panel();

Action<string,string>  removeClassFromCSharpFile =
    (file, classToRemove) => {
                                "removing class '{0}' from file '{1}".info(classToRemove, file);
                                var astCSharp = new Ast_CSharp();
                                astCSharp.createAst(file.fileContents());
                                var specials = astCSharp.Parser.Lexer.SpecialTracker.RetrieveSpecials();
                                astCSharp.Parser.CompilationUnit.Children.RemoveAll((child)=> child.typeName() =="TypeDeclaration" &&
                                                                                    (child as TypeDeclaration).Name == classToRemove);
                                astCSharp.mapAstDetails(astCSharp.Parser.CompilationUnit);  
                                astCSharp.AstDetails.rewriteCode_CSharp(astCSharp.Parser.CompilationUnit,specials);
                                astCSharp.AstDetails.CSharpCode.saveAs(file);
                            };
                           
var testTile = @"C:\O2\_tempDir\6-24-2011\wsdl_tmp45A8\LinqDB.cs";
removeClassFromCSharpFile(testTile, "Credentials");
var codeViewer = topPanel.add_SourceCodeEditor();
codeViewer.open(testTile);

//using ICSharpCode.NRefactory.Ast
//using O2.External.SharpDevelop.AST
//O2Ref:O2_API_AST.dll

for reference here is the script that creates the wsdl-c# files and calls the method show above:

var teamMentor = new API_TeamMentor();
var targetFolder = _UnitTests_Config.Server_SourceCode.pathCombine(@"..\O2 Scripts\UnitTests\HelperFiles\");

 
if (targetFolder.dirExists().isFalse()) 
{
    "Could not find target folder: {0}".info(targetFolder);
}
else
{
    var linkDBFile = teamMentor.webService_LinqDB().wsdl_CreateCSharp(targetFolder);
    removeClassFromCSharpFile(linkDBFile,"Credentials");
    var authenticationFile = teamMentor.webService_Authentication().wsdl_CreateCSharp(targetFolder);
    removeClassFromCSharpFile(authenticationFile,"Credentials");
    var teamMentorSecurityFile = teamMentor.webService_TeamMentorSecurity().wsdl_CreateCSharp(targetFolder);
    removeClassFromCSharpFile(teamMentorSecurityFile,"Credentials");
    teamMentor.webService_OnlineStorage().wsdl_CreateCSharp(targetFolder);  // no need to call removeClassFromCSharpFile since this is the file with the Credentials class
} 
 

June 25, 2011 - Posted by | .NET

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: