Quantcast
Channel: Developer topics
Viewing all articles
Browse latest Browse all 18050

C# web application : import pbix file from azure

$
0
0

The following code works perfectly fine for C# console application. I used the same code for web application and it works very well on localhost too. However, when I publish it on Azure <XXX.azurewebsites.net>, it gives me error. I have found a solution that states that this code will run only on local application. It is not meant for azure. So can I know what changes to make so that it runs on Azure too? Thanks!

 

namespace PBIGettingStarted{classProgram{privatestaticstring clientID ="49df1bc7-db68-4fb4-91c0-6d93f770d1a4";privatestaticstring redirectUri ="https://login.live.com/oauth20_desktop.srf";privatestaticstring resourceUri ="https://analysis.windows.net/powerbi/api";privatestaticstring authority ="https://login.windows.net/common/oauth2/authorize";privatestaticstringPBIAPIUri="https://api.powerbi.com/v1.0/myorg";privatestaticAuthenticationContext authContext =null;privatestaticstring token =String.Empty;privatestaticstring datasetName ="testdataset";privatestaticstring groupID ="XX";privatestaticstring pbixPath = localname;privatestaticstring datasetDisplayName ="test";staticvoidMain(string[] args){string importResponse =Import(string.Format("{0}/groups/{1}/imports?datasetDisplayName={2}",PBIAPIUri, groupID, datasetDisplayName), pbixPath);Console.ReadLine();}staticstringAccessToken(){if(token ==String.Empty){TokenCache TC =newTokenCache();            authContext =newAuthenticationContext(authority, TC);            token = authContext.AcquireToken(resourceUri, clientID,newUri(redirectUri),PromptBehavior.RefreshSession).AccessToken;}else{            token = authContext.AcquireTokenSilent(resourceUri, clientID).AccessToken;}return token;}publicstaticstringImport(string url,string fileName){string responseStatusCode =string.Empty;string boundary ="---------------------------"+DateTime.Now.Ticks.ToString("x");byte[] boundarybytes =System.Text.Encoding.ASCII.GetBytes("\r\n--"+ boundary +"\r\n");HttpWebRequest request =(HttpWebRequest)WebRequest.Create(url);        request.ContentType="multipart/form-data; boundary="+ boundary;        request.Method="POST";        request.KeepAlive=true;        request.Headers.Add("Authorization",String.Format("Bearer {0}",AccessToken()));        using (Stream rs = request.GetRequestStream()){            rs.Write(boundarybytes,0, boundarybytes.Length);string headerTemplate ="Content-Disposition: form-data; filename=\"{0}\"\r\nContent-Type: application / octet - stream\r\n\r\n";string header =string.Format(headerTemplate, fileName);byte[] headerbytes =System.Text.Encoding.UTF8.GetBytes(header);            rs.Write(headerbytes,0, headerbytes.Length);            using (FileStream fileStream =newFileStream(fileName,FileMode.Open,FileAccess.Read)){byte[] buffer =newbyte[4096];int bytesRead =0;while((bytesRead = fileStream.Read(buffer,0, buffer.Length))!=0){                    rs.Write(buffer,0, bytesRead);}}byte[] trailer =System.Text.Encoding.ASCII.GetBytes("\r\n--"+ boundary +"--\r\n");            rs.Write(trailer,0, trailer.Length);}try{            using (HttpWebResponse response = request.GetResponse()asSystem.Net.HttpWebResponse){                responseStatusCode = response.StatusCode.ToString();Console.WriteLine("imported pbix ", responseStatusCode);}}catch(WebException wex){if(wex.Response!=null){                using (var errorResponse =(HttpWebResponse)wex.Response){                    using (var reader =newStreamReader(errorResponse.GetResponseStream())){string errorString = reader.ReadToEnd();dynamic respJson =JsonConvert.DeserializeObject<dynamic>(errorString);Console.WriteLine(respJson.ToString());}}}}return responseStatusCode;}}}

 


Viewing all articles
Browse latest Browse all 18050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>