Tuesday, September 28, 2021

[.NET 6] System.StackOverflowException after upgrade VS2022 to Preview 4.1 from 3.1



After upgrade VS2022 to Preview 4.1 from 3.1, my project throw StackOverflowException (after fix lots of reference error)
System.StackOverflowException
  HResult=0x800703E9
  Message=Exception of type 'System.StackOverflowException' was thrown.

Stack overflow.
Repeat 2291 times:
--------------------------------
   at Microsoft.Extensions.Primitives.ChangeToken+ChangeTokenRegistration`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnChangeTokenFired()
   at Microsoft.Extensions.Primitives.ChangeToken+ChangeTokenRegistration`1+<>c[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<RegisterChangeTokenCallback>b__7_0(System.Object)
   at System.Threading.CancellationTokenSource.Invoke(System.Delegate, System.Object, System.Threading.CancellationTokenSource)
   at System.Threading.CancellationTokenSource.Register(System.Delegate, System.Object, System.Threading.SynchronizationContext, System.Threading.ExecutionContext)
   at System.Threading.CancellationToken.Register(System.Delegate, System.Object, Boolean, Boolean)
   at System.Threading.CancellationToken.Register(System.Action`1<System.Object>, System.Object)
   at Microsoft.Extensions.Configuration.ConfigurationReloadToken.RegisterChangeCallback(System.Action`1<System.Object>, System.Object)
   at Microsoft.Extensions.Primitives.ChangeToken+ChangeTokenRegistration`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].RegisterChangeTokenCallback(Microsoft.Extensions.Primitives.IChangeToken)
--------------------------------
   at Microsoft.Extensions.Primitives.ChangeToken+ChangeTokenRegistration`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnChangeTokenFired()
   at Microsoft.Extensions.Primitives.ChangeToken+ChangeTokenRegistration`1+<>c[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<RegisterChangeTokenCallback>b__7_0(System.Object)
   at System.Threading.CancellationTokenSource.Invoke(System.Delegate, System.Object, System.Threading.CancellationTokenSource)
   at System.Threading.CancellationTokenSource+CallbackNode+<>c.<ExecuteCallback>b__9_0(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.CancellationTokenSource+CallbackNode.ExecuteCallback()
   at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean)
   at System.Threading.CancellationTokenSource.NotifyCancellation(Boolean)
   at System.Threading.CancellationTokenSource.Cancel()
   at Microsoft.Extensions.Configuration.ConfigurationManager.RaiseChanged()
   at Microsoft.Extensions.Configuration.ConfigurationManager.AddSource(Microsoft.Extensions.Configuration.IConfigurationSource)
   at Microsoft.Extensions.Configuration.ConfigurationManager.Microsoft.Extensions.Configuration.IConfigurationBuilder.Add(Microsoft.Extensions.Configuration.IConfigurationSource)
   at Microsoft.Extensions.Configuration.ChainedBuilderExtensions.AddConfiguration(Microsoft.Extensions.Configuration.IConfigurationBuilder, Microsoft.Extensions.Configuration.IConfiguration, Boolean)
   at Microsoft.Extensions.Configuration.ChainedBuilderExtensions.AddConfiguration(Microsoft.Extensions.Configuration.IConfigurationBuilder, Microsoft.Extensions.Configuration.IConfiguration)
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
   at Program.<Main>$(System.String[])

I know post to MS's developer forum is useless, and VS2022 can't downgrade now, so the only way is try and error by myself.

Turn out, the stackoverflow exception cause by Microsoft.Extensions.Configuration from this code:
builder.Services.AddSingleton<IConfiguration>(builder.Configuration);
After more dig-in, I found the framework already called this on Hosting , so maybe the duplicate call cause the stackoverflow exception, but still not sure why it worked on VS2022 preview 3.1, not work on preview 4.1.

Solution


Remove the line
services.AddSingleton<IConfiguration>(configuration);
(or similar) on your Program.cs

Wednesday, September 22, 2021

[JAVA] HMAC SHA384 example code

It was a bit hard to find correct HMAC SHA384 code for JAVA, so here to share my example code, hope can save some time to try and error.

import java.security.NoSuchAlgorithmException;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Mac;

public class SecureUtils {

private static String bytesToHex(final byte[] hash) {
    final StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < hash.length; i++) {
        final String hex = Integer.toHexString(0xff & hash[i]);
        if (hex.length() == 1) {
            hexString.append('0');
        }
        hexString.append(hex);
    }
    return hexString.toString();
}

public static void main(String[] args) throws NoSuchAlgorithmException {
    final String nonce = "test"; //your key
    final String message = "Password"; //your plain txt
    SecretKeySpec signingKey = new SecretKeySpec(nonce.getBytes(),"HmacSHA384");
    final Mac mac = Mac.getInstance("HmacSHA384");
    try{
        mac.init(signingKey);
        System.out.println(bytesToHex(mac.doFinal(message.getBytes())));
    }
        catch(Exception ex){    }
    }
}

You can test this example in here 

Check result with online hmac generator

Those code modify from stackoverflow HMAC SHA-384 in Java