Skip to main content

Invalid URI: The format of the URI could not be determined (System.UriFormatException)

I have recently seen a web application throwing System.UriFormatException error with this message: Invalid URI: The format of the URI could not be determined. It is one of the errors that is almost always related to code, not IIS or other components.

Here is the stack trace and description from DebugDiag dump:

Exception Details:
System.UriFormatException
Invalid URI: The format of the URI could not be determined.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at Varonis.EventHandlerAuditor.WebApplicationConfiguration.IsWebApplicationMatches(String webApplicationUrl)
at Varonis.EventHandlerAuditor.CifsQueueConnectionsMng.ConnectWebApplication(Object connectFilerDataObj)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart(Object obj)

System.UriFormatException error with this message: Invalid URI: The format of the URI could not be determined

Root cause

This issue occurs when the input value is not what the application was expecting. In other words, the application was expecting a valid URI but it received a value that is not in URI format.

For example, in my case, the value was went into the function was “SERVER01“. Since this is not a valid URI, the application threw Invalid URI error at Uri uri = new Uri(webApplicationUrl); line.

public static bool IsWebApplicationMatches(string webApplicationUrl)
    {
      if (string.IsNullOrEmpty(webApplicationUrl))
        return true;

      Uri uri = new Uri(webApplicationUrl);
    }

Another common exception type is BadImageFormatException. Check this post for more details and solution: Attempt to load Oracle client libraries threw BadImageFormatException

Solution for System.UriFormatException Invalid URI: The format of the URI could not be determined

The solution is to implement try-catch in that function to handle it. The root cause why it is happening depends the user behavior (what value is coming into the function). No matter what the root cause is, the application should catch the exception. Otherwise, it will continue to crash.

Look at the stack to find out the assembly that is throwing the error. If it’s a third-party component (it was in my case), you will need to contact them. They might have a fix or updated assembly.

It is not necessary but you can also investigate how that value is passed to that function. Think about the inputs, use-cases, and other application related information. Talk to your user to find out steps reproduce it. In most cases, these kind of string inputs can be ignored. For example: It could be because of a user who enters invalid information in a form. However, no matter who enters it how, the application shouldn’t crash because of it.

Based on how you implemented StringBuilder, you may run into OutOfMemoryException as well. Here is the solution to this issue: OutOfMemoryException caused by StringBuilder

Leave a Reply

Your email address will not be published. Required fields are marked *