Wednesday 31 March 2010

Failed to start monitoring changes to Global.asax

I was happily coding away the other day, when I came across the following error message when compiling my code in VS 2008: Failed to start monitoring changes to global.asax.  After a scratch of the head and another attempt at compiling, I still got the same message.  After a bit of digging around, I found the problem was in my web.config file and the offending line was:
After I removed this line, it compiled successfully.  Strange thing was, I have had this line in my web.config file for a long time and only now was the compiler working.  If anyone has any suggestions as to why, I'd be grateful to receive them.

Tuesday 30 March 2010

I have been developing a WCF web service recently and in a previous post, I explain how to specify an EndPoint & binding in code.  That was a few weeks ago and it’s now time to make the web service run on the customer’s production server, however, to put a spanner in the works, not only did the customer want the web service to run over SSL, they created their own certificate, which forced a browser to show the message, “There is a problem with this website’s security certificate”.
I have two web services in this project, a traditional one (asmx) and the WCF.  So the certificate problem caused the asmx web service to fail too, until I found the following piece of code via Google, which I have extended ever so slightly to ensure that the certificate name is as we expected and whether it’s issue date is within bounds (I would attribute it to the author, but far too much water has passed under the bridge and I can’t remember who it was from).  The following code goes in the client (the caller) software:
public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
{
public TrustAllCertificatePolicy()
{ }
public bool CheckValidationResult(ServicePoint sp, X509Certificate cert,
WebRequest req, int problem)
{
// This is out cert string issuer = ""; DateTime startDate = new DateTime(1753, 01, 01), endDate = new DateTime(1753, 01, 01); try {
startDate =
Convert.ToDateTime(cert.GetEffectiveDateString()); }
catch
{ }
try
{
endDate =
Convert.ToDateTime(cert.GetExpirationDateString()); }
catch
{ }
// Check that the issue and cert start & end date are valid
if (issuer == cert.Subject && startDate <= DateTime.Now && endDate >
DateTime.Now) return true; else
return false;
}
}
You can then issue the following piece of code which will call the CheckValidationRequest:
System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress(URL); WebServiceClient wService = new WebService.WebServiceClient(new System.ServiceModel.WSHttpBinding(), address); System.Net.ServicePointManager.CertificatePolicy = new WebServiceHelper.TrustAllCertificatePolicy();
Place a breakpoint in the CheckValidationRequest and you’ll see the request being issue.
So, this was perfect for the traditional asmx file, but for the life of me, I couldn’t get this to work for the WCF.  I Googled & Googled and found all sorts of weird & wonderful things that you could try and do to your config file to get the certificate to come through, but in the end, I found the following piece of code & config changes were required to get a self cert to work on a WCF web service:
The following line has been altered from above, to tell the TCP layer what type of transport is required:
WebService.WebServiceClient wService = new WebService.WebServiceClient(new System.ServiceModel.WSHttpBinding(System.ServiceModel.SecurityMode.Transport), address);
Notice how we need to tell the WSHttpBinding class, the transport mechanism.
Finally, in the web.config file of your WCF web service, you’ll need the following:
behaviors section --> <
bindings> <
wsHttpBinding> <
binding name="TransportSecurity"> <
security mode="Transport"> <
transport clientCredentialType="None"/>

security>
binding>
wsHttpBinding>
bindings>
system.serviceModel>


That’s it!  As I say, it took me hours to get find these tiny number of lines, but I was elated when I had! The errors that I was getting when running my code were: Could not find a base address that matches scheme http for this endpoint with binding WSHttpBinding ,or The requested service could not be activated

Monday 29 March 2010

Error CS0433: type class exists in both dll location and dll location 2

I’ve just had the most annoying few days trying to identify why my Ajax web site worked on my dev & test machines, but it wouldn’t work on the live machine.  I have spent literally hours on this problem and I thought I’d share my solution even though there are other solutions which may help.
First off, I was receiving the following error on the live site when I tried to open any aspx page, even though the Ajax was only in one page:
“Error CS0433: type '' exists in both '' and ''” and I was also getting System.web.extensions.dll problems too.  Google was offering up a lot of results for both problems but they either talked about putting the System.web.extensions.dll in my Bin folder, which I did and made no difference but didn’t sit right with me anyway, or it said that you shouldn’t use an App_Code folder, which I do.
In the end, I opted for creating a new Ajax enabled project and then compare the web.config files, which was successful for me.  Below I’ve listed the two areas in my old web.config file which were wrong and then I’ve listed the new entries which replaced them.
1.  Old section
<configSections>
<sectionGroup name="microsoft.web" type="System.Web.Configuration.MicrosoftWebSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false"/>
sectionGroup>
sectionGroup>
sectionGroup>
configSections>
 
New section
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
sectionGroup>
sectionGroup>
sectionGroup>
configSections>
2.  Old section
<assemblies>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.Services.Client, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.Services.Design, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Web.Extensions.Design, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
assemblies>
New section
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
assemblies>
That’s it!  And my code then started to work.  If any of the above helps just one person, I’ll be pleased, because this took an age to figure out.

Sunday 28 March 2010

Content-Type-text-xml-charset-utf-8-was-not-supported-by-service-The-client-and-service-bindings-may-be-mismatched

I'm currently building a WCF web service and as we all know, the URL for the web service is stored in the web.config file, and I knew that when I came to deploy it to the live server, the binding would fail. Yes, I know I could publish the web service to the live server and then Add a reference in VStudio, but I like to put the test and live server URLs in the config file.
Anyway, I attempted to do the following in code, but I kept getting the error message: Content Type text/xml; charset=utf-8 was not supported by service The client and service bindings may be mismatched. And the code that caused the error was as follows:
System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress("");
InternalWebService.InternalWebServiceClient wService = new InternalWebService.InternalWebServiceClient(new System.ServiceModel.HttpBinding(), address);
Not exactly the most verbose error message in the world, so I tried a few things but they didn't work, so I started to look at the bindings, as this was perhaps the most likely culprit.  I looked in the web.config file and saw the following section:
<client>
<endpoint address=""
 
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMMDInternalWebService"
 
contract="MMDInternalWebService.IInternalWebService" name="WSHttpBinding_IInternalWebService">
So I scratch my head for a bit and started to think, hang on, wsHttpBinding, I’ve never seen that before.  So I used intellisense and found that if you type System.ServiceMode.wshttpbinding, a new class appears.  After a bit of googling, I found that HttpBinding, is used in Soap 1.1, but Soap 1.2 uses wshttpbinding().  Anyway, I wacked in the code and hey presto, the code started to call my web service.

Saturday 27 March 2010

WinForms & the DataGridView

I was asked to do some WinForm programming the other day, which I haven't done for a couple of years now, because, as most of you are aware, I'm more a ASP.NET man with the occasional Win Mob thrown in.  And boy, was it good to be back!  I forgot how easy it was to do EVERYTHING!  You don't have to worry about CSS, state management, session vars...
Anyway, let me cut to the chase.  I wanted to use the DataGridView, populate it with data, and based on the data coming out of the DB, highlight certain cells, not rows, with the colour red and make the cell editable.  Well, as you know, the DataGridView is editable by default, so I was on a mission.  And the last requirement was to show only certain columns out of the entire data set, so I needed a way of creating the columns.  So, let me get to the point.  Below is how to create the columns:
dataGridView1.Columns.Add("Reason code(s)", "Reason code(s)");
dataGridView1.Columns.Add("Value0", "Value0");
dataGridView1.Columns.Add("Size 1", "Value1");
The first field is the column name, which I believe you can reference with something like: dataGridView1["ColumnName"], but don't quote me, and the second field is the header text for the column.  The final requirement for the columns, was to have a hidden column which would hold the unique key back in the DB.  Here you have to create a new DataGridView column and text box cell and set it properties, like so:

DataGridViewColumn newColl = new DataGridViewColumn();  
newColl.HeaderText = "";
newColl.Name = "NoteID";
newColl.DataPropertyName = "NoteID";
DataGridViewCell oCell = new DataGridViewTextBoxCell();
newColl.CellTemplate = oCell;
newColl.Width = 90;
dataGridView1.Columns.Add(newColl);
GetMobaOutuput();
Finally, I needed to highlight certain cells in red and make the editable and anything outside of the if, not editable.  Here goes:
dataGridView1.CurrentCell = dataGridView1.Rows[count].Cells[0];
dataGridView1.BeginEdit(true);
dataGridView1.Rows[count].Cells[0].ReadOnly = false;
dataGridView1.Rows[count].Cells[0].Style.Font = new Font("Verdana", 10, FontStyle.Bold);
dataGridView1.Rows[count].Cells[0].Style.BackColor = Color.Red;
You seem to need to call the BeginEdit method before you can alter the properties.  It was the ReadOnly property that took me a long time to find.  Setting it to false means that it's editable and vice versa.

Friday 26 March 2010

New line in JScript alert box

I had a jscript alert box being shown in one of my web projects, but I wanted to format the text so that some text appeared on a new line.  I knew the \n would insert a new line character for me, but the code was failing, not even showing the alert box.
I then remembered that you need to escape the escape sequence!  In other words, you have to put in a double slash (\\).  Here's the code:

System.Text.StringBuilder msg = new  System.Text.StringBuilder();
 
msg.Append("<script>");
 
msg.Append("alert(\"You chose date: "
Calendar1.SelectedDate.ToString("d") + ".\\n");
 
msg.Append("The earliest collection date is tomorrow 
(" + Calendar1.SelectedDate.AddDays(1).ToString("d") + ").\\n");
 
msg.Append("Please choose another date\")");
 
msg.Append("</script>");
 
Response.Write(msg.ToString());

Thursday 25 March 2010

Disable typing in a combobox

Just a quickie: I wanted to be able to prevent a user from typing in a Windows Forms ComboBox and struggled to find the answer. Anyway, to stop someone from typing in your combobox, just go to the Properties window, in design mode, and under DropDownStyle, choose the DropDownList. Alternatively, in code, type:
cmbTotalRows.DropDownStyle = ComboBoxStyle.DropDownList;
That's it!

Thanks for your time.

Sys is undefined

Woo!  Just spent a few hours trying to get the Ajax toolkit and the Ajax AutoCompleteExtender working on our companies ISA server.  I placed the AjaxControlToolkit.dll into the Bin folder, but I was constantly getting "Done, with errors" in the IE browser.  Using the Safari browser and the Show Error Console, I found that the WebResource.axd file was not being pulled down, all because the path was not correct.  After many hours, I found that one of the ISA rules was not set correctly.  However, in my many hours of investigations, I found the following two web sites to be particularly helpful:
and

Wednesday 24 March 2010

Failed to start monitoring changes to Global.asax

I was happily coding away the other day, when I came across the following error message when compiling my code in VS 2008: Failed to start monitoring changes to global.asax.  After a scratch of the head and another attempt at compiling, I still got the same message.  After a bit of digging around, I found the problem was in my web.config file and the offending line was:
After I removed this line, it compiled successfully.  Strange thing was, I have had this line in my web.config file for a long time and only now was the compiler working.  If anyone has any suggestions as to why, I'd be grateful to receive them.

Tuesday 23 March 2010

CSS min-height & viewport

I was working on a website the other day which had the usual CSS left, middle and right column, with the middle column being the viewport, or area where the most content was placed.  Anyway, the viewport had too much information on it, causing the content to spill over into the body (yes, I know the viewport is in the body, but I think you get the drift). 
Anyway, I remember setting the viewport before and found the following code in another project of mine, with comments to assist:
#container {
width: 1400px;
margin: 20px auto 0px auto; /* Just your normal margin */
text-align: left; /* Obvious */
background: white; /* Obvious */
border: solid 1px black; /* Obvious */
min-height: 100%; /* Causes the page to be as large as required. */
/* If little content, will be as large as the area INSIDE the browser window */
margin-bottom: -100px; /* Allows an area at the bottom of the screen to */
/* be used for a footer, if required*/
position: relative; /* Obvious */
}
Just to ram home the comment above, that x will allow you to place a footer CSS class in your code, showing such things as copyright info, company info or email info, at the bottom of your website, safe in the knowledge that no matter how much data you have in the viewport, the copyright information will always be at the bottom of the page, just after the content and floating somewhere else.
What are your thoughts on how to set the viewport so that it doesn't look too big?

Error: Cannot write configuration file due to insufficient permissions

I was trying to create a new web site on IIS7 today, for one of our customers, and I wanted to create a new MIME type of .aspx. Every time I clicked the Ok button it kept giving me the following error: Error: Cannot write configuration file due to insufficient permissions.
I first thought that I hadn't given IUSR and IIS_USER access to the folder, but I still received the message. In the end I realised that the file, web.config, was still checked into Team Foundation Server, which meant the read flag was still set on the file.
Once I removed the read flag, I could save the new MIME type!