Monday, September 5, 2011

WCF Exception "The login is from an untrusted domain and cannot be used with Windows authentication."

Today, when I was trying to test a WCF web service from a test harness application, I faced an exception "The login is from an untrusted domain and cannot be used with Windows authentication.".

My WCF service is a simple web service that will query a MSSQL database using Windows Authentication and return a collection of object to the caller. The service is hosted locally at my development machine IIS, while the MSSQL server  is at a different development server. Both are from same domain. After searching through the web for some times, I came through a blog post at here http://blog.mustoverride.com/2009/03/wcf-impersonation.html which exactly solved my problem. Credit to the author and thanks for the explanation.

From my understanding, the solution is quite straight forward. Basically, we need to:
1. Make sure the IIS user has the sufficient rights to access the database server or database.
2. Configure WCF to run in ASP.NET compatibility mode by setting aspNetCompatibilityEnabled property to true in WCF config file.
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
3. Impersonate the user in IIS from our WCF by adding identity tag in WCF config file:
<system.web>
<identity impersonate="true"/>
4. In WCF Service Contract implementation class, add the following statements to "activate" your WCF to run in ASP.NET compatibility mode:
Imports System.ServiceModel.Activation


<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class MyService
    Implements IMyService

However, this problem only happened in my local development machine. When I tried to host the application in a web server and consumed it, there was no problem at all! So, this solution is to serve as a guide for those who need to test and debug their WCF web service at their own development machine.

Once again, thanks to the author for the great post. =)

Saturday, July 30, 2011

Error Hosting WCF In IIS: HTTP Error 404.17 - Not Found - The requested content appears to be script and will not be served by the static file handler.

I was facing above error when trying to host a WCF application in IIS7.5. Tried few suggested solutions from google searches but they just don't work out, until I came across a post in MSDN forum. The link is at below:
http://social.msdn.microsoft.com/Forums/en/adodotnetdataservices/thread/f447c6df-6402-4a13-875c-925445fd7be8

According to the post, the script map for the .svc extension are not registered. Navigate to the WCF folder in .NET v3.0 folder to register it and it solved my problem.

Thursday, July 7, 2011

Report Viewer - Hiding Export Format

Sometime, we will need to hide the export format (excel or pdf) from particular user group. There is a great article explaining how we can do that, which you can find here. Many thanks to the author Ahmed Elbaz for providing such useful solution.

The code snippet in that article is written in C#, so for VB.Net developer like me, I will need to convert the code to VB.Net in my project.

Below are the code I converted to VB.Net using online code conversion tool. Hope it helps!


Public NotInheritable Class ReportViewerExtensions
    Private Sub New()
    End Sub


    Public Shared Sub SetExportFormatVisibility(ByVal viewer As ReportViewer, ByVal format As ReportViewerExportFormat, ByVal isVisible As Boolean)


        Dim formatName As String = format.ToString()


        Const Flags As System.Reflection.BindingFlags = System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.[Public] Or System.Reflection.BindingFlags.Instance
        Dim m_previewService As System.Reflection.FieldInfo = viewer.LocalReport.[GetType]().GetField("m_previewService", Flags)


        Dim ListRenderingExtensions As System.Reflection.MethodInfo = m_previewService.FieldType.GetMethod("ListRenderingExtensions", Flags)
        Dim previewServiceInstance As Object = m_previewService.GetValue(viewer.LocalReport)


        Dim extensions As IList = DirectCast(ListRenderingExtensions.Invoke(previewServiceInstance, Nothing), IList)
        Dim name As System.Reflection.PropertyInfo = extensions(0).[GetType]().GetProperty("Name", Flags)


        'object extension = null;
        For Each ext As Object In extensions


            If (String.Compare(name.GetValue(ext, Nothing).ToString(), formatName, True) = 0) Then
                Dim m_isVisible As System.Reflection.FieldInfo = ext.[GetType]().GetField("m_isVisible", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance)


                Dim m_isExposedExternally As System.Reflection.FieldInfo = ext.[GetType]().GetField("m_isExposedExternally", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance)
                m_isVisible.SetValue(ext, isVisible)
                m_isExposedExternally.SetValue(ext, isVisible)


                Exit For


            End If
        Next
    End Sub


End Class


Public Enum ReportViewerExportFormat
    Excel
    PDF
End Enum

Saturday, June 18, 2011

ClearCase - Promote Files From Sub Branches to Main Branch

ClearCase is a source control management (SCM) tool from IBM. I use ClearCase in the company that I'm working on right now. There are many things to learn for ClearCase and I find that it is so complicated. We have to deal with the cleartool commands most of the time to get something done.

There are times where we need to promote the codes from our development branch to the main branch, especially after the codes go into production. The purpose of doing so is ensure that all the related source files from development branch are "copied" to the main branch and the changes are visible in the future branches sprout out from main branch. We just don't want to miss any changes. Besides that, having the changes promoted to main also means that, the config spec of the future branches will be much more cleaner and will not dependent on the past release branch.

The promote to main process can be automated by using a series of cleartool commands. There is an article in IBM website that guide us how to do so, which you can refer it here. However, when we did it for my company, we found out some issues (perhaps it is more suitable to say, need some enhancement) on the cleartool commands provided from that article. The problem is when we have some files with very large version tree that never promote to main before. New branch sprout out from previous development branch and it keeps going on like that for few years. For this kind of files, we encountered failure in copying the files from development branch to the main branch. It failed at the native windows copy command. This is because, when the file's version tree grows huge, the version extended pathname also grows. When the path grows exceeding the pathname limitation of windows, the copy command will fail.

To prevent this, we can simply replace the windows copy command to use cleartool get command. However, there are some limitations using get command. The command only support static / snapshot view and it can only be used to copy file elements. Here's an example of using the get command in windows environment (make sure you run the command in the root directory of your clearcase view):






What the command above does is to first, find a file that we want to perform the "get" operation using the cleartool find command. After that, perform the get command towards the target file.

There are 2 ClearCase variables being used here, which are: %CLEARCASE_PN% and %CLEARCASE_XPN%. The first variable represents the file(s) on the branch your current view is pointing to (we usually set the current view to main branch for this purpose), while the latter, represents the development branch where you would like to get the file(s) from.

From my experience on this, it tells me that we should always plan, control and manage the source repository carefully which will save us a lots of time solving unforeseen issues.

Converting .dmg to .iso

I think those who familiar with Mac OS will know what a .dmg file is. It is 1 of the file type in Mac OS environment that is similar to windows .iso file.

There are several tools out there to convert the file type from .dmg to .iso. I found one tool that I think is reliable (at least I tried converting 2 .dmg files to .iso files and they worked well) and it's name is dmg2iso.

Here's the link on how to use that tool http://www.blogsdna.com/3183/convert-dmg-to-iso-with-dmg2iso.htm

You will need to run the tool from command prompt and passing in some valid arguments. That's not a big problem for a IT guy I think. Cheers~

Friday, June 17, 2011

Report Viewer - Problem Showing Chinese Characters on PDF

One of the common problem on Report Viewer is showing unicode characters on the pdf report. Instead of showing the correct characters, the report will show question mark (????) for the unicode characters. For me, I need to display chinese character in the pdf report. There are 2 steps required to get it done:

1. Install East Asian Languages on the server  (I'm not 100% sure though, whether client's machine also need to install it. ). Server restart is required after the installation.

2. Change the font type of your report / textbox in your report that will need to display unicode characters. I tried several chinese fonts but so far, only 1 works for me, which is DFKai-SB.

Hope this helps. Happy programming!