Prash's Blog

Could not find default endpoint element that references contract in the ServiceModel client configuration section October 3, 2012

Filed under: Uncategorized — prazjain @ 2:23 pm

I get this error when testing one of my methods that makes WCF service calls, and at service invocation I get this error :

Could not find default endpoint element that references contract ‘XYZ.IMyInterface’ in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

This happens because the wcf service configuration is possibly missing from app.config file of the test project.
So add service configuration from app.config of the wcf library project and copy it to your test project.

This can also happen if WCF service is in a dll, and that dll is being invoked from another startup project, so then the config will need to be in app.config of the startup project.

 

How to setup a Command Prompt Here in Windows context menu October 1, 2012

Filed under: Uncategorized — prazjain @ 9:55 pm

I find it really useful to have an option in windows context menu (right click menu) to open command prompt with present directory as its current directory, so no need to open command prompt and change dir.
This is how to set it up, create a .reg file with following contents :

[HKEY_CLASSES_ROOT\Directory\shell\Command]
@="Command &Prompt Here"
[HKEY_CLASSES_ROOT\Directory\shell\Command\command]
@="cmd.exe \\\"%1\\\""

Now double click on the file, this will enter the settings in registry, and you are ready to use the shortcut.

 

Unable to bind parameter id that is oracle guid type raw using ODP.Net September 28, 2012

Filed under: Uncategorized — prazjain @ 7:10 pm
Tags: , , , ,

I have had this problem of trying to save a GUID in oracle as type RAW.

When using oracle version 11.2.0.3, there was no problem saving it the way it was.

Working version for 11.2.0.3 :

cmd.Parameters.Add(":id", OracleDbType.Raw, ourCombinedList.OrderBy(x => x.ID).Select(e => e.ID).ToArray(), ParameterDirection.Input);

But when we downgraded to oracle version 11.2.0.2, this threw an error, saying unable to bind parameter id (which was a type GUID) to oracle raw types.

Workaround version for 11.2.0.2 :

cmd.Parameters.Add(":id", OracleDbType.Raw, ourCombinedList.OrderBy(x => x.ID).Select(e => e.ID.ToByteArray()).ToArray(), ParameterDirection.Input);
 

Querying XML with XPath in C#

Filed under: Uncategorized — prazjain @ 6:49 pm
Tags: ,

There have been so many times I had to resort to string comparision on XMLs, but this a handy article to using XPath for querying and even comparing values in XML through C#.

http://support.microsoft.com/kb/308333

http://www.codeproject.com/Articles/52079/Using-XPathNavigator-in-C

 

BadImageFormatException : An attempt was made to load a program with an incorrect format.

Filed under: Uncategorized — prazjain @ 9:00 am
Tags:

System.InvalidOperationException : Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.

System.BadImageFormatException : An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

This is the error I got when running nunit tests in my 64 bit Windows 7 machine.
I had 64 bit VS 2010, 64 bit Oracle installed.

I had nunit plugin for Visual studio, and I could right click on a test project and select “Test with NUnit”, but this was always loading up nunit-86 which is 32 bit version of nunit.
This meant it was loading up 32 Oracle client lib.

So I added nunit/bin dir to my path and loading nunit.exe from bin/debug of the test project dir and ran the dll in 64 bit nunit exe and lo and behold my tests pass!

 

Mocking internal methods with Moq in C# September 25, 2012

Filed under: Uncategorized — prazjain @ 12:20 pm
Tags: , ,

I have been using Moq for some time now and love it, but had one frustrating nag, that I could not moq internal methods for some reason.
But after several frustrating hours of figuring out how to moq internal methods in moq. This is how I did it.

In the assemblyinfo.cs file for the project (that has the source you want to test).
Add a line as below :

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

Interesting thing to note here is that to mock internal method it must be virtual. eg:

internal MI5Employee Search(string name)
{
    // other methods like this one will call the virtual method, so the virtual method can be mocked when this method is being tested.
    int id = GetEmployeeId(name);
    // next id is used below to do some other work.

   return myEmpObj;
}
internal virtual int GetEmployeeId(string name) { .. }

So for a method like this in Moq, you can mock it like this :

var mi5EmployeeMock = new Mock<MI5Employee>();
mi5EmployeeMock.SetUp(mi5EmployeeMock.GetEmployeeId("prash")).Returns(007);
// now the method is setup and any method you are testing that makes this method call internally then it will return 7. 
MI5Employee emp = mi5EmployeeMock.Object.Search("prash");
// this test will pass
Assert.IsTrue(emp.Id== 7); 

Now the tricky part is, if your class does not have an interface and you have to test the same method.

var mi5EmployeeMock = new Mock<MI5Employee>();
// lets assume all its dependencies are set up here
int res = mi5EmployeeMock.Object.GetEmployeeId("prash");
// this test will fail
Assert.IsTrue(res == 7, "This test fails to find James Bond!");

Here the test will fail, as res will have value 0, that is because the method GetEmployeeId is set as virtual and Moq cannot test virtual methods, it just will not call its actual implementation.
So though we are able to unit test Seach method we are not able to unit test GetEmployeeId method using Moq.
The way I got around it is by having another implementation that is non virtual and calling that from the virtual method. eg.

internal MI5Employee Search(string name)
{
    // other methods like this one will call the virtual method, so the virtual method can be mocked when this method is being tested.
    int id = GetEmployeeId(name);
    // next id is used below to do some other work.

   return myEmpObj;
}
// this method is used for mocking and called directly by other methods.
internal virtual int GetEmployeeId(string name) 
{
   return GetEmployeeId_NonVirtual(name);
}
// this method exists solely for testing GetEmployeeId logic using Moq.
internal int GetEmployeeId_NonVirtual(string name) { .. }

So our test case will look a bit like this :

var mi5EmployeeMock = new Mock<MI5Employee>();
// lets assume all its dependencies are set up here
int res = mi5EmployeeMock.Object.GetEmployeeId_NonVirtual("prash");
// this test will pass
Assert.IsTrue(res == 7);

This may not be elegant but it works and I get test all my code. Green is Good.

 

Linux Cron Job to Delete tomcat temporary files July 22, 2012

Filed under: Uncategorized — prazjain @ 4:37 pm
Tags: , , , ,

 

Every once in a while my tomcat temporary directory will grow in size by few GBs and I had to manually delete files from server. This also meant that I have to continously monitor my server which can be annoying during holidays. So I wrote a cron job to delete these temporary files :
Create a shell script in your home directory (lets assume it is /root for now):


Files=`ls --sort=t /opt/tomcat-7/temp`</pre>
for filename in $Files
do
if rm "/opt/tomcat-7/temp/$filename"
then
 echo "Successfully deleted : /opt/tomcat-7/temp/$filename "
else
 echo "Could not delete file : /opt/tomcat-7/temp/$filename "
fi
done

The script first executes the ls command to get the list of file in the directory, sorted by their creation time.
Next we loop through the file list in the for loop.
For every file we use the if condition to check if it could be deleted or not, and according echo the response.

Now we create an entry in cron tab file so that scripts are executed every hour.

crontab -e

Press i to go the insert mode.
Enter the following content in the editor :

0 * * * * sh /root/rm_tomcat_temp_files.sh > out.log 2> err.log

Press Esc, Shift + : + x to save the changes.

This entry create a job which executes the sh with the given shell file as arguments and stdout and stderr are redirected to files for better logging of what is happening.

 

Error Import Apex Script : ORA-00001: unique constraint (APEX_040100.WWV_FLOW_ICON_BAR_PK) violated June 13, 2012

Filed under: Uncategorized — prazjain @ 2:50 pm
Tags: ,

This is the error I got when I tried to import my Oracle Apex script from DEV environment to QA environment.

Error GET_BLOCK Error.
ORA-20001: Execution of the statement was unsuccessful. ORA-20001: Error creating navigation bar item id=&amp;quot;246130155962013305&amp;quot; ORA-00001: unique constraint (APEX_040100.WWV_FLOW_ICON_BAR_PK) violated &lt;pre&gt;begin wwv_flow_api.create_icon_bar_item( p_id =&amp;gt; 246130155962013305 + wwv_flow_api.g_id_offset, p_flow_id =&amp;gt; wwv_flow.g_flow_id, p_icon_sequence=&amp;gt; 200, p_icon_image =&amp;gt; '', p_icon_subtext=&amp;gt; 'Logout', p_icon_target=&amp;gt; '&amp;amp;LOGOUT_URL.', p_icon_image_alt=&amp;gt; 'Log

To resolve this error, open the script file in any editor like notepad, notepad++ etc.

Search for “wwv_flow_api.g_id_offset” and it might be set to 0, so change it to 1.

If this is still not resolved change it to 2. (Keep incrementing by 1 until it this error is resolved and you are able to import your apex script).

 

SVN password lost – recovery May 19, 2012

Filed under: Uncategorized — prazjain @ 12:55 pm
Tags: , , , , ,

This is what I did when I lost password for my svn repositories, though it was cached in memory by Tortoise SVN, but when I added a new plugin for Eclipse I could not recall the password.

In situation like this you can use this tool to recover cached passwords for svn repository http://www.leapbeyond.com/ric/TSvnPD/.

 

SVN Working Copy text base is corrupt – Checksum mismatch – Try cleanup

Filed under: Uncategorized — prazjain @ 10:39 am
Tags:

I have noticed this error crop up a few times randomly when I am about to check in my file changes in SVN. I have a SVN server on my home machine, and I am the only one checking in code in that code repository. I have Visual SVN server and Tortoise SVN client installed.

I was able to narrow it down to the fact that I did refactoring or search and replace by Eclipse in my xml files. Both the times the xmls were fine per-se but Tortoise SVN complained about checksum mismatch.
So if you are doing refactoring or search and replace in IDE then be sure that you will also get this error.
To resolve it first keep a backup of the new modified file that you want to check in, then to commit changes there are two ways :

  • Delete the file from the repository on server using repo-browser. Update the directory on client local machine so the file will be removed. Now use the backup file to copy over the file again and add it into the repository again.
  • Another way to do it, revert the changes on the file using tortoise svn, then go to parent directory and do a cleanup on directory (select the option to revert recursively, delete unversion and delete ignored files).
 

 
Follow

Get every new post delivered to your Inbox.