I cannot access ApplicationData.Current from Win32 unpackaged app? - windows

My code:
using System;
using Windows;
namespace SampleSynthesis {
class Program {
static async System.Threading.Tasks.Task Main(string[] args) {
var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
Windows.Media.SpeechSynthesis.SpeechSynthesisStream stream =
await synth.SynthesizeTextToStreamAsync("I want to listen to this text");
using (var reader = new Windows.Storage.Streams.DataReader(stream)) {
await reader.LoadAsync((uint)stream.Size);
Windows.Storage.Streams.IBuffer buffer = reader.ReadBuffer((uint)stream.Size);
Windows.Storage.StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("audio.wav");
await Windows.Storage.FileIO.WriteBufferAsync(file, buffer);
}
}
}
Compiled with this command:
"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\Roslyn\csc.exe" /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Speech.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll","C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.runtime.dll","C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.17763.0\Windows.winmd","C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Threading.Tasks.dll" tts.cs
I am getting:
Unhandled Exception: System.InvalidOperationException: The process has no package identity. (Exception from HRESULT: 0x80073D54)
at Windows.Storage.ApplicationData.get_Current()
at SampleSynthesis.Program.<Main>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at SampleSynthesis.Program.<Main>(String[] args)
Maybe exists another way to save wav? I know that I can use simpler SetOutputToWaveFile() but because of cracks in the synthesized audio I want to try some another method. I need to use Desktop Bridge? Instead of Windows.Storage.ApplicationData.Current.LocalFolder I tried Windows.Storage.ApplicationData.Current.TemporaryFolder and got the same exception.

Windows.Storage.ApplicationData.Current only applies to packaged apps that have an identity (UWP apps and Desktop Bridge apps). Unpackaged Win32 EXE don't have the concept of their own isolated app data.
It's not clear why you need to this here, or what you are trying to accomplish. This shouldn't have anything to do with cracks in the audio playback.

Related

File.Delete() throw "owner died" in Xamarin

Generally, file deletion can be performed correctly on most Android phones
However, when I run this line of code on "Huawei art-al00x (Android 10.0 - API 29)", the operation throws an exception "owner died"
I can create files and write data, but I can't delete files
I've never been in this situation before
After Google searched, there is still no result
After the second test, I found that if I checked the file in the file manager, I could delete it smoothly, while if I didn't check the file, it would throw an exception
The Code:
string path = "Want Save Path";
byte[] buffer = new uint8_t[73790];
using (BinaryWriter BW = new BinaryWriter(new FileStream(path, FileMode.Create)))
{
BW.Write(buffer, 0, (int)exer.Size);
BW.Flush();
BW.Close();
Console.WriteLine("USM:Save Image Data Success");
}
StackTrace:
at System.IO.FileSystem.DeleteFile (System.String fullPath) [0x00065]
in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.IO.FileSystem/src/System/IO/FileSystem.Unix.cs:215
at System.IO.File.Delete (System.String path) [0x0000e]
in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.IO.FileSystem/src/System/IO/File.cs:107
at DebugTest.MyView.MainPage_OperateView.TTTFrame_Click () [0x00014]
in G:\VS2019 Projects\DebugTest\DebugTest\DebugTest\MyView\MainPage_OperateView.xaml.cs:263

Catastrophic failure(Exception HRESULT: 0x8000FFFF (E_UNEXPECTED)) while trying to use MS WebView2 in ASP,NET Web API in production environment

I'm getting 'Catastrophic failure(Exception HRESULT: 0x8000FFFF (E_UNEXPECTED))' run-time error while trying to use MS WebView2 in ASP.NET Web API in production environment (Win10, IIS v.20H2 (OS Build 19042, 1586)) - see details in the code inline.
When the same code is used in a .NET Framework console app or under VS2019/Kestrel(?) (Ctrl+F5 - Start without debugging) everything works well.
private ManualResetEvent _event;
public void StartBrowserThread()
{
var th = new Thread(async () =>
{
BrowserFormsLogger.Log($"th.Context = {contextDescription}");
var webView = new Microsoft.Web.WebView2.WinForms.WebView2();
...
udf = ...
var envTask = await Microsoft.Web.WebView2.Core.CoreWebView2Environment
.CreateAsync(userDataFolder: udf);
var ecwTask = webView.EnsureCoreWebView2Async(envTask);
while (ecwTask.IsCompleted == false)
{
Application.DoEvents();
//
// Run-time error happens here in ASP.NET environment after .DoEvents called several thousand times.
// In a .NET Framework Console App everything works well.
//
// WebView2 creation failed with exception = System.Runtime.InteropServices.COMException (0x8000FFFF):
// Catastrophic failure(Exception HRESULT: 0x8000FFFF (E_UNEXPECTED))
// in System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
// in Microsoft.Web.WebView2.Core.CoreWebView2Environment.<CreateCoreWebView2ControllerAsync>d__58.MoveNext()
// --- End of stack trace from previous location where exception occurred ---
// in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
// in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
// in System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
// in Microsoft.Web.WebView2.WinForms.WebView2.<InitCoreWebView2Async>d__18.MoveNext()
};
...
_event.Set();
System.Windows.Forms.Application.Run();
});
th.SetApartmentState(ApartmentState.STA);
th.IsBackground = true;
_event = new ManualResetEvent(false);
th.Start();
...
Any clues how this issue can be solved?

Exception -"Cannot send a content-body with this verb-type" using httpclient.sendasync

Already tried searching SO for similar issues but those didn't help to resolve.
Scenario:
Have a Web Client app which has a Web API app as a backend. There is no issue when calling the same URI if it's from this Client - Web API but the exception happens when I'm trying to trigger the API request from another WebAPI Services application ( say its a separate project for accessing reports which run as a service using Telerik Reporting).
Code:
private async Task<HttpStatusCode> AccessService()
{
HttpResponseMessage response = null;
try
{
using (HttpRequestMessage request = new HttpRequestMessage()
{
Content = new System.Net.Http.StringContent(JSONValues, System.Text.Encoding.UTF8, "application/json"),
RequestUri = new Uri(UriString),
Method = CallMethod
})
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Utils.Token) ;
response = await httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
string httpResponse = await response.Content.ReadAsStringAsync();
httpString = JsonConvert.DeserializeObject<string[]>(httpResponse);
}
ErrorCode = response.StatusCode;
response.Dispose();
}
}
catch (Exception ex)
{
Misc.Error= ex.Message;
}
finally
{
response.Dispose();
}
return ErrorCode;
}
Exception:
Cannot send a content-body with this verb-type.
at System.Net.HttpWebRequest.CheckProtocol(Boolean onRequestStream)
at System.Net.HttpWebRequest.BeginGetRequestStream(AsyncCallback callback, Object state)
at System.Net.Http.HttpClientHandler.StartGettingRequestStream(RequestState state)
at System.Net.Http.HttpClientHandler.PrepareAndStartContentUpload(RequestState state)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
This works fine when I access the same from the Client app ( asp.net mvc ) and issue happens when I call this from the Report Services API app. Is this has something to with it.
Edit:
The Web client Project is asp.net mvc core 2.1, so I think it works while using content in GET method of HttpClient.SendAsync but the Reports Services App where this issue happens are targets .net 461 .
I strongly doubt this might be the cause since .net framework doesn't allow content for GET operations? Any work arounds?
Link
TIA
Based on the description provided, if you use get method you usually don't have a body in the get request. For more details, please refer to the solutions below:
http://stackoverflow.com/questions/3981564/cannot-send-a-content-body-with-this-verb-type

Upload a text file as attachment using Bot framework not working in Skype channel

I am unable to upload a text file as an attachment using the Bot framework (Bot.Builder v3.11.0), when trying in the Skype channel. It worked in the Bot framework emulator though. Following is the code which uploads a file and to returns the activity with the uploaded file URL in the attachment. It throws an exception when using the Skype channel. Alternatively is there any other way to achieve uploading/attaching a text file in the Skype channel which the user can then download from within the client?
public static async Task<Activity> GetTextAttachmentAsync(Activity message)
{
var reply = message.CreateReply("Here is a text attachment");
var serviceUrl = reply.ServiceUrl;
var conversationId = reply.Conversation.Id;
byte[] fileData = null;
using (var wc = new System.Net.WebClient())
fileData = wc.DownloadData("https://textfiles.com/100/adventur.txt");
using (var connector = new ConnectorClient(new Uri(serviceUrl)))
{
var attachments = new Attachments(connector);
var token = await (connector.Credentials as MicrosoftAppCredentials).GetTokenAsync();
connector.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await attachments.Client.Conversations.UploadAttachmentAsync(
conversationId,
new AttachmentData
{
Name = "transcript.html",
OriginalBase64 = fileData,
Type = "text/html"
});
reply.Attachments = new List<Attachment>
{
new Attachment
{
Name = "transcript.html",
ContentType = "text/html",
ContentUrl = attachments.GetAttachmentUri(response.Id)
}
};
return reply;
}
}
Exception thrown from the UploadAttachmentAsync() function above:
Microsoft.Rest.HttpOperationException: Not Found
at Microsoft.Bot.Connector.ErrorHandling.<HandleErrorAsync>d__2`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Bot.Connector.ConversationsExtensions.<UploadAttachmentAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Support.Services.Bot.Core.Utilities.AdaptiveCardsHelper.<GetTextAttachmentAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Support.Services.Bot.Core.Dialogs.BotDialog.<HandleMessageAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Support.Services.Bot.Core.Dialogs.DialogBase`1.<MessageReceivedAsync>d__8.MoveNext()
When running your code with Common Language Runtime Errors enabled in Visual Studio. I got the following error:
It seems that the site address https://textfiles.com/100/adventur.txt does not have a trusted certificate and that makes .NET unhappy. When I went to investigate, I found this Stack Overflow answer that suggested using this code below to overcome this but is strongly recommended not to use this in production.
ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;
This was going to be my original suggestion before I ran your code: many channels (Skype included), limit the types of files you are able to send from a bot when using base64 and/or local files. For example, I know that you cannot send PDF files as Base64 in Skype. If memory serves correctly, you can only send image and video files (maybe audio too) using the base64 method in Skype. So even if you resolve this error you may run into this afterward. The workaround for this is using hosted files. I'm not exactly sure what you are trying to do with your bot, so I'm not sure if this is an option for you specifically, but it is an option.
So if you find this code not working after resolving the certificate issue, try sending an image file and see if that works, if it does but your HTML file still fails, you will know this is why.

"GattDeviceServicesResult" can not be found

I am trying to create client side app using C# for BluetoothLE in VisualStudio 2015 on Windows-10 laptop.
I have problem using Windows.Devices.Bluetooth.GenericAttributeProfile, the issue is my code has compile error saying GattDeviceServicesResult can not be found.
-> I have added package UwpDesktop 10.0.14393.3 by Valdimir Postel... (before installing this even "using Wndows.Devices.Bluetooth" was not working)
-> Then I added SDK, windows Kit that was recommended by VisualStudio when I tried to open one of the example (So I accept the recommendation to build that project and VS installed packages of around 9GB)
-> now I can use some of the Bluetooth api's I can scan and connect to a BLE device, but I can not use classes to deal with services and characteristics because GattDeviceServicesResult and GattCharacteristicsResult types are not found. Although these are mentioned on MSDN website
-> searching in forums I came to know I need to add one more reference System.Runtime.WindowsRuntime.dll, I browsed to proper folder through add reference utility of VS, I am trying to add this and it does nothing, after I select the dll and click 'Add' just nothing happens. (Add reference is not adding this dll).
Just for example if I select some other dll and try to add, that works fine!
Could somebody please help me with this,
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Bluetooth.Advertisement;
Int16 uuid_count = 0;
BluetoothLEAdvertisement[] ble_adv = new BluetoothLEAdvertisement[5];
BluetoothLEAdvertisementReceivedEventArgs[] ble_received_adv = new BluetoothLEAdvertisementReceivedEventArgs[5];
BluetoothLEDevice bluetooth_LE_Device;
GattDeviceServicesResult result_service;// This line does not compile
//Error: CS0246 the type name 'GattDeviceServicesResult ' could not be found
// I am adding reference to "System.Runtime.WindowsRuntime" as mentioned in some solutions
// the reference does not seems to be added at first when I click add button, but I can see the reference dll being mentioned in solution explorer (assuming it's been added)
// using this to scan available devices
private void scann_ble()
{
var watcher = new BluetoothLEAdvertisementWatcher();
watcher.Received += Watcher_Received;
watcher.AdvertisementFilter.Advertisement.ServiceUuids.Clear();
watcher.Start();
while (true)
{
Thread.Sleep(10000);
break;
}
watcher.Stop();
}
// receiver event to collect addresses of available devices
private void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
bool update_adv = true;
Int16 i = 0;
if(uuid_count < 5)
{
if (uuid_count > 0)
{
while (i < uuid_count)
{
if (ble_received_adv[i].BluetoothAddress == args.BluetoothAddress)
update_adv = false;
i++;
}
}
if(update_adv != false)
ble_received_adv[uuid_count++] = args;
}
}
// now connecting and checking available services
// as per "https://learn.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-client"
private async void BLE_connect_button_Click(object sender, EventArgs e)
{
int i = 0;
i = BLE_device_grid_view.CurrentCell.RowIndex; // getting index from item selected in gridView
bluetooth_LE_Device = await BluetoothLEDevice.FromBluetoothAddressAsync(ble_received_adv[i].BluetoothAddress);
// Connection works fine, I can see it on my peripheral device
//get services - This is not working
result_service = bluetooth_LE_Device.GetGattServicesAsync();
if (result_service.Status == await GattCommunicationStatus.Success)
{
var services = result_service.Services;
// ...
}
}
I am using UwpDesktop package.
I had the same problem. My problem was solved after I changed the reference to windows.winmd from C:\Program Files (x86)\Windows Kits\10\UnionMetadata\windows.winmd to C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.17763.0\windows.winmd.
I'm using BLE 4.0 in a WinForm application and I'm working with Visual Studio 2017.
It is very likely that the directory 10.0.17763.0 doesn't exist on every computer, but you may look what versions of windows.winmd exists on your computer.

Resources