Latest Updates: .NET RSS

  • Basic Authentication against the Superfeedr HTTP PubSubHubbub API using a .NET HttpWebRequest

    Phil Leggetter 10:58 am on July 4, 2010 | 0 Permalink | Reply
    Tags: .NET, , ,

    It would appear that setting the Credentials property of a HttpWebRequest still leads to authentication failure against the Superfeedr HTTP PubSubHubbub API. This might be because the way the HttpWebRequest works is that it first waits to be challenged for credentials via a 401 (not authorized) and then replies with the authentication (more info include PreAuthenticate here).

    So, this doesn’t work:

    string username = "username";
    string password = "password";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url-here");
    request.Credentials = new NetworkCredential(username, password);
    

    To work around this you need to force the authentication to be sent. I found the details of how to do this in a blog post by Ian Dykes here:

    http://devproj20.blogspot.com/2008/02/assigning-basic-authorization-http.html

    But for fullness here’s the code:

    string username = "username";
    string password = "password";
    byte[] authBytes = Encoding.UTF8.GetBytes(username + ":" + password.ToCharArray());
    request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);
    

    Permalink

    | Leave a comment  »

     
  • How I approach problem solving in code?

    Phil Leggetter 6:44 pm on October 23, 2009 | 5 Permalink | Reply
    Tags: .NET, Agile, , Coding,

    Recently I was posed the following question:

    Write a piece of code that prints all odd integer numbers between 1 and 99

    This really isn’t a difficult question but it still requires some thought. When I’m posed with any question I like to break things down into their constituent parts.

    Here’s the process I went through:

    Okay, so I’ll define two variables for a start and end value and there’s going to have to be a loop.

    int startValue = 1;
    int endValue = 99;
    for(int i = startValue;
         i <= endValue;
         i++)
    {
       // work out if "i" is an odd number
    }

    Now, for the odd number detection. And… after a few umms and errrs … I’m going to have to mod 2 (%2) the current value of i to work out if the value is odd. More … umms and errs. Okay, I’ve finally worked out that if something mod 2 is not equal to 0 it’s clearly an odd number. This took me longer than it should have but never mind. Once I’ve detected if i is an odd number I’ll then put the odd number into a list for use later.

    int startValue = 1;
    int endValue = 99;
    IList<int> oddValues = new List&lt;int&gt;();
    for(int i = startValue;
         i &lt;= endValue;
         i++)
    {
       if(i%2 != 0)
       {
          oddValues.Add(i);
       }
    }
    

    Those of you that are good at these little puzzles, or just think this is way too easy, might already be screaming at me about one of the following:

    • Why are you using a IList, why don’t you just print the value?
    • Odd numbers are always 2 apart so why aren’t you just increment i by 2 using i+=2?

    (More …)

     
  • Windows Azure - 503 Service Unavailable

    Phil Leggetter 12:44 am on August 28, 2009 | 0 Permalink | Reply
    Tags: .NET, , ,

    I finally got around to trying out ASP.NET MVC and while I was at it I thought I’d also give Windows Azure a whirl. Windows Azure doesn’t support ASP.NET MVC out of the box but Jim over on MSDN Blogs has written up the details of how to get ASP.Net MVC Projects running on Windows Azure.

    Problem

    So, I followed the instruction from the blog and uploaded the package and configuration file, waited for the staging application to get into a runnable state, and clicked on the staging link…

    Windows Azure – 503 Service Unavailable

    Clearly not a good thing! In addition to this error I occasionally got a random network error or a full-on connection error reported by the browser.

    I tried googling for this error but there was nothing about this problem after deploying to the real Windows Azure hosting (the cloud). As far as I can tell there’s no way of getting any debug information or logs to work out what’s going wrong. Maybe this is something that Microsoft will add later on?

    Solution

    Then it struck me that I hadn’t actually set up any kind of TableStorage on my Windows Azure hosting so I removed all mentions of storage and database connections from the MVC application. This included editing Web.config and removing the following.

    • The <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 element.
    • The <connectionStrings> element
    • The <membership>element
    • The <authentication mode=”Forms”> element
    • The <profile> element
    • The <roleManager>element

    Update: I’ve found that without the <authentication>element I started getting the error below so you may want to instead replace it with :

    CCT: Role instances did not start within the time allowed. Please try again. If you continue to encounter this behavior please try shutting down the Development Fabric.

    I’m guessing that not all of these elements actually need to be removed but doing so resulted in the MVC ASP.NET application working in Windows Azure.

    Update 2: ServiceDefinition.csdef

    If you happen to get network timeouts or 404 network connection issues it’s worth checking your ServiceDefinition.csdef to make sure that you’ve got port 80 configured. I noticed that mine had updated to port 8080 so obviously I couldn’t access the staging site on port 80 like I was trying. If you do have the port set to something else other than port 80 you can use that port to access your application but the ServiceDefinition.csdef does have a comment in there telling you your application should be on port 80 so you are probably best to stick with that.

    <!– Must use port 80 for http and port 443 for https when running in the cloud –>
    <InputEndpoint name=”HttpIn” protocol=”http” port=”80″ />

     
  • System.Security.SecurityException: That assembly does not allow partially trusted callers

    Phil Leggetter 12:51 am on August 26, 2009 | 1 Permalink | Reply
    Tags: .NET, , ,

    I was writing a Windows Azure ASP.NET MVC application and when making a call to a page I received the following exception:

    System.Security.SecurityException: That assembly does not allow partially trusted callers

    In my application I’m using Castle Windsor for dependency injection and when trying to resolve a service using:

    }

    IGravatar gravatar = MvcApplication.Container.Resolve<IGravatar>();

    I received this message. To solve things I needed to update the WebRole element in the ServiceDefinitions.csdef file so that enableNativeCodeExecution is enabled.

    <WebRole name=”TwitterGravatarMVC” enableNativeCodeExecution=”true”>

    I found the solution, detailed above, on the Azure Services Platform Developer Centre forum.

     
  • Live Mesh - my experience

    Phil Leggetter 11:52 pm on November 30, 2008 | 0 Permalink | Reply
    Tags: .NET, Live Mesh, , , Synchronisation

    I think that Live Mesh will be really useful. I think it provides great benefit to individual users, such as myself, and has great potential to be used by software solution developers. I’m presently using it to synchronise some files that I want to backup and be available wherever I am.

    I originally had a lot of web files being synchronised, around 700MB, but the synchronisation was taking ages and killing my CPU. CPU was sitting at 100% and there was no sign of things completing. I think it was having a problem with the volume of small files.

    To summarise: it’s a great product with bags of potential but something needs to be done to reduce the CPU usage when dealing with the synchronisation of a large volume of small files.

     
  • How to make a cross domain web request with SilverLight 2

    Phil Leggetter 12:16 pm on October 24, 2008 | 0 Permalink | Reply
    Tags: .NET, .NET, , crossdomain, , ,

    To make a cross domain web request with SilverLight 2 really isn’t that tough. I did have some problems with RC0 but I have no idea why. I just tried writing a little app to do this and it worked straight away.
    (More …)

     
  • .NET Obfuscators

    Phil Leggetter 10:45 pm on June 26, 2008 | 0 Permalink | Reply
    Tags: .NET, Obfusators, Software,

    As I was cleaning up the contents of my desktop I came across a text file containing a list of links to .NET obfuscator offerings. I created this list when researching options about six months ago. The main criteria was that the obfuscator could be executed as part of an automated build process.

    Here’s the contents of the list:

     
  • How to sign an unsigned DLL

    Phil Leggetter 11:37 am on December 7, 2007 | 0 Permalink | Reply
    Tags: .NET

    Taken from: http://forums.microsoft.com/MSDN/showpost.aspx?postid=420884&siteid=1

    Step 1: Dis-assemble the assembly
    ildasm myTest.dll /out:myTest.il

    Step 2: Re-Assemble using your strong-name key
    ilasm myTest.il /res:myTest.res /dll /key:myTest.snk /out:myTestSN.dll

    This code work perfectly to assign strong name.

    for verification you can use following command,
    sn -vf myTestSN.dll

     
  • .NET 2.0 Documentation Generation with Sandcastle

    Phil Leggetter 1:02 pm on May 11, 2007 | 0 Permalink | Reply
    Tags: .NET,

    I’ve been aware of Microsoft’s documentation generation system called Sandcastle for a while now but only recently have I had a chance to use it.

    Microsoft Sandcastle

    With NDoc all but dead (see NDoc 2.0 Alpha) Sandcastle is now one of very few free alternatives that I could find to generate documentation from my coding comments. However, it did take a bit of searching and playing around to get things working.

    (More …)

     
  • C# - Get Windows Temporary Directory

    Phil Leggetter 10:25 am on February 2, 2007 | 0 Permalink | Reply
    Tags: .NET, , ,

    Once you know it you’ll probably never forget it…but wait, this is the second time I’ve googled for this. I’d best post how you “get the Windows temporary directory using C#“!

    string tempPath =
        System.IO.Path.GetTempPath();
     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
esc
cancel