简体   繁体   English

控制台应用程序 - 将 JSON 文件发送/写入 Azure Blob 存储

[英]Console Application - Send/Write JSON files to Azure Blob Storage

I have a Console Application created in C# that extracts data from Dynamics 365 Customer Voice.我在 C# 中创建了一个控制台应用程序,它从 Dynamics 365 Customer Voice 中提取数据。 The following code extracts data of Projects, Surveys, Questions and Question Responses respectivelly.以下代码分别提取项目、调查、问题和问题响应的数据。 The data extracted are written in JSON files that are saved on my HD.提取的数据写入保存在我的 HD 上的 JSON 文件中。

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UmbracoMVC.App_Code.Infrastructure.CRMIntegration.Business;
using System.IO;
using Microsoft.Crm.Sdk.Messages;
using System.Globalization;
using E2BWorkflow.Classes;
using System.Web.Management;
using System.ServiceModel;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;  

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            GetCustomerVoice();
        }

        public static void GetCustomerVoice()
        {
            IOrganizationService crmServiceTo;

            crmServiceTo = Connect.Crm("myemail@e-mail.com", "mypassword", "https://mydynamics.crm4.dynamics.com/XRMServices/2011/Organization.svc");

            // Get Projects
            QueryExpression qP = new QueryExpression("msfp_project");
            qP.ColumnSet = new ColumnSet(true);
            var projects = crmServiceTo.RetrieveMultiple(qP);
            foreach (var p in projects.Entities)
            {
                msfp_project project = p.ToEntity<msfp_project>();
            }
            var projectsList = projects.Entities.Select(
                s => new {
                    msfp_projectId = s.Attributes["msfp_projectid"],
                    msfp_name = s.Attributes["msfp_name"]
                }
            ).ToList();

            var jsonSerialiser = new JavaScriptSerializer();
            var json = jsonSerialiser.Serialize(projectsList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\projects.json", json);

            // Get Surveys
            QueryExpression qS = new QueryExpression("msfp_survey");
            qS.ColumnSet = new ColumnSet(true);
            var surveys = crmServiceTo.RetrieveMultiple(qS);
            foreach (var s in surveys.Entities)
            {
                msfp_survey survey = s.ToEntity<msfp_survey>();
            }
            var surveysList = surveys.Entities.Select(
                s => new {
                    msfp_surveyId = s.Attributes["msfp_surveyid"],
                    msfp_name = s.Attributes.Contains("msfp_name") ? s.GetAttributeValue<string>("msfp_questiontext") : "",
                    msfp_anonymousurl = s.Attributes.Contains("msfp_anonymousurl") ? s.GetAttributeValue<string>("msfp_anonymousurl") : "",
                    msfp_friendlyname = s.Attributes.Contains("msfp_friendlyname") ? s.GetAttributeValue<string>("msfp_friendlyname") : "",
                    msfp_surveyurl = s.Attributes.Contains("msfp_surveyurl") ? s.GetAttributeValue<string>("msfp_surveyurl") : "",
                    msfp_projectId = s.Attributes.Contains("msfp_project") && s.GetAttributeValue<EntityReference>("msfp_project").Id != null ? s.GetAttributeValue<EntityReference>("msfp_project").Id : Guid.Empty
                }
            ).ToList();

            jsonSerialiser = new JavaScriptSerializer();
            json = jsonSerialiser.Serialize(surveysList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\surveys.json", json);

            // Get Questions
            QueryExpression qQ = new QueryExpression("msfp_question");
            qQ.ColumnSet = new ColumnSet(true);
            var questions = crmServiceTo.RetrieveMultiple(qQ);
            foreach (var q in questions.Entities)
            {
                msfp_question question = q.ToEntity<msfp_question>();
            }
            var questionsList = questions.Entities.Select(
                s => new {
                    msfp_questionId = s.Attributes["msfp_questionid"],
                    msfp_questionText = s.Attributes.Contains("msfp_questiontext") ? s.GetAttributeValue<string>("msfp_questiontext") : "",
                    msfp_surveyId = s.Attributes.Contains("msfp_survey") && s.GetAttributeValue<EntityReference>("msfp_survey").Id != null ? s.GetAttributeValue<EntityReference>("msfp_survey").Id : Guid.Empty
                }
            ).ToList();

            jsonSerialiser = new JavaScriptSerializer();
            json = jsonSerialiser.Serialize(questionsList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\questions.json", json);

            //Get Question Responses
            QueryExpression qR = new QueryExpression("msfp_questionresponse");
            qR.ColumnSet = new ColumnSet(true);
            var responses = crmServiceTo.RetrieveMultiple(qR);
            foreach (var r in responses.Entities)
            {
                msfp_questionresponse response = r.ToEntity<msfp_questionresponse>();
            }

            var responsesList = responses.Entities.Select(
                s => new {
                    msfp_questionresponseId = s.Attributes["msfp_questionresponseid"],
                    msfp_questionresponse = s.Attributes["msfp_name"],
                    msfp_questionId = s.GetAttributeValue<EntityReference>("msfp_questionid").Id
                }
            ).ToList();

            jsonSerialiser = new JavaScriptSerializer();
            json = jsonSerialiser.Serialize(responsesList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\responses.json", json);

            Console.WriteLine("END");
            Console.ReadKey();
        }
    }
}

Now what I need is to have the JSONs be saved on a Azure Blob Storage account.现在我需要将 JSON 保存在 Azure Blob 存储帐户上。 I searched many solutions on the internet, but not sure what should I use for Console Application.我在互联网上搜索了许多解决方案,但不确定我应该为控制台应用程序使用什么。

I have a Console Application created in C# that extracts data from Dynamics 365 Customer Voice.我在 C# 中创建了一个控制台应用程序,它从 Dynamics 365 Customer Voice 中提取数据。 The following code extracts data of Projects, Surveys, Questions and Question Responses respectivelly.以下代码分别提取项目、调查、问题和问题响应的数据。 The data extracted are written in JSON files that are saved on my HD.提取的数据写入保存在我的 HD 上的 JSON 文件中。

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UmbracoMVC.App_Code.Infrastructure.CRMIntegration.Business;
using System.IO;
using Microsoft.Crm.Sdk.Messages;
using System.Globalization;
using E2BWorkflow.Classes;
using System.Web.Management;
using System.ServiceModel;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;  

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            GetCustomerVoice();
        }

        public static void GetCustomerVoice()
        {
            IOrganizationService crmServiceTo;

            crmServiceTo = Connect.Crm("myemail@e-mail.com", "mypassword", "https://mydynamics.crm4.dynamics.com/XRMServices/2011/Organization.svc");

            // Get Projects
            QueryExpression qP = new QueryExpression("msfp_project");
            qP.ColumnSet = new ColumnSet(true);
            var projects = crmServiceTo.RetrieveMultiple(qP);
            foreach (var p in projects.Entities)
            {
                msfp_project project = p.ToEntity<msfp_project>();
            }
            var projectsList = projects.Entities.Select(
                s => new {
                    msfp_projectId = s.Attributes["msfp_projectid"],
                    msfp_name = s.Attributes["msfp_name"]
                }
            ).ToList();

            var jsonSerialiser = new JavaScriptSerializer();
            var json = jsonSerialiser.Serialize(projectsList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\projects.json", json);

            // Get Surveys
            QueryExpression qS = new QueryExpression("msfp_survey");
            qS.ColumnSet = new ColumnSet(true);
            var surveys = crmServiceTo.RetrieveMultiple(qS);
            foreach (var s in surveys.Entities)
            {
                msfp_survey survey = s.ToEntity<msfp_survey>();
            }
            var surveysList = surveys.Entities.Select(
                s => new {
                    msfp_surveyId = s.Attributes["msfp_surveyid"],
                    msfp_name = s.Attributes.Contains("msfp_name") ? s.GetAttributeValue<string>("msfp_questiontext") : "",
                    msfp_anonymousurl = s.Attributes.Contains("msfp_anonymousurl") ? s.GetAttributeValue<string>("msfp_anonymousurl") : "",
                    msfp_friendlyname = s.Attributes.Contains("msfp_friendlyname") ? s.GetAttributeValue<string>("msfp_friendlyname") : "",
                    msfp_surveyurl = s.Attributes.Contains("msfp_surveyurl") ? s.GetAttributeValue<string>("msfp_surveyurl") : "",
                    msfp_projectId = s.Attributes.Contains("msfp_project") && s.GetAttributeValue<EntityReference>("msfp_project").Id != null ? s.GetAttributeValue<EntityReference>("msfp_project").Id : Guid.Empty
                }
            ).ToList();

            jsonSerialiser = new JavaScriptSerializer();
            json = jsonSerialiser.Serialize(surveysList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\surveys.json", json);

            // Get Questions
            QueryExpression qQ = new QueryExpression("msfp_question");
            qQ.ColumnSet = new ColumnSet(true);
            var questions = crmServiceTo.RetrieveMultiple(qQ);
            foreach (var q in questions.Entities)
            {
                msfp_question question = q.ToEntity<msfp_question>();
            }
            var questionsList = questions.Entities.Select(
                s => new {
                    msfp_questionId = s.Attributes["msfp_questionid"],
                    msfp_questionText = s.Attributes.Contains("msfp_questiontext") ? s.GetAttributeValue<string>("msfp_questiontext") : "",
                    msfp_surveyId = s.Attributes.Contains("msfp_survey") && s.GetAttributeValue<EntityReference>("msfp_survey").Id != null ? s.GetAttributeValue<EntityReference>("msfp_survey").Id : Guid.Empty
                }
            ).ToList();

            jsonSerialiser = new JavaScriptSerializer();
            json = jsonSerialiser.Serialize(questionsList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\questions.json", json);

            //Get Question Responses
            QueryExpression qR = new QueryExpression("msfp_questionresponse");
            qR.ColumnSet = new ColumnSet(true);
            var responses = crmServiceTo.RetrieveMultiple(qR);
            foreach (var r in responses.Entities)
            {
                msfp_questionresponse response = r.ToEntity<msfp_questionresponse>();
            }

            var responsesList = responses.Entities.Select(
                s => new {
                    msfp_questionresponseId = s.Attributes["msfp_questionresponseid"],
                    msfp_questionresponse = s.Attributes["msfp_name"],
                    msfp_questionId = s.GetAttributeValue<EntityReference>("msfp_questionid").Id
                }
            ).ToList();

            jsonSerialiser = new JavaScriptSerializer();
            json = jsonSerialiser.Serialize(responsesList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\responses.json", json);

            Console.WriteLine("END");
            Console.ReadKey();
        }
    }
}

Now what I need is to have the JSONs be saved on a Azure Blob Storage account.现在我需要将 JSON 保存在 Azure Blob 存储帐户上。 I searched many solutions on the internet, but not sure what should I use for Console Application.我在互联网上搜索了许多解决方案,但不确定我应该为控制台应用程序使用什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM