Browsing Category dotNet

Code cannot find reference to assembly in GAC

September 29th, 2006 // 7:09 pm @

I came across this error today which made me quite curious on why it is happening. This occured to me in a SharePoint WebPart, but it is a very general .NET Framework error and as such can occur in any type of .NET application.


CS0011: Referenced class someClass’ has base class or interface
someNamespace.someBase’ defined in an assembly that is not referenced. You must add
a reference to assembly ’someNameSpace’.


To explain the scenario. You have an assembly in the GAC with some classes. You have some code which you are running which reference the GAC dll. For my case, I was inheriting my webpart from one of the baseclasses defined in the GAC assembly. If I put the assembly in the /bin directory as well as the GAC it worked beautifully, but if I deleted the assembly from my /bin directory, I got the above error. This sort of nullified the entire purpose of having a GAC.


What was happening was that the code was trying to reference the someNamespace.someBase Class but as there was no link in the code to provide it with a hint that the required assembly is in the GAC, it was complaining. As soon as the dll was in the /bin directory it was able to reference it and all was ok. To solve the problem, there are two ways to do it.


1. If you have a ASP.NET application, you can add a @Register Assembly derivative in your page.
2. You can add the reference in your web.config to hint the application to look for the assembly in the GAC by adding the following lines in your web.config file..
<system.web>
<compilation batch=”false” debug=”true”>
<assemblies>
<add assembly=”someAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=s0m3publ1ck3y” />
</assemblies>
</compilation>
</system.web></code>


The solution was found, the day was won. But it made me wonder if this is a design defect with .NET. If placed in the \bin directory, the application was very happy to reference the dll. If an assembly is in the GAC, why do we have to explicitely tell out application to look for the reference in the GAC. Shouldn’t it be doing that automatically in the first place.

Category : dotNet &SharePoint

Extracting dll’s out of the GAC

June 13th, 2006 // 7:06 pm @

I was recently asked how to reference the Microsoft.SharePoint and few of the other SharePoint dll’s in a project while developing on a non SharePoint machine. The solution is quite simple which we often seem to neglect.


This is applicable for any assembly which is installed in the GAC. If you use the Windows Explorer, you can see those assemblies in the Windows\Assembly\GAC folder, but cannot copy them out of there. To access the internal structure, use the command shell, by typing cmd in the Start->Run menu. Navigate to the GAC folder and you will see all installed dll’s as folders, which inturn will contain the corresponding folders for each version. Just copy the dll’s from there to some other location and you have successfully extracted the dll’s from the GAC.

Category : dotNet &SharePoint

MSBee – The new buzz in town

May 17th, 2006 // 12:05 pm @

I have been playing around with the MSBee project. I was trying to figure out, how to build my .NET 1.1 based SharePoint webparts projects using Visual Studio 2005, while keeping the project unchanged so that other VS 2003 users can work on it without any problems. That was when I came across MSBee. The Beta 2 had been released somedays ago and it is a nice addition. I wanted to start using VS 2005 on a regular basis and not only for R&D purposes but existing projects required me to build .NET 1.1 code. So cannot make the transition, and ended up installing both VS2003 and VS2005 previously. This time, I am determined not to install VS2003 on my main dev box and somehow get VS2005 to do all my work. For those adamant people like myself, check out the MSBuild Extras Project.

Category : dotNet

Profile WebParts for performance and memory

May 15th, 2006 // 3:05 pm @

I recently wanted to profile some of my Webparts to identify possible memory usage and speed issues. So I started to look for some profiling tools which I can evaluate and maybe ask my company to purchase in the long run. I tried three tools. Compuware Devpartner Community Edition, AQTime and ANTS Profiler. First of all, let me clarify that this was no proper review. I did not have ages to sit and setup and determine which one performed best. All I wanted was which tool would get the job done quick and easy.


Devpartner Studio installed properly, but resulted in a blue screen crash on my PC when I tried to configure it to profile ASP.NET application. Also tried to run my application in debug mode but did not seem to work. Maybe I was doing something wrong or maybe it was not working properly due to that crash, but anyways I did not bother with it much and uninstalled it. Too bad, as I have always admired Compuware tools. Will maybe patch my PC and see if I can get it working again sometime in the future. Not sure when I will get time.


AQTime was a bit complex to use. I had to create a project and set it to profile and ASP.NET application. Tried to tell it to profile only my webpart assembly but it kept on profiling the entire ASP.NET application including the sharepoint dll’s and .net stuff. Tried tweaking for sometime, but no prevail. I guess I will have to read the manual sometime. But one thing I observed, was that it gives a lot of details. But unless I can tell it to profile all the classes and code in just my webpart, I don’t think I can make any sense out of the results.


The last and the hero of these tests was ANTS Profiler. One of the easiest tools I have used. Hardly took any time, and I configured it successfully to profile just my webpart. Fired off the sharepoint page in a browser, used it a bit and closed it down and voila, my results were ready. It works in two modes. Memory profiling and performance profiling. In performance mode, it showed all the classes in my assembly and the amount of time each took, the number of hits each received etc. Does not display a lot of details, but is perfect for my work as all I needed was to optimise the most hit code in my project. So my winner for today is ANTS Profiler. I have heard about Microsoft CLR Profiler, but have not used it. Will give it a try maybe later, but not I have some code optimisations to do :)


Incidently, ANTS Profiler was the smallest of the three, around 6 MB installation. The other two were around 40MB+. I personally like lightweight quick to use utilities which get the job done instead of heavy duty state of art stuff which would take ages to configure and run. Will like to spend more time using ANTS profiler within the trial period I have to evaluate it better and see how good it is?


With this post by no means I am suggesting a proper review and evaluation of the above tools. It is just my first looks with these tools having spent no more than 15 mins on each. If someone finds better results, do let me know as I am interested in finding a good memory and performance profiling tool.


Update: Compuware just offered me a free 14 day trial of their full version. Will try it out when I have a little bit of time and report my findings in a later post someday.

Category : dotNet &SharePoint

DataFormatString not working with BoundFields

April 4th, 2006 // 9:04 am @

I came across this issues when I added a GridView to display some data. One of the data fields was a Date Time field and it was displaying both the date and time portions. So I added the DataFormatString as:

<asp:BoundField DataField=”PubDate” HeaderText=”PubDate” SortExpression=”PubDate” DataFormatString=”{0:d}” >

But strangely, it did not display. It was not using any formatting which I put in the DataFormatString. The solution to this was to set HtmlEncode=false. This made the control honor the DataFormatString. The explaination to this behaviour can be found in the MSDN Documentation for BoundField. The exceprt of this behaviour is given below.



When the HtmlEncode property is true, the value of the field is HTML encoded to its string representation before the formatting string is applied. For some objects, such as dates, you might want to control how the object is displayed with a formatting string. In those cases, you must set the HtmlEncode property to false.

Category : dotNet

Manually sign interop assemblies

February 20th, 2006 // 5:51 pm @

I was asked how to import a COM dll into a .NET project and sign it using a key. e.g. ActiveDS.dll. This is not signed and hence if added by the Visual Studio UI into a project which was signed, it will refuse to compile saying that ActiveDS.dll is not signed. Made me realize that many developers are so much dependent on the Visual Studio way of adding references that they were not aware of the manual way of generating a dll interop from a tlb.


Start by opening the Visual Studio command prompt. We use the tool called tlbimp to generate the interop assembly. When generating the assembly you can specify a keyfile to sign the newly generated assembly with.


tlbimp YourFile.tlb /out:YourFile.dll /keyfile:your_keyfile.snk


Add this newly generated dll into your Visual Studio project references manually and problem solved!

Category : dotNet

Solution to ASP.NET, Javascript value modification problem

February 20th, 2006 // 4:14 pm @

Sorry for being late. Was tied up in work. So lets start where I left off before. The main problem which I found was that if the ASP.NET button was clicked, and I wanted to run some client side Javascript to update the values of hidden form elements and get the updated values on the postback event handler for that same button, it ignored all the changes javascript made and did not reflect to the code behind. My target was to somehow reflect the changes to the values to the back end.


The solution was not a very elegant one, and I am sure there has to be a different solution to this. But at least it works and gets the job done.


What I did was that I changed by ASP Button in page B to a LinkButton. I assigned blank text to it so effectively making a hidden link button.


<A href=”javascript:SendMail();”>Send Mail</A>
<asp:linkbutton id=SendMail runat=”server”></asp:linkbutton>


This enabled me to call a Javascript when the button was clicked, and also allowed me to have a code behind event handler for the link button click. As the link button is effectively hidden and hence cannot generate an event for the link button click on the back end, we have to raise this event manually in order for this code to work.


The next step would be to implement the SendMail() javascript function.


<script language=”javascript”>
function SendMail()
{
// save the values of the hidden variables from page A
// to the hidden variables in page B
// then call a postback with the id of our SendMail link button
__doPostBack(’SendMail’,’’);
}


This enables the javascript to store the value to the hidden variables first and then call a manual postback which would now take the new updated values and reflect it on the code behind. Effectively the user only has to click one button, which would first call a Javascript, change the values of the hidden form fields, and then do a postback with the new values reflected at the code behind.


Posted at DotNetJunkies on Wednesday, November 17, 2004 6:50 AM

Category : dotNet

ASP.NET Javascript value modification problem 2

February 20th, 2006 // 4:04 pm @

continued from my previous post …


I guessed as .NET viewstate is managed mostly using Javascript and GET / POST, there should be some way by which I can signify my hidden variables as dirty or refresh the viewstate to reflect the value changes by Javascript, before it is sent to the backend when the Send Message button is clicked. But no avail. I could not find any way to refresh or resync the viewstate using Javascript. I guess that should be because Viewstate is managed on the server so maybe we cannot modify it from the client and ask the server to resync with it.


So second approach, was to try to somehow add the values to a post queue. As new updated values will be send using a standard POST and ASP.NET will sync and update the ViewState internally. So that instead of accessing using ViewState, I can maybe access the updated Javascript values from the backend page using the old Request.Form method. But no avail. Still did not work.


Another idea was to create dynamic textboxes thru Javascripts, when the SendMail button is clicked and fill it with the value and then access them using Request.Form from the backend. But still the new values were not reflected.


Posted at DotNetJunkies on Tuesday, November 09, 2004 1:20 PM

Category : dotNet

ASP.NET, Javascript value modification problem 1

February 20th, 2006 // 4:02 pm @

While solving one particular case in a SharePoint webpart, I came across a very queer problem in ASP.NET Postback and Viewstate handling. Let me declare the scenario in detail :


I have a page A which has some values in hidden variables of type HtmlControls.HtmlInputHidden.


I popup a page B from page A using javascript window.open. Page B has a button called Send Mail which would send a mail to some recipients. The recipients list has to come from page A.


The standard way of passing data from one window to a javascript popup is querystring, but that is something which is not wanted. It has to work without querystring data passing, and as it is in SharePoint sessions are disabled by default, and hence passing by session is also not an option.


So the only option, is to keep a few hidden variables in page B, read the values from page A using window.opener from Javascript in page B, set the values of the hidden variables in page B, and then on click on Send Mail, access the hidden variables and send a mail using ASP.NET.


The funny problem, is that I can read the values from page A, using javascript. I can also set the values to the hidden variables ( verified by using and alert and checking the value of the hidden variable after it is set ), but as soon as I click Send Message, and try to access the value of the hidden variables from the backend ASP.NET code, the values set using Javascript are lost. On examination, the Viewstate seems to ignore the changes made to the Javascript, if the controls are hidden and does not pass it to the backend.


If the controls are like ASP.NET Textboxes, it works properly, but if they are hidden, again the same problem as above occurs. So definitely it not something exclusive for non .NET Controls.


More on this later on how I solved it when I take my next break :)


Posted on DotNetJunkies at Tuesday, November 09, 2004 1:12 PM

Category : dotNet

Latest Posts

Testimonials

"Amar clearly has a thorough knowledge of Sharepoint, this knowledge, coupled with his professionalism and dedication made him a tremendous asset to the project. He was the key contributor to the later stages of the project delivery effort (the really difficult bit, long hours and hard work that puts huge demands on people), and I can state categorically that without his focussed dedication and hard work we would not have been able to meet the timescales imposed upon the project."

Tim Ellis , Sharepoint Project Manager , Royal Bank of Scotland

Subscribe Now