Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.InvalidOperationException : An invalid request URI was provided. Either the request URI must be an absolute URI or BaseAddress must be set. #198

Open
sam-wheat opened this issue Jul 1, 2024 · 5 comments
Labels
answered question Further information is requested

Comments

@sam-wheat
Copy link

System.InvalidOperationException : An invalid request URI was provided. Either the request URI must be an absolute URI or BaseAddress must be set.

This seems to be the default error message and it appears it gets thrown regardless of what error actually occurs.

For example I can shut down my api server and rather than get a 404 I get the error above.

I can also intentionally provide an invalid section name for the api config and I get the same error:

tokenAcquirerFactory.Configuration.GetSection("ThisDoesNotExist"); 

Now, I may be deserving of some error - but that error certainly seems like it is not the one I should be getting:

"MyApi": { 
	"BaseUrl": "https://localhost:5010/", 
	"RelativePath": "api/MyController/ThisMethod",
	"RequestAppToken": true,
	"Scopes": [ "api://yada-6e75f60a2ef7/.default" ] // . E.g. 'api://<API_APPLICATION_ID>/.default'
},

BaseUrl is provided and the RelativePath component is valid - even if it is invalid it is not malformed.

  IConfiguration appSettings = new ConfigurationBuilder().AddJsonFile(configFilePath, false).Build();
  var tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance();
  tokenAcquirerFactory.Services.AddDownstreamApi("MyApi", appSettings);
  tokenAcquirerFactory.Configuration.GetSection("MyApi");
  var sp = tokenAcquirerFactory.Build();
  var api = sp.GetRequiredService<IDownstreamApi>();
  string result = await api.GetForAppAsync<string>("MyAPI");  

How can I troubleshoot this error?

@jmprieur
Copy link
Contributor

jmprieur commented Jul 2, 2024

You need to do that:

 - IConfiguration appSettings = new ConfigurationBuilder().AddJsonFile(configFilePath, false).Build();
  var tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance();
-  tokenAcquirerFactory.Services.AddDownstreamApi("MyApi", appSettings);
+  tokenAcquirerFactory.Services.AddDownstreamApi("MyApi", tokenAcquirerFactory.Configuration.GetSection("MyApi"));
  var sp = tokenAcquirerFactory.Build();
  var api = sp.GetRequiredService<IDownstreamApi>();
  string result = await api.GetForAppAsync<string>("MyAPI"); 

but you also need an "AzureAd" section unless you want to use Managed identity

@jmprieur jmprieur added question Further information is requested answered labels Jul 2, 2024
@sam-wheat
Copy link
Author

Thank you @jmprieur

My config file does not exist in the bin folder so I create it manually and pass it to AddDownstreamApi:

IConfiguration appSettings = new ConfigurationBuilder().AddJsonFile(configFilePath, false).Build();

The config file does have an AzureAd section. Are you saying that is what is causing the problem? If so, how can I use a config file that does not get copied to bin (secrets file)? In prod it will get copied - however in dev the config file is not source controlled so I hardcode a path and load it as shown.

@sam-wheat
Copy link
Author

I'm still stuck on this - any suggestions how to get around this error?

@jmprieur
Copy link
Contributor

Which chapter of this sample is it about? do you mind providing a link?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants