"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

March 16, 2012

Tool Developer Notes - Part IV

[Previous Post in Series - Tool developer notes part III]
Tip #1 - Initially I used to read IP address from App.config File for my C# Application. Based on feedback I moved the URL assigment in implementation. Only IP Address and Port would be read from App.config

While trying to compile below is error. Error - C# + System.UriFormatException: Invalid URI: A port was expected because of there is a colon (':') present but the port could not be parsed.
Below link was useful for the answer. Let's try out that answer

Step 1 - A simple Windows Forms App

Under App.Config App setting add following keys

<!--IP Address-->
<add key="IPAddress" value=""/>
<add key="Port" value="1433"/>

Step 2 - Add Windows Configuration Reference and System.Web Reference

Step 3 - For the button click add below code in the method

string IpAddress = ConfigurationManager.AppSettings["IpAddress"];
string Port = ConfigurationManager.AppSettings["Port"];
string TestUrl = string.Format(@"http://{0}:{1}/test/services/testnode/", HttpUtility.UrlEncode(IpAddress),
Uri U = new Uri(TestUrl, UriKind.Absolute);

Using HttpUtility and Uri for setting up URL fixed the issue. If the URL is relative you need to use relative option while setting up new uri (uniform resource identifier). Also note there is a double '//' after the http. This is also very important.

Tip #2 - Log4Net logging had duplicate entries created in logs. This was due to configuration setting in app.config file. Stackoverflow answer was useful to correct duplicate logging issue. Lets try a sample code for Log4Net logging. Earlier example please check in link

Modified App.Config File

<?xml version="1.0" encoding="utf-8" ?>
<sectionname="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
<appSettingsfile="" >
<addkey="log4net.Internal.Debug" value="false"/>
<!-- This section contains the log4net configuration settings -->
<appendername="LogFileAppender" type="log4net.Appender.FileAppender">
<layout type="log4net.Layout.XMLLayout" /> -->
<param name="File" value="Log4NetExample.log"/>
<param name="AppendToFile" value="false" />
<layout type="log4net.Layout.PatternLayout">
<header type="log4net.Util.PatternString" value="[START LOG] %newline" />
<footer type="log4net.Util.PatternString" value="[END LOG] %newline" />
<conversionPattern value="%d [%t] %-5p - %m%n"/>
<!-- Setup the root category, add the appenders and set the default level -->
<level value="INFO" />
<appender-ref ref="Console" />
<appender-ref ref="LogFile" />
<!-- Specify the level for some specific categories -->
<loggername="log4NetExample" additivity="false">
<!-- <appender-ref ref="B" /> -->
<level value="INFO" />
<appender-ref ref="LogFileAppender" />

This config changes to previous example fixed the duplicate logging issue. I still need to explore all the Log4Net settings. Hoping to try them out in next set of posts.
Tip #3 - This is for error - "The AXIS engine could not find a target service to invoke!  targetService is ABC/". When I was trying to invoke a service through proxy code i received this error. The reason is there was a front slash '/' which need to be removed to fix this issue. There were so many answers provided for this error. Hint to remove slash was useful from answer

Tip #4 - Read Text from a File and Encode it in UTF8 format

Tip #5 - Reading a File till end

Tip #6 - Why do I get the error "Object reference not set to an instance of an object"?

Happy Learning!!!

No comments: