Friday, May 29, 2009

Disabling Enhanced Security in IE 8 on Windows Server 2008

I have to install some pre reqs on windows server 2008 and i started with browsing the intranet sites to find the pre reqs.
Now the problem on a windows server 2008 is that all kind of security features are installed by default and hence none of the intranet sites were working. I am unable to download any file either.
The quick fix for this is to disable enhanced security feature of IE8 on Windows Server 2008. And this can be done by going to
appwiz.cpl -> Turn Windows Features on or off-> Security Information snap in-> Configure IE ESC
and disable it.

And now the IE works normally. Its a good trick to work in development environment and definitely not recommended on a production server .

Happy coding :)

~Abhishek

Wednesday, May 20, 2009

Resgining a 3rd Party Assembly

Many a times we've to ship 3rd party source code alongwith our code. The catch comes when the 3rd party assembly is an unsigned assembly while you have a strong need to sign the assembly being shipped by you.
If you try to refer an unsigned assembly into an assembly which is being signed then you get an error which says "Assembly generation failed Referenced assembly ‘xxx’ does not have a strong name"

The easy solution to this problem is to resign the 3rd party assembly using your key.
You can fing out how to do it on the blog by allkampfer on this link.

Happy signing :)

~Abhishek

Friday, May 15, 2009

params keyword in C#

Its strange but true that i got a requirement for the first time today where i wanted to pass variable number of parameters to a method i was writing in C#. Now i always knew that its possible however as soon as the requirement struck i just didn't have a clue about how to achieve this.
And so i hit the msdn and boy its so simple.
You just need to use params keyword to achieve this

So a method which needs multiple parameters of different type can be written as

public static void MethodName(params object[] parameters)

And it just works :)

Happy coding....

~Abhishek

Thursday, May 14, 2009

Using Fusion Logs to debug the assembly binding failures

While working with WCF i wrote a custom Client Message level interceptor and as usual WCF tries to load the assembly containing the interceptor type using reflection.
While doing this WCF runtime threw an exception which suggested that the Assembly Binding failed however no information is supplied regarding why the binding is failing except that it failed with HRESULT 0x80131040
In order to find the exact reason we can enable the fusion logs and check the actual binding steps going on in order to find the actual problem in binding
To do this following steps should be used to check the binding process
1) Make a DWORD entry to the Registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\FusionEnableLog and change its value to 1
2) Open the fusion log viewer fuslogvw from the command prompt
3) In the settings of log viewer select the option to log the binding failures to disk
4) Restart the process which is having binding failures
5) Refresh the log viewer and you'd see the exact reasons why the bindings are failing

Happy coding :)

~Abhishek

Tuesday, May 5, 2009

System.CodeDom.GeneratedCodeAttribute for suppressing the generated code warnings

For one of the utilities i am writing i am using the resx file to store various blocks of code and generating a csharp class file based on a xml file. Because of dynamic generation of blocks i am not able to indent the code properly and hence when i compile this code i obviously get style cop warnings in my project.
When i investigated further i saw that style cop warnings are not displayed in the designer files generated by Visual Studio for ASP.NET and WinForms application. One of the differences i found in the files generated by Visual Studio and files generated by my utility was that visual studio puts a tag <auto-generated> in the file header while i put a tag called <autogenerated/> in mine.
Any ways changing this didn't affect it at all and the warnings were still generated in the compilation log.
So i put a System.CodeDom.GeneratedCode attribute on my class and bingo all the warnings were happily ignored by the compiler.

So if you are writing any tool to auto generate the code use this attribute to suppress all the warnings from the auto generated code. Of course you need to be sure that warnings are not harmful and are ignorable warnings (In my case they were all stylecop warnings and no fxcop warnings)

~Abhishek

Style cop and helper utilities

One of the basic necessities when you are working on a project is that the style cop warnings in your code should be zero. If there is a single style cop warning in the code one can run into several issues like
1) I don't like it when there's a warning in the Visual Studio Compile log
2) I don't like it when my build log on the build server becomes several hundred kbs just because of style cop warnings and to find wh

For the records style cop is a visual studio plug in which ensures that the code is written in a consistent way i.e. tabs, indentation, regions etc. are defined very clearly in the code. It can even be integrated into msbuild so that the build points out the errors and warnings.It can be downloaded from msdn if you want it for your project.

Once you start working with Style Cop you'd find it difficult sometimes to take care of every warning on yourself. The trick is to do the hard work early on by fixing each and every warning manually and then slowly you'd become habitual of writing the style cop compliant code.

Here are a couple of utilities which can help in case the warnings are huge and there's a time constraint on delivering the code

1) NArrange :- Takes care of beautifying the code. It can be downloaded from here. Use it carefully for the existing files as it rearranges the whole file according to style cop rules and more. It clearly demarcates the regions for fields, methods etc. The catch is that if you already have a version of a file in your source control the tool will rearrange the code in such a way that diffing the file against the version in source control is not going to help much.

2) GhostDoc :- Its an intelligent documentation tool which generates the documentation for the methods based on parameters. It can be downloaded from here. Most of the time if you are using the proper naming conventions the documentation generated by this tool works pretty well. However some time you might want to review it. What's more it can even take your customizations done on a method in interface to the class automatically.

So use these cool utilities and write your code using style cop.
Happy coding....

~Abhishek

Monday, May 4, 2009

Bad Request in WCF REST services

While working with WCF services we tend to fall on FaultException or FaultException class to send the errors occuring on WCF server to the WCF client.
Now this works with most of the bindings on the WCF framework. However if one is using WebHttpBinding in order to expose the service as a REST service the FaultException or FaultException doesn't work. i.e. if you throw this exception from the server the client gets a Bad Request error. Now this is funny and confusing. I was testing my service by sending raw xml in http requests and when i saw a Bad Request Error i was investigating the schema of the request instead of debugging the service.
After checking the schema many times i finally changed the data in the xml and bingo it worked. And that kind of made me understand the any exception on the service results in a Bad Request exception in the client side.
Now this is not the way we'd like our exceptions to go from server to client. We want to send more specific details about what happened so that the client can take appropriate corrective measures.

One way to achieve this is to define a ResponseStatus enum and include a value of enum in the ResponseMessages from the Service Operations. This enum will keep growing as we want to send more details about the exception and then instead of throwing an exception or fault from the service set the responseStatus in the Response message which the client can use in order to take corrective actions. This obviously is an overhead since this is not the way we'd like to communicate exceptions while using Non WebHttpBindings.

Another way could be to use extensibility features of WCF to set the http status code and status description in the http response. We can intercept the response from the service just before it is sent to the client and set the details appropriately. I want to explore this option in detail if possible however for now i'd go with first approach as my customer has asked me to provide a REST service by today evening :).

~Abhishek

Unable to see the $Exception details in Visual Studio

I was away from coding in Visual Studio 2008 for a while, instead i was using it for drawing class diagrams and reading some code.
While i was doing that i disabled some package because of an error and then as it turned out i was not able to use the Exception details view in the Visual studio. i.e. If i ask visual studio to break if an exception occurs by going to Debug -> Exceptions it breaks but doesn't show me the details of the exception which occured.
As soon as this happened i went to Visual studio output window and saw a message in the general category which said that Visual Studio is unable to load package "8D8529D3-625D-4496-8354-3DAD630ECC1B" and i should use "devenv /resetskippkgs" to load the package.
As i tried to do that the package still failed to load without giving any reason as to why its not loading the package.
On doing some searches i figured out the problem happens many a times in Visual Studio 2008 and the solution is to repair the .NET Framework 3.5 SP1 from Add Remove Programs snap in.
Once the repair is done use resetskippkgs option to start Visual Studio and the problem is resolved.
I am not sure if this is the most efficient way to solve this problem but it works nonetheless :)

~Abhishek

Friday, May 1, 2009

Consuming WCF services on SSL

Okay so its been ages since i posted something here and just now i struggled with a problem which i struggled with so many times during the past 2-3 months. So i decided to document the solution here as it might help someone else or me to fix this sometime in future :).
So I am working with a WCF service exposed over IIS with SSL binding.
I setup a WCF client for consuming this service and everything was supposed to work out of the box. However there are hiccups which one faces. And the worse is that you start getting security exceptions which give hardly any information about what the problem could be. All i was able to understand was that there's a problem establishing a secure connection over TLS/SSL.
So here are a few troubleshooting steps
1) If its the development/test environment and you are using a SSL certificate issued by an internal authority then ensure that you have the root certificate authority installed in your trusted authoritites store. (In a typical development environment certificates are stored on a shared folder and all the people in team use different copies of certificates from this shared folder. Sometimes certificate authority certificate has versions and the one you trust is a different version itself). So the safest option is to hit the service from IE. Check the certificate , export the root certificate authority in a .CER file and install it in your local machine store. Byu default the wizard will install it in your personal store though its a good idea to copy it to local machine store because you might be using some other account to consume this service.

2) Another problem one faces quite often in the development environment is that the SSL certificate is issued to a specific machine while multiple developers use the same certificate to work in parallel. In such a case the consumer of this service might face an exception as the name of the machine to which the certificate has been issued is different then the name of the machine it is being presented by. To skip this check we can configure the ServicePointManager component in .NET to skip the name verification process. To do this add the following section to your config file for the WCF client....







I'll update this post as i face more issues with SSL......

~Abhishek

PS :- I am working on a reusable Performance counter application block which i might publish on this blog in some time.