Run an XMLA query using WebApi from C #

0

My WebApi in C # is 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 having an error return in the Analisys Service that says the following:

  

The Discover element at line 7, column 72 (namespace urn: schemas-microsoft-com: xml-analysis) may not appear in Envelope / Body / Execute / Command.

Does anyone know what I might be doing wrong?

    
asked by anonymous 10.07.2018 / 16:12

1 answer

-1

I have completely changed the service and now it is working perfectly. I'll post the code because I believe many people have had the same difficulty 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;
        }
    }
}
    
10.07.2018 / 18:33