Solved

Consume OData API via .NET Core

  • 28 November 2019
  • 1 reply
  • 970 views

Userlevel 5
Badge +15

I'm trying to use the Indicium OData API via a .NET Core 3.0 console application. I receive an exception on inserting records, while the record is inserted in the database. I have no clue why. I'm not sure if this is an issue in the Indicium OData API or the .NET nuget package.

For example I want to add a ‘server’ to the IAM database. By using a generated piece of code (see https://docs.microsoft.com/nl-nl/odata/client/basic-crud-operations) this is quite easy to set up;

Add the indicium OData API via “Unchase OData Connected Service”
Configure the metadata ($metadata.xml is the result of http://server/indicium/iam/iam/$metadata)

With the following code you retrieve the ‘server’ table and insert a new record in to it:

using SQLSERVER_IAM;
using System;
using System.Net;

namespace IndiciumTest
{
class Program
{
static void Main(string[] args)
{
var uri = new Uri("http://server/indicium/iam/iam");
var container = new Container(uri);

container.Credentials = new NetworkCredential("userName", "password");

// iterating servers works.
var servers = container.Server.ExecuteAsync().Result;
foreach(var s in servers)
{
Console.WriteLine(s.Server_name);
}

var server = new Server
{
Server_address = "localhost",
Server_name = "localhost",
Rdbms_type = 0
};

container.AddToServer(server);

// saving works, but gives an exception
var result = container.SaveChangesAsync().Result;
}
}
}

On the line ‘var result = container.Save...’ it gives the following exception:

System.AggregateException
HResult=0x80131500
Message=One or more errors occurred. (An error occurred while processing this request.)
Source=System.Private.CoreLib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at IndiciumTest.Program.Main(String[] args) in C:\Users\user\source\repos\IndiciumTest\IndiciumTest\Program.cs:line 33

Inner Exception 1:
InvalidOperationException: An error occurred while processing this request.

Inner Exception 2:
InvalidOperationException: The 'Location' header value specified in the response must be an absolute URI.

The server ‘localhost’ is in this case added to the iam database, but due to the fact it gives an exception I'd almost assume it failed. Any ideas on this?

icon

Best answer by Vincent Doppenberg 28 November 2019, 17:31

View original

1 reply

Userlevel 6
Badge +3

Hello René,

When inserting a record, Indicium will return a Location header in the response which contains a relative URL that points to the newly inserted record. The OData library that you are using states that “the URL in the Location header must be absolute”. However, this is not true, relative URLs are allowed. Luckily, you are not the first person to use this library and run into this issue, so we have a workaround available for you.

The problem will be fixed if you set the following extended property to true in IAM on the applications affected by this issue: use_absolute_location_headers.

Please note that this can cause issues when Indicium is located behind a reverse proxy. This is also the reason why we use relative URLs in the first place. Let's say Indicium is hosted at http://internal/indicium and exposed through the reverse proxy https://external/indicium, then the Location headers returned by Indicium will contain internal URLs which cannot be used by clients. We are currently looking into other solutions for this problem which will allow us to always return absolute URLs again.

I hope that explains why this is happening and how you can avoid it.

Reply