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

吃可爱长大的小学妹 提交于 2019-11-30 00:26:10

问题


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.


回答1:


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.




回答2:


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.




回答3:


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 });



回答4:


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.




回答5:


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 });




回答6:


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 })



来源:https://stackoverflow.com/questions/30558827/method-not-found-system-string-system-string-formatsystem-iformatprovider-sy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!