I am trying get roles inside a database in Azure AnalysisServices.
I am using NuGet package Microsoft.AnalysisServices.retail.amd64
Below is the code that I am using:
using Microsoft.AnalysisServices.Tabular;
using System;
namespace AnalysisServicesRolesRetrieval
{
class Program
{
static void Main(string[] args)
{
// Your Analysis Services server name
string serverName = "asazure://eastus.asazure.windows.net/yourAnalysisServicesServerName";
// Authentication details
string userName = $"app:{config.AnalysisServiceSpnClientId}@{config.TenantId}";
string password = "SpnSecret";
// Connect to the server
var connStr = $"Provider=MSOLAP;Data Source={serverName};User ID={userName};Password={password};Persist Security Info=True;Impersonation Level=Impersonate;";
Server server = new Server();
server.Connect(connStr);
// Iterate through each database and model to list roles
foreach (Database db in server.Databases)
{
Console.WriteLine($"Database: {db.Name}");
foreach (Model model in db.Model)
{
Console.WriteLine($" Model: {model.Name}");
foreach (Role role in model.Roles)
{
Console.WriteLine($" Role: {role.Name}");
// List members of the role
foreach (RoleMember member in role.Members)
{
Console.WriteLine($" Member: {member.Name}");
}
}
}
}
// Disconnect from the server
server.Disconnect();
}
}
}
Error Message: $exception {"Could not load type 'System.Runtime.Remoting.Messaging.CallContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.":"System.Runtime.Remoting.Messaging.CallContext"} System.TypeLoadException
I also see below warning while building:
Severity: Warning(active)
Code: NU1701
Description: Package 'Microsoft.AnalysisServices.retail.amd64 19.84.1' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net6.0'. This package may not be fully compatible with your project.
Is there any other way to get roles from an Azure Analysis Services database? I am able to achieve this using this PowerShell script. But I need this in C#.
Your running .NET 6, which is a version of .NET Core, not .NET Framework. So you're using the wrong nuget package. Use https://www.nuget.org/packages/Microsoft.AnalysisServices.NetCore.retail.amd64