Wednesday, March 14, 2012

C# : The server is unwilling to process the request

 

Busy doing some AD work with C#.

I’m creating the user, changing some of the attributes (e.g. setting “DONT_EXPIRE_PASSWORD”) and then setting the password.

Got the exception:

DirectoryServicesCOMException (0x80072035): The server is unwilling to process the request

Mr. Google to the rescue. After sorting through the huge pile of crap, found the answer.

You need to do it in the following order:

  • Create the user
  • Commit changes
  • Set the password
  • Commit changes
  • Change the attributes
  • Commit changes

i.e. Set the password BEFORE doing any attribute CRUD stuff.

Enjoy!

Thursday, March 08, 2012

WCF : Message logging


The best developer tool is Google. 99 % of the time you can find an answer or sample code to your problem.

On the other hand, the worst developer tool is Google. At least half the links are rubbish. There should be some kind of peer review like stackoverflow has to delete crap articles from the Web.

This came about because I was looking for a way to log the actual messages being sent over WCF.

Came across loads of articles, none of which worked 100%. Some were misleading and some were absolute rubbish.

The worst aspect was that they include some small XML snippet which you cut and paste into the web.config without any context i.e. just where are you supposed to insert it?

Anyway, here’s the solution I came up with:

<configuration>
...
  </system.serviceModel>
    <diagnostics>
         <messageLogging 
         logEntireMessage="true" 
         logMalformedMessages="false"
         logMessagesAtServiceLevel="true" 
         logMessagesAtTransportLevel="true"
         maxMessagesToLog="3000"
         maxSizeOfMessageToLog="2000"/>
     </diagnostics>    
  </system.serviceModel>
...
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue=
"Information,ActivityTracing"
        propagateActivity="true">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
          <add name="messages"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="c:\...\messages.svclog" />
        </listeners>
      </source>  
    </sources>
       <sharedListeners>
      <add initializeData="C:\...\Trace.svclog"  
type="System.Diagnostics.XmlWriterTraceListener"
        name="xml" />
    </sharedListeners>
    <trace autoflush="true" />
  </system.diagnostics>
...
<configuration>

Enjoy!

Wednesday, February 29, 2012

Visual Studio : "Set As Start Page" doesn't work in the real world

I have a web site that contains a "Default.aspx" but I want the start page to be "abc.aspx".

So in Visual Studio 2010, I right click the "abc.aspx" page and select "Set As Start Page".

Hit F5 - works perfectly.

Deploy it to a real IIS 7 server and WTF - Default.aspx is displayed in all its delights?

Turns out the "Set As Start Page" option only applies to Cassini - the VS internal web server (which is why it works when you hit Run ( = F5)).

In the real world, you have to go to the web site inside the IIS Manager, select "Default Document" and then type abc.aspx and move it up to the top.

There you go!

Enjoy!

msdeploy : Package installation failed - 'managedRuntimeVersion' differs

So there I was trying to use msdeploy to deploy a web site from Visual Studio 2010 into an IIS 7 server on another box using the "Build Deployment Package".

I got the error:

The package installation failed.
Details:
The application pool that you are trying to use has the 'managedRuntimeVersion' property set to 'v4.0'. This application requires 'v2.0'.

My web application used the "Default Web Site" and the application pool that it was connected to (DefaultAppPool) was .NET Framework 2.0. Yeah - I screw around with IIS a lot!

Of course, where I was deploying it to was the more usual .NET Framework 4.0.

Mr. Google to the rescue and the answer is to set the following options under the project / Properties / Package/Publish Web:



You need to tick the two text boxes "Include IIS Settings" and "Include application pool settings".
And away you go with msdeploy.

Enjoy!



Wednesday, February 22, 2012

WIF : Web Platform Installer

WIF has just been added to the Microsoft Web Platform Installer. (WPI)

Just search on the keyword "identity".

Hey - hey - it's becoming mainstream.

Makes it just that bit easier to download.

The WPI is a really useful tool that collects a pile of Microsoft (and other) downloads in one place. Well worth 5 minutes of your time to go and have a look!

Enjopy!


Monday, February 13, 2012

C# : Some basic validation




Doing some basic validation and I found these to be useful:

Check for mandatory fields:
if (String.IsNullOrEmpty (xxx)) … error

Field must be numeric only:
int number;
bool result = Int32.TryParse (xxx, out number);
if (!result) … error

Valid date format:
DateTime date;
bool result = DateTime.TryParse (xxx, out date);
if (!result) … error

Valid email format:
bool result = Regex.IsMatch(xxx, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)
(([\w-]+\.)+))([a-zA-Z]{2,4}[0-9]{1,3})(\]?)$");
if (!result) … error

Enjoy!

WCF : soapUI error "BadContextToken"

 

While soapUI is a really useful tool for web service unit testing, it doesn’t work with WCF.

In particular, with the default "wsHttpBinding", you get the message  "BadContextToken".

Mr. Google comes back with tons of results about why soapUI doesn’t play nicely with WCF and recommends WCF Storm which looks good but it’s not free.

So a whack of investigation later:

Add the following binding into the <bindings> section of your web.config:

<!-- soapUI -->
      <wsHttpBinding>
        <binding name="wsHttpBindingNoSecurity">
          <security mode="None">
            <transport clientCredentialType="None" />
            <message establishSecurityContext="false" />
          </security>
        </binding>
      </wsHttpBinding>

Add the following "bindingConfiguration” parameter to your <endpoint> section:

binding="wsHttpBinding" bindingConfiguration="wsHttpBindingNoSecurity"

The WCF service has an endpoint like:

blahblah.svc

For soapUI you need to change this to blahblah.svc?wsdl when you “Add WSDL”.

Once you’ve added the service to soapUI, click the default operation and then click the “WS-A” tab at the bottom.

Click “Enable/disable WS-A addressing”.

Click “Add default wsa:Action”.

Click “Add default wsa:To”.

And (finally) success!

REMEMBER: Every time you change the web.config, you need to right-click the soapUI service and “Update Definition” or F5!

Enjoy!

Wednesday, February 08, 2012

Visual Studio : Showing the active file in the Solution Explorer tree

So there I was doing some consultancy work on site and I noticed that when you click through the open files in the file bar on the top, their Visual Studio tracks the active file in the tree.

Something I've always thought would be really useful.

Turns out the way to do this is via:

Tools / Options / Projects and Solutions / General / Click "Track Active Item in Solution Explorer".

WTF isn't this the default?

Enjoy!

Stackoverflow : The big 100 badges

Another of my goals - getting 100 badges on stackoverflow.




Enjoy!


Thursday, January 26, 2012

c# : Nullable DateTime


So there I was trying to convert a nullable date time "DateTime?" to DateTime.

Then I got the error:

"Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)"

stackoverflow to the rescue and you need to use the null-coalescing operator.

So the code ended up like:
DateTime x = DateTime y ?? DateTime.Now;
Love those "??".

Aside: When I tried to find the article again for this blog, found that searching Google for "??" is an interesting exercise.

Enjoy!

Thursday, December 22, 2011

C# : Counting "rows" in an XML structutre

Working on a system that returns an XML structure as a string.

The structure looks like:

Table
Row
Info1/
Info2/
...
/Row
/Table

I needed to find out how many rows there were.

Mr. Google to the rescue and the solution is:
XmlDocument readTable = new XmlDocument();

readTable.LoadXml(stringXml);
int rowCount = readTable.SelectNodes("Table/Row").Count;
Refer XPath Examples for the syntax of more kinds of searches you can do.

Enjoy!

Friday, December 02, 2011

ASP : The Web Form equivalent of MessageBox

When you are writing a Windows application, the ubiquitous MessageBox is extremely useful for popping up a quick debug message but it’s not available for ASP.NET Web Forms.

In such cases, use:

Response.Write("<script language='javascript'>alert('Your message');</script>");


Enjoy!