Run an XMLA query using C#Webapi

Asked

Viewed 56 times

0

My C# Webapi looks like this.

using Microsoft.AnalysisServices.AdomdClient;
using System;
using System.Data;
using System.Web.Http;
using System.Xml;

namespace FastReport.WebApi.Controllers
{
    [RoutePrefix("FastReport")]
    public class ConsultasController : ApiController
    {
        [Route("ConsultarCubo")]
        [HttpPost]
        public string ConsultarCubo()
        {
            AdomdConnection conn = new AdomdConnection("Data Source=localhost;");
            conn.Open();

            AdomdCommand cmd = new AdomdCommand();

            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = @"<Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'>
                                    <Body>
                                        <Discover xmlns='urn:schemas-microsoft-com:xml-analysis'>
                                            <RequestType>MDSCHEMA_DIMENSIONS</RequestType>
                                            <Restrictions>
                                                <RestrictionList>
                                                    <CATALOG_NAME>Bahamas</CATALOG_NAME>
                                                    <CUBE_NAME>DirectorData Bahamas</CUBE_NAME>
                                                </RestrictionList>
                                            </Restrictions>
                                            <Properties>
                                                <PropertyList>
                                                    <Catalog>Bahamas</Catalog>
                                                    <LocaleIdentifier>22</LocaleIdentifier>
                                                </PropertyList>
                                            </Properties>
                                        </Discover>
                                    </Body>
                                </Envelope>";

            try
            {
                System.Xml.XmlReader reader = cmd.ExecuteXmlReader();
                var resultadoXml = reader.ReadOuterXml();
                reader.Close();

                return resultadoXml;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}

When I run the method, I’m getting an error return in Analisys Service that says the following:

The 'Discover' element in row 7, column 72 (namespace urn:schemas-microsoft-com:xml-analysis) cannot appear in Envelope/Body/Execute/Command.

Does anyone know what I might be doing wrong ?

1 answer

-1

I completely modified the service and now it’s working perfectly. I’m going to post the code because I believe that a lot of people have been through the same hardship as me.

using System.Web.Http;
using Microsoft.AnalysisServices;
using System.IO;
using System.Net.Http;

namespace FastReport.WebApi.Controllers
{
    [RoutePrefix("FastReport")]
    public class ConsultasController : ApiController
    {
        [Route("ConsultarCubo")]
        [HttpPost]
        public string ConsultarCubo(HttpRequestMessage xmlEnviado)
        {
            Server server = new Server();

            server.Connect("Data Source=localhost");

            var xmlRecebido = xmlEnviado.Content.ReadAsStringAsync().Result;

            System.IO.TextReader xmlaRecebido = new StringReader(xmlRecebido);
            System.Xml.XmlReader xmlaResponseFromServer;

            xmlaResponseFromServer = server.SendXmlaRequest(XmlaRequestType.Undefined, xmlaRecebido);

            xmlaResponseFromServer.MoveToContent();
            string fullEnvelopeResponseFromServer = xmlaResponseFromServer.ReadOuterXml();

            xmlaResponseFromServer.Close();

            return fullEnvelopeResponseFromServer;
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.