Tuesday, August 31, 2004

VJs Tip Of The Day - August 31st 2004

COM Callable Wrapper(CCW) - In simple terms -CCW is the special wrapper created over your .Net classes so that they appear as COM objects to other COM components... If your .Net application needs to be called by other COM components you need to make sure that CCW is created over your .Net classes...

Runtime Callable Wrapper (RCW) - In simple terms - RCW is a special wrapper created over COM components so that they appear as .Net components to other .Net components... If your .Net components call other COM components then you need to make sure that RCW is created over the COM components that your application calls...

PS: I will be shifting to Milwaukee, WI tomorrow... Till I get my new home set up, I might not be having any access to internet so probably would not be able to send in Tips for a while... I apologize in advance for the same and will try to get back to grove ASAP...

Monday, August 30, 2004

VJs Tip Of The Day - August 30th 2004

Maintaining State Across ASP.Net Pages - Continued 4

Due to the weekend you might have lost touch... In that case do browse through the previous tips on the blog site... The current mail is in continuation of previous tips...

There is a possibility of InvalidCastException, which may come in case of using properties with Server.Transfer... If you think for a while you can very easily conclude the possible causes...
1.) SecondPage is called from a page other than the FirstPage
2.) SecondPage is directly loaded
3.) SecondPage is called from any other page using response.redirect

In such a scenario do make sure that you catch this exception and ignore it...

PS: You know blogspot has provided search facility for the blogs... So now if you want to search some specific tip in my blog you can use that search facility and you will get the tip right over there... :-)

Friday, August 27, 2004

VJs Tip Of The Day - August 27th 2004

Maintaining State Across ASP.Net Pages - Continued 3

Yesterday we discussed on NOT having second parameter to be true in Server.Transer() method while transferring data of first page to next page using properties... Visit the previous tips to follow up...
Now the answer to "WHY" not true... Well, I was trying that out yesterday and I got an exception message:
"The View State is invalid for this page and might be corrupted"...

On exploring more I discovered that: It is that "EnableViewStateMac"* property of the page(which we discussed few days back)is set to true by default... If this property is true ASP.Net makes a check to find out if the viewstate was modified or not when the page was posted from the client... Now as in case of Server.Transfer's second parameter being true what happens is that all the form variables (including viewstate) of the first page is preserved and sent to second page... Here due to EnableViewStateMac being true a check is made by ASP.Net but now as you are no more in first page so it is but obvious that this check will fail... And as in case of properties option without setting second parameter of Server.Transfer as true also the values are transferred so you can as well pass it to be false...
As such you can also make "EnableViewStateMac" as false but that would not be a good idea from security perspective...

*MAC stands for Message Authentication Check

PS: You know Sudhakar Sadasivuni, a fellow MVP has helped to get all our MVP blogs at one place at the site http://www.mvpblog.com... I bet you will find loads of information here so do visit it when you find time...

Thursday, August 26, 2004

VJs Tip Of The Day - August 26th 2004

Maintaining State Across ASP.Net Pages - Continued 2

Hi Folks,
I apologize for abruptly stopping the tips... I was not having any access to internet for the last few days... I am shifting to Milwaukee, Wisconsin to join in as a .Net System Architect in an MNC... I will remain busy during this transition but will ensure that I do not display this abrupt behavior again... The tips of Aug 23rd, 24th and 25th are up there on my blog site(Click Here) The current tip is in continuation of the previous tips so please do visit the site and catch up before you move ahead...


4.) Using Properties with Server.Transfer: Make your first page in such a way that you expose the required values in next page as properties in the first page... Then use Server.Transfer to go to the next page(keep the second parameter as false here, WHY SO WE WILL TALK TOMORROW)... Now in the next page using context handler you can get all the properties of the first page...

I am not writing the first page code as it is just simply adding values into properties and calling Server.Transfer and I believe you all know how to do that...

CODE SNIPPET

NextPage Code:

if(Context.Handler != null)
{
string strInternalVariableOfNextPage;
FirstPage objFirstPage = new FirstPage();
objFirstPage = (FirstPage)Context.Handler;
//Note that SomeValue is a public property of First Page
strInternalVariableOfNextPage = objFirstPage.SomeValue;
}
PS: I like receiving mails that blast me on not sending the tips... This keeps me motivated so thanks to all those who wrote in those mails...

Wednesday, August 25, 2004

VJs Tip Of The Day - August 25th 2004

Maintaining State Across ASP.Net Pages - Continued 1

3.) Using HttpContext/RequestParams with Server.Transfer: We discussed adding to session yesterday, similarly you can add to HttpContext as well and this eliminates the cleaning code required in the next page like the way it is required in Session... There are other advantages too...

If you use Server.Transfer then all your control values are automatically transferred to the next page you do not have to add anything to the context explicitly here rather just access the values in the coming page... The code example will show this more clearly...

If you have certain values other than the above mentioned then you have to do certain special operations to explicitly add them in the first page and retrieve them in the second...

CODE SNIPPET

FirstPage Code:
//These are the special values being added to context
//we will retrieve them in the next page
context.Items.Add("specialVariableName", "specialVariableValue");
//We set the second parameter of Server.Transfer to True this will help
//sending certain values automatically
Server.Transfer("NextPage.aspx", True);


NextPage Code:
string strValueOfFirstPageTextBox;
string strSpeciallyAddedValue;
//Use Request.Params to get values of the standard variables
//which get automatically transferred
strValueOfFirstPageTextBox = Request.Params.Get("firstPageTextBox")
//Use HttpContext to get the values specially added
strSpeciallyAddedValue = context.Items("specialVariableName")

Tuesday, August 24, 2004

VJs Tip Of The Day - August 24th 2004

Maintaining State Across ASP.Net Pages

We will talk about two standard methods of maintaining state across ASP.Net pages today... The more sophisticated methods will be discussed from tomorrow onwards...

1) Using QueryString : You can pass values of one page to another page using Query strings... This is an old ASP custom but for security reason not always recomended in ASP.Net

2) Using Session : You can also pass values of one page to another by storing them in session... Use of session is pretty simple and we have discussed that earlier as well (Click Here)... But do understand the fact that Session variables are to store user specific information which is unique to each user and not each page... Also do note that if you forget to clean the session variables in the next page you might land up increasing the session size like anything effecting performance of your application...

Monday, August 23, 2004

VJs Tip Of The Day - August 23rd 2004

Steps to Enable Automatic Transactions in .Net

We have discussed all these steps earlier but now we will club them together to see how you go about doing it....
1. Add TransactionAttribute to your class
2. Derieve your class from ServicedComponents class
3. Give strong name to your assembly
4. Register your class for COM+ services using Regsvcs.exe


Friday, August 20, 2004

VJs Tip Of The Day - August 20th 2004

Transactions in .Net - Using Services of COM+

To use the services of COM+ and transactions you need to register the assembly that contains you class (transactional) with the COM+... You can do so manually by using .Net Services Installation Tool (Regsvcs.exe)... Well if your client is managed client then you do not need to do this registeration as CLR will do it for you but if you know that at sometime your clients can be unmanaged then it is always useful to register your assembly manually...

Thursday, August 19, 2004

VJs Tip Of The Day - August 19th 2004

TransactionOption

We are getting into the details of TransactionOption today...

1. TransactionOption.Disabled implies that automatic transactions cannot control the object... Still if you want this object to support transaction then it can be done by manual transactions...

2. TransactionOption.NotSupported implies that object context will always be created without transaction for this object no matter whether a transaction is already existing...

3. TransactionOption.Supported implies that if transaction exists the object will run under it, if it does not exist it will run without transaction...

4. TransactionOption.Required implies that if transaction exists the object will run under it, if it does not exist a new transaction will be created...

5. TransactionOption.RequiresNew implies that the a new trasaction is started for every request...

PS: Trivandrum User Group in India launches its own site... First bold move by any User Group in India and I bet it is for good... visit
http://www.t-mug.org/membersite.aspx to see for yourself...

Wednesday, August 18, 2004

VJs Tip Of The Day - August 18th 2004

Transaction Attribute for your class

We discussed that for our class to participate in transactions we need to do certain things... Now we will talk about what those certain things have to be...

One of the things that you need to do is to apply Transaction attribute to your class... You would apply it just before your class declaration... It would look something like:

[Transaction(TransactionOption.Supported)]

Now if you are familiar with COM+ transactions then TransactionOption has values which are very similar to COM+ transaction options...

The TransactionOption values can be Disabled, NotSupported, Supported, Required, RequiresNew... Out of these Required is the default...

Tuesday, August 17, 2004

VJs Tip Of The Day - August 17th 2004

Transactions in .Net

We talked about Transactions in .Net a few days back... We learnt that how they can be automatic or manual...Now ,we will talk about Automatic Transactions this whole week... Hopefully it will be interesting and useful...

Automatic Transaction manages transaction boundaries for you... But again it manages the boundaries on the basis of what you suggest it to do (you do that by attributes which we will talk in a couple of days... ) The transaction flows from one object to other as instructed... Within its scope, it includes, the objects that are suppose to participate in transactions and leaves out the objects that are not suppose to participate...

Note: Nested transactions are not possible in Automatic model...

PS: I sent a document to my team for review... Two days later a friend/collegue wrote to me that I had sent the wrong document... Till now no reviewer other than her has got back (out of 6-7 in the list)... I was wondering about the chances of reviewers not opening the review attachments which someone sends and also chances of them getting mad if the document was never sent, interstingly both the chances are equally bright... :-)

Monday, August 16, 2004

VJs Tip Of The Day - August 16th 2004

Design Days: Caching and Performance

Usually we assume that Caching is done to improve performance, well it is true but at the same time it may deteriorate the performance as well... It many a times depends on the Cache Hit Ratio... And you should definitely consider your Cache Hit Ratio [CHR] before considering your caching strategy...
To make it more clear consider a scenario in which you cache the state list of a country... Now if user has to select one of the available states then there are bright chances that your cache hit is a success.... It might be a good idea to cache the list instead of retrieving it from the data source all the time...
Now consider that you cache product list for a store like Walmart... You might have millions of product in the database and it is very unlikely that the product selection will lie in the correct category and type... There are more likely chances of miss than of hit, but you will lose time in searching the cache, so here is when you should consider hitting the data source instead of caching...
Now there are options other than caching and there might be other factors governing the decisions, this is just a design suggestion...

PS: 15th August was India's Independence Day... Please accept my belated wishes for a Happy Independence Day... Jai Hind... (Hail India!!)

Friday, August 13, 2004

VJs Tip Of The Day - August 13th 2004

If you want to use the services provided by COM+ in your .Net then you write Serviced Components...

To write serviced components you will have to include the namespace of System.EnterpriseServices... You will also have to inherit you class from ServicedComponent

PS: For My Records(FMR) - I am travelling to Omaha, NE to celebrate my friends Birthday...

Wednesday, August 11, 2004

VJs Tip Of The Day - August 12th 2004

ViewState Encryption- Considerations

Well to add to yesterday's tip, there are few more tips, rather considerations to come on the way related to viewstate encryption...

If you are using SSL for your webpages then you would want to reconsider whether you need this encryption of ViewState seperately or not as your whole data will be encrypted anyways...

The other point is webfarm scenario... When deploying your webapp to Webfarm without sticky sessions if AutoGenerate option is on (which is by default) then there are chances that your each request will go to different machine and ViewState decryption will fail there... You will have to do synchronization activities in this scenario...

Note: Sticky session is a scenario in which, once the first request in a session is served by a particular server all the consecutive requests for that session will be directed to the same server...


PS: I was wondering that if someone reads one useful article from MSDN daily, (note: JUST ONE useful article) how many days would it take for him/her to become "Most Knowledgeable Person" around there... Think about this, I smile when I come up with a number out of my hat... :-)

Tuesday, August 10, 2004

VJs Tip Of The Day - August 11th 2004

Encrypting View State

Few days back we talked about ViewState being Base64 Encoded and that it moves back and forth from client to server... Now if you wish to encrypt the ViewState for security reasons then below is the way to achieve the same...
You have to change your web.config file... In the system.web section of the web.config file you need to add the below tags

<pages enableViewStateMAC="true"/>
<machineKey validation="3DES"/>

PS: Note that here validation="3DES" causes the encryption... enableViewStateMAC causes the check that ViewState is not actually tampered...

VJs Tip Of The Day - August 10th 2004

Processing Transactions in .Net

.Net components do support transactions, the only thing is that you have to do some extra work to achieve the same... .Net framework supports two kind of transactions models
1.) Manual Transactions
2.) Automatic Transactions

Manual Tranactions is used in case of ADO.Net, message queuing etc... Automatic Transactions are available for ASP.Net, Web Services, .Net Components etc... .Net classes can also use the services of COM+ but they need to be registered with COM+ for doing so... We will talk more about Transactions in .Net in time to come...

PS: Is it a good idea to send in some code snippets in Tips?? If so do let me know, I do not want the tips to be cumbersome to read...

Monday, August 09, 2004

VJs Tip Of The Day - August 9th 2004

Changing Reference to different version of external assembly

Consider a scenario when your application is referencing version 1.0.1.1 of "SomeAssembly" in GAC... Now you realize that there are some major bugs in that assembly and you want your assembly to reference the new version of the assembly (say 1.0.2.0) and you do not want to go ahead and recompile your code for that... What would you do... Well here is the answer... You would go and change the configuration file with similar updates to the ones I am writing below...


<configuration>
<runtime>
<assemblybinding xmlns="urn:schemas-microsoft-com:asmv1">
<publisherpolicy apply="no">
<dependantassembly>
<assemblyidentity publickeytoken="whatever" name="SomeAssembly">
<publisherpolicy apply="no">
<bindingredirect newversion="1.0.2.0" oldversion="1.0.1.1">
</dependantassembly>
</assemblybinding>
</runtime>
</configuration>


PS: You can achieve the same with different methods as well this is just one of them

Friday, August 06, 2004

VJs Tip Of The Day - August 6th 2004

Common Type System - Value Types vs Reference Types

Value Types and Reference Types are two basic types of CTS... Value Types are allocated on the stack and Reference Types are allocated on Managed heap... How this thing works is that the value of the "Value Types" is allocated on the stack where as in case of "Reference Types" the value i.e. the object itself resides in the heap but there is a pointer to the object in the memory...

Considering this differences value types are faster but consume the process memory....
"Reference types don't consume this process memory but then they become slower... When value types are copied then the value is copied as a whole on the stack, when reference types are copied then only the references are copied on the stack..., when to use Reference Types as compared to Value Types and visa-versa is based on various other factors also so do take time to decide when to use what..."
Value types are usually native data types like int, float, char, enums etc... Reference types mostly include string, classes and some other user defined types...

PS: I wrote Value types to be allocated on heap in my second paragraph only Kathleen was able to find that out... You guys do write back when you see such gross errors... Thanks Kathleen...

Thursday, August 05, 2004

VJs Tip Of The Day - August 5th 2004

Design Days: View State Performance Tips


In controls like listbox controls, items added to the list are carried between client and server via view state... When the list of items become considerably large it starts hitting the performance and increasing the traffic between client and server... To avoid this set the EnableViewState property of the control to be false... By this unnecessary data will not move back and forth to the server... Though, do note that by doing so your selected items will no longer remain selected after postback and you will have to write special code to achieve this functionality...
The same problems are also faced in controls like Datagrid so do ensure that you use View State judiciously and give enough thoughts while designing your application...

To see the view state of a page right click on displayed page and 'view source', here you will see a long line of junk characters for __VIEWSTATE property... This is the encrypted view state of your controls... Well, that makes me add one more point the performance... All the viewstate data that passes to and comes back from server goes through the cycles of encryption and decryption which in itself is also a costly affair hitting performance...

Note: Ben Miller from Microsoft corrected my silly mistake here... view state is Base64 encoded when it moves back and forth from client to server.... It is not encrypted... Thanks for the correction Ben... Btw, Ben Miller is also ASP.Net MVP Lead of North America... !!


PS: Did you read my suggestion to Microsoft long time back... You will like it... Click Here

Wednesday, August 04, 2004

VJs Tip Of The Day - August 4th 2004

Assemblies Details Introduction

•Assembly has a four part name that uniquely identifies it – Name, Version, Culture & develope
•Version – Major.Minor.Build.Revision
•Culture – Satellite Assemblies contain culture info attribute that indicates the spoken language and country-code
•Public Keys – Used to uniquely identify the developer of a component and provide protection from getting tampered by external sources
using System.Reflection;
[assembly: AssemblyVersion(“1.2.3.4”)]
[assembly: AssemblyCulture(“en-US”)]
[assembly: AssemblyKeyFile(“mykey.snk”)]

PS: Naveeth Krishnan from Trivandrum .Net User Group has suggested me to start writing more about Assemblies so this is the beginning... We will deviate once in a while so that none of us get bored but still we will touch more about Assemblies now...

PPS: Did you read my suggestion to Microsoft long time back... You will like it.. Click Here

Tuesday, August 03, 2004

VJs Tip Of The Day - August 3rd 2004

Design Days - Data Access Ways

This is just a small little thought, which I thought, can be shared... When we try to access data from a table in DataSet many a times we have to use the name of the columns... We have multiple Datagrids bound to each table and many other controls of your page also access data from these tables... Now, just in case your column name changes in the table you would want to have minimum impact on the stuff that you have to change...
As a solution to this you can have DataDefinition class... This class can just return back the name of the columns as properties, you can define these light weight classes if you have less number of tables to be accessed... In any case if you do not follow this approach then it is always advisable to have constants declared at a common location and store your column names in there....
You can later use either these constants or light weight classes to get the column names...

PS: Do note that by introducing such things you can make your design more maintainable but sometimes loose on performance... Take decisions on the basis of what is ideal for your situation... Don't sue a poor fella like me for suggesting, you anyways won't get much... :-)

Kathleen Dollard, Microsoft MVP adds the below:
Use enums. Much faster and far more maintainable....

Monday, August 02, 2004

VJs Tip Of The Day - August 2nd 2004

A design consideration to access Web.Config

If you are going to access Web.config file for various configuration settings of yours e.g. ConnectionString.... Then sometimes it would be a good idea to develop a class specially to access configuration settings, something like a ConfigManager... what this class can inturn do is to access Web.Config file in its constructor to retrieve your configuration settings and fill up private variables and in turn expose them as properties...

The advantage of having this approach is that if you decide to change your source for these settings then you do not have to go to every page and change the way you access the settings you just change your class... Other than this there can be potential situations in which you might decide to encrypt some values in your web.config file later... This security enhancement would otherwise cause all your pages accessing this information change... During such situations it would be ideal to have a class like this...

To add icing to the cake you can make required member variables static so that the class's instance need not be created everytime... Also do try to make sure that every call to your property should not need to query web.config file to access values or else they may degrade performance...

PS: This is a design tip, it may not be perfect design for all situations these are just some of my generic thoughts, if you have anything to dispute please feel free to drop me an email

Binu from Trivendrum User Group has added the below thoughts...
There is one more advantage to this. if you want to deploy the application in different environments (developer sand box/ integration/ QA / production) it would be easy to have this App.Config . The possibility of using different encryption keys / SQL server in different environments is pretty high in real scenario and with the number of builds we deploy before pushing to production corroborates this.
-- Binu