Solving development problems  |  About this blog

Archive for the ‘ASP.NET’ Category

The service cannot be activated due to an exception during compilation. The exception message is: This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.

You can solve this issue by adding the following code to your web.config:

<system.serviceModel>
	...
	<serviceHostingEnvironment>
	  <baseAddressPrefixFilters>
	  	<add prefix="http://sample.domain.tld/Sample/"/>
	  </baseAddressPrefixFilters>
	</serviceHostingEnvironment>
	...
</system.serviceModel>

Compiler Error Message: CS0016: Could not write to output file – Temporary ASP.NET Files – Access is denied

Everything was working perfectly and then suddenly during ASP.NET (MVC) development you got this error from your IIS 7 server (in our case from Windows 7 64-bit):
Compiler Error Message: CS0016: Could not write to output file ‘c:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\myapp\xxxxx\xxxxx\App_GlobalResources.xxxxxxx.dll’ — ‘Access is denied.’
So, what goes wrong? You don’t know because it was working perfectly previous day, it is also compiles OK but not working anymore on your IIS webserver and, all you got, is yellow screen with this error message.

We searched a little bit and found that the problem appeared because the permission were lost on following Temporary folders:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\

C:\Windows\Temp

Recipe

  • Gave a full permission to NETWORK SERVICE user to these folders (extreme case is to give full permission to Everyone user to these folders – do this only it doesn’t work)
  • Restart IIS using iisreset command

IIS started to serve pages again….

Publish on Twitter nice and easy from ASP.NET MVC (C#)

How can you automatically post message to Twitter from C#, ASP.NET MVC (from your application)?

First, setup new Twitter application under your Twitter account.

  1. Go to dev.twitter.com and login with your Twitter account
  2. Click on Add Application and enter the all needed data

You will get all all needed secret keys and tokens and they are needed for automatic tweeting.

  • Consumer key: xxxxxxxxxxxxxxxxxxxxx
  • Consumer secret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  • Access Token (oauth_token): xxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  • Access Token Secret (oauth_token_secret): xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Now, download Twitterizer API

  1. reference Twitterizer2.dll
  2. Put this code into your application (of course change the location from where you get your secret keys – we have a Settings class)
public void PublishOnTwitter(string message)
{
	OAuthTokens tokens = new OAuthTokens();
	tokens.ConsumerKey = Settings.TwitterConsumerKey;
	tokens.ConsumerSecret = Settings.TwitterConsumerSecret;
	tokens.AccessToken = Settings.TwitterToken;
	tokens.AccessTokenSecret = Settings.TwitterTokenSecret;

	TwitterStatus status = TwitterStatus.Update(tokens, message);
}

And that’s it!
Enjoy!

How to get user’s (visitor’s) IP address in your ASP.NET (MVC) application?

Extend HttpRequest by writting and extension:

using System;
using System.Web;
using System.Net;
using System.Linq;
using System.Data.Linq;

namespace Some.Yours.Namespace
{
  /// <summary>
  /// Extends HttpRequest class with additional methods.
  /// </summary>
  public static class HttpRequestExtensions
  {
    /// <summary>
    /// Gets the IP address of the request.
    /// This method is more useful than built in because in
    /// some cases it may show real user IP address even under proxy.
    /// The <see cref="System.Net.IPAddress.None" /> value
    /// will be returned if getting is failed.
    /// </summary>
    /// <param name="request">The HTTP request object.</param>
    /// <returns>IPAddress object</returns>
    public static IPAddress GetIp(this HttpRequestBase request)
    {
      string ipString;
      if (string.IsNullOrEmpty(request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
      {
        ipString = request.ServerVariables["REMOTE_ADDR"];
      }
      else
      {
        ipString = request.ServerVariables["HTTP_X_FORWARDED_FOR"]
           .Split(",".ToCharArray(),
           StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
      }

      IPAddress result;
      if (!IPAddress.TryParse(ipString, out result))
      {
        result = IPAddress.None;
      }

      return result;
    }
  }
}

PDF files not Publish in ASP.NET MVC when deploying

We faced a problem when we added some PDF files into our media library of ASP.NET MVC website and noticed that these files are not Published automatically.
So you need in Visual Studio to click on each PDF file and under Properties (F4) just choose

  • Build Action=Content
  • Copy to Output Directory=Copy Always

Written by Avivo

June 1st, 2010 at 1:28 pm