23

I have a Web API 2 project with help pages that runs fine locally but throws this error when I push it to Azure:

Method not found: 'System.String System.String.Format (System.IFormatProvider, System.String, System.Object)

I temporarily turned custom errors off so full stack trace can be seen here

The error is originating from this line of code:

string selectExpression = String.Format(CultureInfo.InvariantCulture, MethodExpression, GetMemberName(reflectedActionDescriptor.MethodInfo));

See Line 96 here

The full source code is available on GitHub

I'm not even sure where to go with this one.

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Dale Alleshouse
  • 1,627
  • 2
  • 17
  • 24
  • Please post the code snippet containing the offending line, instead of a link to a github repo. – Alex May 31 '15 at 15:01
  • I have seen similar behaviors where the code had a right to left casting ambiguity. When going from .NET 3 to .NET 4 . The solution ended up being something like this ((string)(MyFunction).ToString()) – Sql Surfer May 31 '15 at 15:05
  • @Alex - I added more detail. Sorry I didn't add it in initially, I didn't think it would be important because the missing method is from the dot net framework. – Dale Alleshouse May 31 '15 at 15:21
  • Try `String.Format(CultureInfo.InvariantCulture, MethodExpression, new object[]{GetMemberName(reflectedActionDescriptor.MethodInfo)});` I expect the underlying cause to be related to incorrect targeting. Either you're targeting the wrong version or the wrong variant of the framework. – CodesInChaos May 31 '15 at 15:22
  • 3
    [That overload was added in .NET 4.5](https://msdn.microsoft.com/en-us/library/vstudio/dn906224(v=vs.110).aspx). What framework is your Azure platform running? – CodeCaster May 31 '15 at 15:23
  • @CodesInChaos - Changing that line of code produces a slightly different error - Method not found: 'System.String System.String.Format(System.IFormatProvider, System.String, System.Object, System.Object)'. – Dale Alleshouse May 31 '15 at 15:25
  • @DaleAlleshouse Is that new error in a different line? You could apply the same work around there. But like CodeCaster remarked, you should either upgrade the server or target a lower version in your project. The error results from overloads your compiler sees but which are missing on the server. – CodesInChaos May 31 '15 at 15:27
  • @CodesInChaos - It's the same line. My project is targeting 4.6 and Azure is set to 4.5. This shouldn't be a problem, right? – Dale Alleshouse May 31 '15 at 15:27
  • 1
    @DaleAlleshouse you may want to ensure you are running on the platform version you expect by checking the version in `Environment.Version` (or like this: https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx#net_d). The most logical explanation for what you are observing is that you are not running a CLR version 4.5 or higher. – Alex May 31 '15 at 15:37
  • 3
    Oh, on that [MSDN page I linked to](https://msdn.microsoft.com/en-us/library/vstudio/dn906224(v=vs.110).aspx) it says _".NET Framework 4.6 and 4.5"_ at the top, but _"Supported in: 4.6"_ at the bottom. Test it on a machine running 4.6. – CodeCaster May 31 '15 at 15:41
  • @CodeCaster - That was it. I guess they don't have 4.6 on Azure yet. I changed the target framework on my project to 4.5 and it works. I guess that's what I get for using RC bits... – Dale Alleshouse May 31 '15 at 15:57
  • Stupid question, how can I mark this question as answered? – Dale Alleshouse May 31 '15 at 15:59
  • 1
    So, is the overload only supported in 4.6 or is it also supported in 4.5? My project targets .NET 4.5.2, but I am getting this error from a machine that is supposed to have .NET 4.5.2 installed. Isn't is supposed to be recognised as a `params object[]`? – Philip Atz Nov 24 '15 at 13:27

6 Answers6

30

According to its MSDN page, the overload you're using is only supported on .NET 4.6.

Either configure the host to run .NET 4.6 or change the target framework of the project to 4.5 and recompile.

In 4.5 there's a params object[] overload which will then be chosen, without having to alter your code.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
7

This doesn't make sense. We've had a line of code like this in our application since 2009

String.Format(CultureInfo.CurrentCulture, "You must specify a new password of {0} or more characters.", _membershipService.MinPasswordLength);

Recently we upped the project to .NET 4.6 and now, for me at least, this line breaks with the same error. So obviously the new overload is breaking something, and the method is not new.

nportelli
  • 3,934
  • 7
  • 37
  • 52
7

If you can neither upgrade host to 4.6 nor downgrade project to 4.5 there is a workaround : pass an "object[]" as args instead of an "object". So you will force usage of the "params object[]" overload. Example :

return string.Format(formatProvider, "{0:" + format + "}", new object[] { value });
bN_
  • 772
  • 14
  • 20
2

In case this helps anyone. We encountered this issue recently after upgrading our development environment to VS2015 (Our target environment is .Net 4)

Our C++/clr projects had not been setup correctly to use the /clr switch i.e. they were set to no common language support, even though we were using the clr. This didn’t cause an issue until we upgraded to VS2015.

I’m not completely clear on why this works. I’m guessing c++/clr project must bind to a specific version of the CLR runtime at compile time. I’d be interested if someone could explain this more clearly.

chrism233
  • 97
  • 10
1

We are using custom build server. Even if project TargetFrameworkVersion is v4.5.1, when .net 4.6.1 installed to build server and single argument passed as format argument, the compiler prefers to use this overload

public static string Format(IFormatProvider provider, string format, object arg0)

instead of

public static string Format(IFormatProvider provider, string format, params object[] args)

Only solution is creating and passing array argument

Example:

string.Format(CultureInfo.CurrentCulture, "Hello {0}", new[] { name });

Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
ali kucuk
  • 31
  • 1
1

Azure Data Lake Analytics runs on .NET 4.5 today. So we don't support .NET 4.6 assembly scenarios and this kind of errors are possible. To avoid it, you should rebuild your assembly in .NET 4.5.

The following "non-recommended" workaround might work with a .NET 4.6 assembly: Rewriting string.Format(provider, format, arg0, arg1) into string.Format(provider, format, new object[] { arg0, arg1 })

CodeCaster
  • 147,647
  • 23
  • 218
  • 272