WCF: This collection already contains an address with scheme http

If you have this error you may have done some Google searches and found some complex explanations and solutions.

The good news is this is not one of them.

Here is the solution:

Add the following to the Web.Config:


<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
<baseAddressPrefixFilters>
<add prefix="http://www.mydomain.com/application_directory/" />
</baseAddressPrefixFilters>
</serviceHostingEnvironment>

The error is result of having multiple addresses available for the domain.  (e.g. “www.domain.com”, “domain.com”, etc.).  This entry in the config file ensures there is only one address.

View Detailed Server Errors in Classic ASP and ASP.NET at GoDaddy and Rackspace Cloud

Override Generic Server 500 Errors

Here is the solution:

Add the following code to your Web.config:


<system.webServer>
<httpErrors errorMode="Detailed" />
</system.webServer>

Note: If it does not work you may have an error in your Web.Config.  If the Web.Config cannot load it will not be able to enable the detailed errors.  Also, the host must have this option enabled.  If the applicationHost.config file on the server has detailed errors disabled you will never be able to view them.

WCF – Request for the permission of type ‘System.Web.AspNetHostingPermission

System.Security.SecurityException: Request for the permission of type ‘System.Web.AspNetHostingPermission

Some people say this error can occur if you have resources (e.g. assemblies, etc.) on a network path. In such a case you need to enable Full Trust. It is a simple solution. However, this post is about another cause for this error. It will occur when you try to change the site trust level and you do not have permission.

Here is the solution:

Find and remove the following code in your Web.Config:

<trust level="[Full|High|Medium|Low|Minimal]"  />

WCF – Required permissions cannot be acquired.

System.Security.Policy.PolicyException: Required permissions cannot be acquired.

This error is caused when you have a dll in your bin folder that is not compatible with Medium Trust. It does not matter if you are actually using the dll. You need to remove the dll completely.

In my case, I needed to remove the dll that came with memory profiler. Also, Enterprise Library version 4.1 is compatible with Medium Trust, but all the extra “design” dlls that get generated at compile time are not.

I have to go through my bin folder and remove all the dlls that end with “design” manually.

Here is an example of a dll that would cause this error:

Microsoft.Practices.EnterpriseLibrary.Data.Configuration.Design.dll

WCF – 500.19 The page you are requesting cannot be served because of the extension configuration

This error may occur when you are in medium trust, such as GoDaddy or RackSpaceCloud.  It may also say “XML Parsing Error: no element found”.

Here is the solution:

You need to add the following HTTP Handler to your domain. You can do this via IIS, but I will explain how to do it very easily via the Web.config.

Simply add the following code:

<system.webServer>
<handlers>
<add name="svc" preCondition="integratedMode" verb="*" path="*.svc" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</handlers>
</system.webServer>
<system.web>
<httpHandlers>
<remove verb="*" path="*.svc"/>
<add path="*.svc" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false"/>
</httpHandlers>
</system.web>

Bizzaro Firefox hits 1 Billion downloads – Not impressed!

If you are a Seinfeld fan you completely get the bizzaro reference. If you are a Firefox 3.5 early adopter you probably do not need a reference to know what I am talking about. The latest release of Firefox is just plain bizarre.

The browser that we have all come to know as fast and secure has been reduced to a buggy mess. Half a dozen crashes a day, 2-5 minute start-up times, screen burning (or rather image retention), lockups, etc.  A quick Google search reveals I am not alone. Yet, somehow, Firefox has managed to use v3.5 to catapult itself to 1 billion downloads. I just wonder what will happen to their reputation if just a fraction of those users are experiencing the same issues as myself. At first, I thought I could hold out for the next update, but 3.5.2 was not any better. If not for Google Chrome’s unbearably slow file downloading I would probably say goodbye to Firefox forever.  In fact, I have actually starting using IE again [ouch].  Atlas its time to revert.

What is that you ask? How do I revert back to Firefox 3 without loosing all my bookmarks? It’s easy… just read on…

Firefox 3.5 slow and crashes: How to revert back to Firefox 3

The first step is to uninstall Firefox 3.5.  Be sure you do NOT check the box to delete your private files and bookmarks.

Next, download the old version and install.  Easy!

ASP.NET – Getting values from Resx file

Programmatically retrieve text from a Resx file based on a specific culture

Use .resx files for storing multiple languages inside a WCF Service without compiling the files.

Problem:

I need to develop a multiple language web service.  The articles on the web focus on two things.

  1. Using resx files with web pages.  You change the culture in your web site and asp.net automatically uses the correct resx file.  That is, assuming you name it in the correct format and place it in the correct folder.
  2. Using resx files globally in your application.  This unfortunately compiles your resx files though and you cannot easily edit them .

I needed to be able to programmatically retrieve the correct resx file, keep the resx file uncompiled so it could be edited, and do so within a web service.

This post assumes you have already found a bunch of articles via Google about how to set a thread’s culture, or a page’s culture. I found those articles as well. They were helpful learning how use .Resx files for a multiple language web site. However, I needed to use multiple languages in a WCF Service and ASMX Web Service.

Solution:

The solution is to use the resource manager class, but override it so you can use resx files.  Then you can easily grab the text you need from the correct language.  You can use this method in a web service or anywhere in your application.

Here is the code:

ResxResourceManager.vb

Imports System.ServiceModel
Imports System.Runtime.Serialization
Imports System.Resources
Imports System.Reflection

Namespace MyNamespace

Public Class ResxResourceManager
Inherits ResourceManager

Public Sub New(ByVal baseName As String, ByVal resourceDir As String)

BaseNameField = baseName
ResourceSets = New Hashtable()

Dim baseType As Type = Me.GetType.BaseType
Dim flags As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.SetField

baseType.InvokeMember("moduleDir", flags, Nothing, Me, New Object() {resourceDir})
baseType.InvokeMember("_userResourceSet", flags, Nothing, Me, New Object() {GetType(ResXResourceSet)})
baseType.InvokeMember("UseManifest", flags, Nothing, Me, New Object() {False})
End Sub

Protected Overrides Function GetResourceFileName(ByVal culture As Globalization.CultureInfo) As String

Dim resourceFileName As String = MyBase.GetResourceFileName(culture).ToLower

Return resourceFileName.Replace(".resources", ".resx")

End Function

End Class

End Namespace

Usage:


Dim sBinDirectory As String
sBinDirectory = HostingEnvironment.ApplicationPhysicalPath & "App_LocalResources\"

Dim rm As New ResxResourceManager("States", sBinDirectory)
Dim StateName As String = CStr(rm.GetString(StateCode, CultureInfo.CreateSpecificCulture("en-EN")))

Notes:

  • I use “HostingEnvironment.ApplicationPhysicalPath” instead of Server.Mappath since this is a WCF Service
  • You must add some references to your project :
    - Imports System.ServiceModel
    - Imports System.Runtime.Serialization
  • In the code sample above “States” is the prefix of the resx file.  So I would have the following files:
    - States.resx
    - States.it.resx
    - States.es.resx
  • Of course, you place these files inside the “App_LocalResources” directory which is set to the “sBinDirectory” variable
  • In the code above StateCode is a variable.  It is the name of value you want from the resx file.  In my example it would be something like “PA” or “NY” or “CA”.  It would return the text “Pennsylvania”, etc.

ASP.Net Get Currency Symbol From Currency Code

ASP.NET – Currency Symbol From Currency Code

Figure out the currency symbol for a culture by using their currency code.

Currency.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Globalization

Namespace MyNamespace

Public Class Currency

Public Function GetCurrencySymbol(ByVal CurrencyCode As String) As String

Dim regionInfo As RegionInfo = (From culture In CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures) _
Let region = New RegionInfo(culture.LCID) _
Where [String].Equals(region.ISOCurrencySymbol, CurrencyCode, StringComparison.InvariantCultureIgnoreCase) _
Select region).First()

Return regionInfo.CurrencySymbol

End Function

End Class

End Namespace

Usage:

Dim CurrencySymbol As String = CurrencyObj.GetCurrencySymbol("USD")