Monday, December 28, 2009

IIS 7 – Managed pipeline mode – Global.asax redirect not working

I installed Windows 7 and I really like it. I migrated all my applications from my old machine to new. And my old machine is running WS 2003 [iis 6] and new machine is Windows 7 [IIS 7]. In all applications everything ran successful except one. I implemented exception handling in my application. I have Global.asax page and in the Application_Error event, I log the exception details and redirecting the user to the error page. Whenever there is an exception in my application the page never redirect to the error page, it stays there in the same page with exception details on the screen.

The same code ran very successful on my old server and in Windows 7 it's not. I just started thinking what could be the issue. The analysis went in this way. Code not changed, DB not changed and the only part changed is the IIS. Windows 7 has IIS7. So, The problem would be either the isapi dll or the application pool. So, started researching on those and found interesting points.

In IIS 7, application pool has a separate feature named "Managed pipeline mode". This will tell the pool the pipeline mode. [which is of type sint32 in c# and the possible values for it are 0 and 1.]

  • 0 is managed pipeline runs in integration mode.
  • 1 is managed pipeline runs in classic or ISAPI mode. [IIS 6].

In Classic mode, managed application events are executed by using ISAPI. In Integrated mode, ASP.NET request processing integrates directly into the IIS 7 request-processing pipeline. Integrated mode enables you to configure managed modules for Web sites that are developed with unmanaged code.

So, to work your logic correct, go to your site and see under what application pool your site running. Now, go to that application pool, properties and change the pipeline mode to classic mode. Now, you see everything works fine and the page starts redirecting to error page when any exception in your application.

For more details: http://msdn.microsoft.com/en-us/library/microsoft.web.administration.managedpipelinemode.aspx

Hope this helps and new tip today. Do you like this post?

Sunday, December 27, 2009

SharePoint timer job and settings or configuration file

I got a question that what is the case when a SharePoint timer job needs a configuration file? Because, I really need the configuration settings for a timer job which I deployed. Because I got a requirement where I need to use the web service in a timer job and it needs the httpBindings and the endpoints. I can't place them in web.config of the web site as I can't access it here. But, I can access the web.config file as I discussed it in my previous post. I can get all the bindings and endpoints from web.config and create a c# bindngs object. And for the web service I passed the binding and that didn't solve the problem. Somehow, I got the below exception.

WSE012: The input was not a valid SOAP message because the following information is missing: action.

So, I started thinking in different ways and out of box. Below is the way how I approached towards the solution.

  • Everyone knows that all the timer job runs with the help of the OWSTIMER.EXE.
  • If you take a look at the console application, c# project and if you add the app.config file to the project then in your bin folder, you will see two files like if it is c# project then projectname.dll and projectname.dll.config and if it is console application then projectname.exe and projectname.exe.config.
  • So, what you understood from it? for dll or exe there is associated config file if that project contains app.config file.
  • What will happens when we use the dll or try to run the exe, the related config file also loads into memory and you can use the config file in the dll or exe.
  • There my thought starts and I want to repeat the same concept for the OWSTIMER.EXE too. 
  • Find the location of where OWSTIMER.EXE is reside in. Generally, the location is at "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN\OWSTIMER.EXE". So, go to that location and create a file named OWSTIMER.EXE.CONFIG.
  • Open it in notepad and place all the settings code in it. Save it.
  • Now, it's time to restart the timer service. So, go to services and find SharePoint Timer Service. And restart it.
  • Now, run the timer service and you see everything is running successful. I mean all the settings will be loaded and run.

Note: Try to include all the settings in the config file within these tags.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>

Hope this detailed post will help you to improve knowledge and how to think out of box. Love to hear comments.

Access web.config in SharePoint timer job

In SharePoint customization we have the requirements to write custom code for web parts, timer jobs etc. In this post I want to tell you some interesting things in SharePoint. I have written some timer jobs to deploy into my SharePoint server to match some requirements or to solve some problems.

My requirement is, I want to access the web application or web site configuration [web.config] file in the SharePoint timer job. But, the limitations in SharePoint stops me to access the configuration files in timer job. Below are the problems I faced.

  • SharePoint timer job is running in different process named OWSTIMER.EXE.
  • SharePoint web application or site will run with the help of process W3WP.EXE.
  • So, the application configuration file is associated with the W3WP.EXE, so there is no way to access the web.config file in the owstimer.exe process at all. The context is completely different and out of domain. So, we need to call or access the web.config file explicitly in timer job. How to?
  • Remember, always C# is completely object oriented and you can access everything through objects.
  • Usually the timer job installed as a feature. And all the logic of the timer job will go inside the Execute() method. So, write below statements in that method and access the complete web.config file. Below is the example of accessing appsettings.
SPWebApplication webApplication = this.Parent as SPWebApplication;
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", webApplication.Name);
String appValue = config.AppSettings.Settings["appSettingKey"].Value;

WebConfigurationManager is the class which is in the namespace Systen.Web.Configuration. This way, you can access the config settings from a web.config of a SharePoint web site. Hope you understood this well and please let me know what you think.

How to get or access master page in user control

My requirement is I want to get the reference to the master page and access the public property of a master page in user control. As we know that master page is also inherited from the user control, I can say this is simple and we can easily get the reference.

I am using master page in my web application and all pages are using that master page. And there are user controls in the application and my requirement is how to access the master page property inside a user control. Below is the solution how I resolved the problem.

In user control ASCX file declare the below line to establish the reference to the master page.

<%@ Reference Control="~/DefaultMaster.Master" %>

IN the ASCX.CS file, we need to fill the master page object to access it's properties. For this,

  • Declare a master page class variable as the user control class variable as shown below.

DefaultMaster masterpage = null;

NOTE: Remember the DefaultMaster is the class of DefaultMaster.master class.

  • In Page_Load event of user control, fill the masterpage object as shown.

masterpage = this.Page.Master as DefaultMaster;

Now, you are all set to use master page object and access everything inside it. If you observed, everything is simple object model. I am just accessing the objects. I think, you got better idea and the way I used. Hope this helps and any comments always welcome.

How to get the current row in the grid view command event

This is what I faced a small problem when using GridView. I have a LinkButton element in the gridview and my goal is when user clicks on the button, I need to raise the grid view command event and in that event, I need to get the row values. But, the questions is how to get the current row in the grid view command event?  Below is the solution for it.

GridViewRow row = ((LinkButton)e.CommandSource).NamingContainer as GridViewRow;

Note: Remember LinkButton class I used in the above code is assuming that the command event raised when clicked on the LinkButton control. If your requirement is not linkbutton then please place the corresponding control name.

Most of the times and most of the developers never use the property available to each and every object named NamingContainer. But, there are lot of advantages with this property. Especially when we write and render controls to page dynamically then this property will help a lot. And there are many properties which will help us to solve so many problems. Use the intellisense and try to know most of the properties available for a .NET control.

Hope, this will help you and you got what you need. Please let me know what you think on this post.

C# display date time with milliseconds

By default, in c# DateTime object will display the values in the 12 hours format with AM and PM. Which don't include the milliseconds. But how to display the date and time with milliseconds? Use the below format to display it.

DateTimeObject.ToString("yyyy-MM-dd HH:mm:ss.fff")

f is for the format for printing the milliseconds. Remember when you try to convert the date time with milliseconds back to C# Date time, then just use Convert.ToDateTime().

Hope this helps.

Request format is unrecognized for URL unexpectedly ending in methodname

This is the exception I am getting when I try to access web services from my web site. I just created a web service and tried to access the service from the browser using the get request. But I got the invalid operation exception. See complete exception details below:

System.InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/MethodName'

So, Before we will go to know the solution for it, I want to explain the reason behind this.

A .NET web service supports three protocols named HTTPGET, HTTPPOST and SOAP. When .NET released I mean, ASP.NET v1.0 all these three protocols enabled by default. So, any requests which made from the browser which belongs to a web service will be responded without any issues. But when come to ASP.NET 1.1 version the protocols HTTPGET and HTTPPOST are disabled by default. The only reason is SECURITY issue. So until you explicitly enable them no web service request will be processed if you call them in the format domain/webservice.asmx/methodname [Get/Post].

But, where as if you call direct web service from browser using SOAP request that will work and will give result what you need. But, if you make any GET or POST request then only the problem will come. 

RESOLUTION:

To enable these protocols in the application level simply we need to change the application config file. Web.config. Add below lines to the web.config to enable these protocols.


    
    
        
            
            
        
    
    

So, this webservices tag will go inside the <system.web> namespace. With the above lines, the protocols enabled and they listen the request and respond accordingly for that application. Sometimes you may need to enable these protocols in system level. I mean to all applications in the server should have these protocols enabled. Then below is the solution for it.

Find machine.config file and open it in nice editor like visual studio. Before do any changes please take a backup of the file. Now find the protocols section and add the below lines if they are not already there.






Hope, you understood it well and you are now knew the reason behind of the above exception message. Please know me the feedback.

Friday, December 11, 2009

Could not load type Microsoft.TeamFoundation.WorkItemTracking.Client.workItemTypeDeniedOrNotExistException

This is the exception we are getting when we try to view the workitems from the Team explorer in visual studio. The problem is, you installed the team explorer after you installed the Visual studio 2008 SP1. Because of this, all the workitem related dll’s are not referenced correctly. And result is below exception message.image

Resolution:

To fix this issue, we just need to reinstall the visual studio sp1. So, the order should be

  • Visual Studio
  • Team Explorer
  • Visual Studio SP1

Hope this helps to the people who are facing the same issue.