C#: I've some errors about assembly while compiling the project on CFD

Discussion in 'Call Flow Designer' started by Bekir Ersin ÖZTEKİN, Dec 28, 2017.

Thread Status:
Not open for further replies.
  1. Bekir Ersin ÖZTEKİN

    Joined:
    Dec 19, 2017
    Messages:
    4
    Likes Received:
    0
    Hi everyone,

    I've problem while compiling the Call Flow Designer project. I've looked your article and your solved topics but I couldn't fix it.

    https://www.3cx.com/community/threads/c-assembly-and-namespace-errors.50028/
    https://www.3cx.com/docs/manual/cfd-troubleshooting/

    I've a cs library which has two methods. The first method only use System.IO and It works. It's okay.

    The second method uses System.IO too. But It doesn't work.

    This is my error list:

    Code:
    Creating source code output...
    Compiling the created source code to .NET library...
    Error compiling source code to .NET library: (23,46): error CS1061: 'MemoryStream' does not contain a definition for 'GetBuffer' and no extension method 'GetBuffer' accepting a first argument of type 'MemoryStream' could be found (are you missing a using directive or an assembly reference?)
    Error compiling source code to .NET library: (24,49): error CS1061: 'MemoryStream' does not contain a definition for 'GetBuffer' and no extension method 'GetBuffer' accepting a first argument of type 'MemoryStream' could be found (are you missing a using directive or an assembly reference?)
    Error compiling source code to .NET library: (27,51): error CS0103: The name 'HttpWebRequest' does not exist in the current context
    Error compiling source code to .NET library: (27,35): error CS0246: The type or namespace name 'HttpWebRequest' could not be found (are you missing a using directive or an assembly reference?)
    Error compiling source code to .NET library: (36,40): error CS0246: The type or namespace name 'HttpWebResponse' could not be found (are you missing a using directive or an assembly reference?)
    Error compiling source code to .NET library: (55,30): error CS1061: 'MemoryStream' does not contain a definition for 'Close' and no extension method 'Close' accepting a first argument of type 'MemoryStream' could be found (are you missing a using directive or an assembly reference?)
    Error compiling source code to .NET library: (56,28): error CS1061: 'FileStream' does not contain a definition for 'Close' and no extension method 'Close' accepting a first argument of type 'FileStream' could be found (are you missing a using directive or an assembly reference?)
    Error compiling source code to .NET library: (58,20): error CS0246: The type or namespace name 'WebException' could not be found (are you missing a using directive or an assembly reference?)
    I already have System.IO, System.Net DLLs in your "CompilerDepencies" folder.
    I never changed your default System.Net.dll and System.IO.dll files in "CompilerDepencies".

    Why It doesn't work? Do I need other sub DLLs? or It's a version problem?


    My namespace:

    Code:
    using System;
    using System.IO;
    using Newtonsoft.Json.Linq;
    using System.Net;
    using System.Text;
    
    My Code:

    Code:
           public bool CheckNotice(string appPath, string sourceFile) //This is OK.
            {
                return File.Exists(appPath + sourceFile);
            }
            
            public string FileToText(String filePath) //This is Not OK.
            {
                try
                {
                    FileStream fileStream = File.OpenRead(filePath);
                    MemoryStream memoryStream = new MemoryStream();
                    memoryStream.SetLength(fileStream.Length);
                    fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
                    byte[] audioFile = memoryStream.GetBuffer();
                    var base64Binary = Convert.ToBase64String(audioFile);
                    byte[] bytes = Encoding.UTF8.GetBytes("{\"config\": {\"encoding\": \"LINEAR16\", \"sampleRateHertz\": 8000, \"languageCode\": \"tr-TR\", \"enableWordTimeOffsets\": false},\"audio\": {\"content\": \"" + base64Binary + "\"}}");
                    var webRequest = (HttpWebRequest)(HttpWebRequest.Create("apiUrl"));
                    webRequest.Credentials = CredentialCache.DefaultCredentials;
                    webRequest.Method = "POST";
                    webRequest.ContentLength = bytes.Length;
                    using (Stream requestStream = webRequest.GetRequestStream())
                    {
                        requestStream.Write(bytes, 0, bytes.Length);
                    }
    
                    using (var response = (HttpWebResponse)(webRequest.GetResponse()))
                    {
                        using (var responseStream = response.GetResponseStream())
                        {
                            if (responseStream != null)
                            {
                                using (var responseReader = new StreamReader(responseStream))
                                {
                                    var responseText = responseReader.ReadToEnd();
                                    JObject root = JObject.Parse(responseText);
                                    var transcript = root["results"][0]["alternatives"][0]["transcript"].ToString();
                                    return transcript;
                                }
    
                            }
                        }
    
                    }
    
                    memoryStream.Close();
                    fileStream.Close();
                }
                catch (WebException ex)
                {
                    using (var stream = ex.Response)
                    {
                        if (stream != null)
                        {
                            using (var reader = new StreamReader(stream.GetResponseStream()))
                            {
                                //Console.WriteLine(reader.ReadToEnd());
                            }
    
                        }
                    }
    
                    //Console.WriteLine(ex.Message);
                }
                catch (Exception ex2)
                {
                    //Console.WriteLine(ex2.Message);
                }
                return null;
            }
    
     
  2. edossantos

    edossantos Support Team
    Staff Member 3CX Support

    Joined:
    Jun 27, 2007
    Messages:
    1,064
    Likes Received:
    75
    Hello @Bekir Ersin ÖZTEKİN,

    The problem is that the CFD needs .NET Core 1.0 C# code, which is a subset of the .NET Framework. For example, the MemoryStream object doesn't have the GetBuffer method, and you need to use the TryGetBuffer instead.

    The same happens with HttpWebRequest, which doesn't exist. You need to use the class HttpRequestMessage for that.

    Kind regards.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Bekir Ersin ÖZTEKİN

    Joined:
    Dec 19, 2017
    Messages:
    4
    Likes Received:
    0
    Thanks for your help @adossantos, I created a new .NET Core 1.0 project and I fixed problems on my codes so it works. I don't have any assembly problem and when I need a new dll I copy from my computer to "CompilerDepencies" folder. It's ok.

    But I have a new problem on Call Flow Designer:
    We have only "GetRequestStreamAsync" and "GetResponseAsync" methods on WebRequest class in .net Core 1.0 so I have to use an async method.

    When I try to write the result which return from my method, "ComponentName.ReturnValue" is returning "System.Threading.Tasks.Task`1[System.String]".

    In my project I can get the result with "MethodName().Result". How can I get it on Call Flow Designer Compenent?

    [​IMG]


    My codes:
    Code:
    public static async System.Threading.Tasks.Task<string> SpeechToTextAsync(string filePath)
            {
                var result = string.Empty;
                try
                {
                    FileStream fileStream = System.IO.File.OpenRead(filePath);
                    MemoryStream memoryStream = new MemoryStream();
                    memoryStream.SetLength(fileStream.Length);
                    var arraySegment = new ArraySegment<byte>();
                    var audioFile = memoryStream.TryGetBuffer(out arraySegment);
                    fileStream.Read(arraySegment.Array, 0, (int)fileStream.Length);
                    var base64Binary = Convert.ToBase64String(arraySegment.Array);
                    byte[] bytes = Encoding.UTF8.GetBytes("{\"config\": {\"encoding\": \"LINEAR16\", \"sampleRateHertz\": 8000, \"languageCode\": \"tr-TR\", \"enableWordTimeOffsets\": false},\"audio\": {\"content\": \"" + base64Binary + "\"}}");
    
                    var webRequest = WebRequest.Create("ApiUrl");
                    webRequest.Credentials = CredentialCache.DefaultCredentials;
                    webRequest.Method = "POST";
    
                    using (Stream requestStream = await webRequest.GetRequestStreamAsync())
                    {
                        requestStream.Write(bytes, 0, bytes.Length);
                    }
    
                    using (var response = (await webRequest.GetResponseAsync()))
                    {
                        using (var responseStream = response.GetResponseStream())
                        {
                            if (responseStream != null)
                            {
                                using (var responseReader = new StreamReader(responseStream))
                                {
                                    var responseText = responseReader.ReadToEnd();
                                    JObject root = JObject.Parse(responseText);
                                    result = root["results"][0]["alternatives"][0]["transcript"].ToString();
                                }
    
                            }
                        }
    
                    }
                }
                catch (Exception ex)
                {
    
                }
                
                return result;
            }
     
  4. Bekir Ersin ÖZTEKİN

    Joined:
    Dec 19, 2017
    Messages:
    4
    Likes Received:
    0
    I solved the problem that like:

    Code:
    public static string SpeechToText(string filePath)
            {
                var result = string.Empty;
    
                try
                {
                    var speechtoTextAsync = SpeechToTextAsync(filePath);
                    speechtoTextAsync.Wait(10000);
    
                    result = speechtoTextAsync.Result;
                }
                catch (Exception ex)
                {
                   
                }
    
                return result;
            }
    But I wonder how can I use the async methods on CFD's components
     
  5. edossantos

    edossantos Support Team
    Staff Member 3CX Support

    Joined:
    Jun 27, 2007
    Messages:
    1,064
    Likes Received:
    75
    The CFD has a synchronous nature, every component is executed after the previous one has ended. If you need to keep code running in the background while you do something else, then you can call an async method from a Launch External Script component which returns a Task, and then later you call the Task.Result from another Launch External Script component....
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. Bekir Ersin ÖZTEKİN

    Joined:
    Dec 19, 2017
    Messages:
    4
    Likes Received:
    0
Thread Status:
Not open for further replies.