I have a simple OWIN self-hosted application with a controller extending ApiController. The problem is, when I send a POST request with no data to the controller (the method doesn't have parameters), it returns:
Length Required
And then the application crashes with an unhandled ObjectDisposedException (full stack trace below). If the POST request contains data then all is well.
I removed all controller logic from the equation by replicating with this simple method:
[HttpPost]
public void MyMethod()
{
return;
}
Therefore I'm guessing it's something to do with routing? But my routes are only set up as the default:
config.Routes.MapHttpRoute("My Service", "{controller}/{action}/{id}", new { id = RouteParameter.Optional });
Why would my application be behaving in this manner?
To send the requests I use curl:
curl --request POST http://localhost/mycontroller/mymethod
Stack trace:
Unhandled Exception: System.ObjectDisposedException: Cannot access a
disposed object. Object name: 'System.Net.HttpListenerResponse'. at
System.Net.HttpListenerResponse.set_StatusCode (System.Int32 value)
[0x00016] in <59be416de143456b88b9988284f43350>:0 at
Microsoft.Owin.Host.HttpListener.RequestProcessing.OwinHttpListenerResponse.End
() [0x0001c] in <5086711574984403b242554b11c41440>:0 at
Microsoft.Owin.Host.HttpListener.RequestProcessing.OwinHttpListenerContext.End
() [0x00010] in <5086711574984403b242554b11c41440>:0 at
Microsoft.Owin.Host.HttpListener.RequestProcessing.OwinHttpListenerContext.End
(System.Exception ex) [0x0001e] in
<5086711574984403b242554b11c41440>:0 at
Microsoft.Owin.Host.HttpListener.OwinHttpListener+d__5.MoveNext
() [0x001f9] in <5086711574984403b242554b11c41440>:0
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
[0x0000c] in :0 at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
(System.Threading.Tasks.Task task) [0x0004e] in
:0 at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification
(System.Threading.Tasks.Task task) [0x0002e] in
:0 at
System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd
(System.Threading.Tasks.Task task) [0x0000b] in
:0 at
System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in
:0 at
Microsoft.Owin.Host.HttpListener.OwinHttpListener+d__0.MoveNext
() [0x00202] in <5086711574984403b242554b11c41440>:0
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
[0x0000c] in :0 at
System.Runtime.CompilerServices.AsyncMethodBuilderCore.m__1
(System.Object state) [0x00000] in
:0 at
System.Threading.QueueUserWorkItemCallback.WaitCallback_Context
(System.Object state) [0x0000e] in
:0 at
System.Threading.ExecutionContext.RunInternal
(System.Threading.ExecutionContext executionContext,
System.Threading.ContextCallback callback, System.Object state,
System.Boolean preserveSyncCtx) [0x0008d] in
:0 at
System.Threading.ExecutionContext.Run
(System.Threading.ExecutionContext executionContext,
System.Threading.ContextCallback callback, System.Object state,
System.Boolean preserveSyncCtx) [0x00000] in
:0 at
System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem
() [0x0002a] in :0 at
System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00096] in
:0 at
System.Threading._ThreadPoolWaitCallback.PerformWaitCallback ()
[0x00000] in :0
Maybe if you show all your controller class I can help you a little more.
Try to make your post like this.
Make one class every get/post
for example in folder Controllers/Api/MyFunction.cs
in WebApiConfig.cs
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "API/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
in your "MyFunction.cs"
public class MyFunction : ApiController
{
public string Get()
{
return "hello";
}
public bool Post(string param1)
{
return 1;
}
}
Related
I am building an app in Unity which uses Bluetooth to read from various sensors e.g. heart rate. I am trying to communicate with different Bluetooth devices at once. When I try to subscribe to more than 1 (sometimes 2 work) notification characteristics I get a "ObjectDisposedException: Cannot access a disposed object." exception at line "var accessStatus = await service.RequestAccessAsync();". Is there a way to prevent that asynchronous task from being disposed since I believe that this is the problem, or is there any other issue?
private async Task CommunicateWithDevice(CustomBluetoothDevice device) {
#if WINDOWS_UWP
if (device.bluetoothServices.Count == 0) {
Debug.LogError("No services available. Did you forget to call GetDeviceServices?");
return;
}
foreach(var bluetoothService in device.bluetoothServices) {
var service = bluetoothService.Value.gattService;
if ((bluetoothService.Key.Equals(GattGuids.POWER_SERVICE) || bluetoothService.Key.Equals(GattGuids.CSC_SERVICE)) && !userSelectedDeviceCategories.Contains(bluetoothService.Key)) //XX.Contains !bluetoothService.Key.Equals(userSelectedDeviceCategory
{
continue;
}
var accessStatus = await service.RequestAccessAsync();
Debug.Log("service: " + service.Uuid + " | accessStatus: " + accessStatus);
if (accessStatus == DeviceAccessStatus.Allowed) {
try {
var characteristicsResult = await service.GetCharacteristicsAsync(BluetoothCacheMode.Uncached);
if (characteristicsResult.Status == GattCommunicationStatus.Success) {
foreach(var characteristic in characteristicsResult.Characteristics) {
GattCharacteristicProperties properties = characteristic.CharacteristicProperties;
if (properties.HasFlag(GattCharacteristicProperties.Read)) {
SaveReadCharacteristic(device, characteristic);
}
if (properties.HasFlag(GattCharacteristicProperties.Write)) {
await WriteToCharacteristic(device, characteristic, true);
}
if (properties.HasFlag(GattCharacteristicProperties.Notify)) {
await NotifyOnCharacteristic(device, characteristic);
}
}
}
}
catch(Exception e) {
Debug.Log("CommunicateWithDevice catch: " + e);
await CommunicateWithDevice(device);
}
}
}
#endif
}
ObjectDisposedException: Cannot access a disposed object. at
BluetoothController.CommunicateWithDevice (CustomBluetoothDevice
device) [0x00165] in
D:\GitHub\BleApp\Assets\Scripts\Bluetooth\BluetoothController.cs:413
at
System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine]
(TStateMachine& stateMachine) [0x00000] in
<00000000000000000000000000000000>:0 at
BluetoothController.CommunicateWithDevice (CustomBluetoothDevice
device) [0x00000] in <00000000000000000000000000000000>:0 at
BluetoothController.CommunicateWithDevice (CustomBluetoothDevice
device) [0x004e3] in
D:\GitHub\BleApp\Assets\Scripts\Bluetooth\BluetoothController.cs:444
at
System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext
(System.Object stateMachine) [0x00000] in
<00000000000000000000000000000000>:0 at
System.Threading.ExecutionContext.RunInternal
(System.Threading.ExecutionContext executionContext,
System.Threading.ContextCallback callback, System.Object state,
System.Boolean preserveSyncCtx) [0x00000] in
<00000000000000000000000000000000>:0 at
System.Threading.ExecutionContext.Run
(System.Threading.ExecutionContext executionContext,
System.Threading.ContextCallback callback, System.Object state,
System.Boolean preserveSyncCtx) [0x00000] in
<00000000000000000000000000000000>:0 at
System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run
() [0x00000] in <00000000000000000000000000000000>:0 at
System.Threading.Tasks.AwaitTaskContinuation.InvokeAction
(System.Object state) [0x00000] in
<00000000000000000000000000000000>:0 at
System.Threading.Tasks.AwaitTaskContinuation.RunCallback
(System.Threading.ContextCallback callback, System.Object state,
System.Threading.Tasks.Task& currentTask) [0x00000] in
<00000000000000000000000000000000>:0 at
System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.Run
(System.Threading.Tasks.Task ignored, System.Boolean
canInlineContinuationTask) [0x00000] in
<00000000000000000000000000000000>:0 at
System.Threading.Tasks.Task.FinishContinuations () [0x00000] in
<00000000000000000000000000000000>:0 at
System.Threading.Tasks.Task.FinishStageThree () [0x00000] in
<00000000000000000000000000000000>:0 at
System.Threading.Tasks.Task.FinishStageTwo () [0x00000] in
<00000000000000000000000000000000>:0 at
System.Threading.Tasks.Task.Finish (System.Boolean
bUserDelegateExecuted) [0x00000] in
<00000000000000000000000000000000>:0 at
System.Threading.Tasks.Task.TrySetException (System.Object
exceptionObject) [0x00000] in <00000000000000000000000000000000>:0
at
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult].SetException
(System.Exception exception) [0x00000] in
<00000000000000000000000000000000>:0 at
System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetException
(System.Exception exception) [0x00000] in
<00000000000000000000000000000000>:0 at
BluetoothController.NotifyOnCharacteristic (CustomBluetoothDevice
device,
Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic
characteristic) [0x0013c] in
D:\GitHub\BleApp\Assets\Scripts\Bluetooth\BluetoothController.cs:490
at
System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext
(System.Object stateMachine) [0x00000] in
<00000000000000000000000000000000>:0 at
System.Threading.ExecutionContext.RunInternal
(System.Threading.ExecutionContext executionContext,
System.Threading.ContextCallback callback, System.Object state,
System.Boolean preserveSyncCtx) [0x00000] in
<00000000000000000000000000000000>:0 at
System.Threading.ExecutionContext.Run
(System.Threading.ExecutionContext executionContext,
System.Threading.ContextCallback callback, System.Object state,
System.Boolean preserveSyncCtx) [0x00000] in
<00000000000000000000000000000000>:0 at
System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run
() [0x00000] in <00000000000000000000000000000000>:0 at
System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation+<>c.<.cctor>b__7_0
(System.Object state) [0x00000] in
<00000000000000000000000000000000>:0 at
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke ()
[0x0000e] in
C:\buildslave\unity\build\Runtime\Export\Scripting\UnitySynchronizationContext.cs:153
at UnityEngine.UnitySynchronizationContext.Exec () [0x00058] in
C:\buildslave\unity\build\Runtime\Export\Scripting\UnitySynchronizationContext.cs:83
at UnityEngine.UnitySynchronizationContext.ExecuteTasks () [0x00015]
in
C:\buildslave\unity\build\Runtime\Export\Scripting\UnitySynchronizationContext.cs:107
--- End of stack trace from previous location where exception was thrown ---
I am trying to create a mobile application using MvvmCross v.4.4.0.
Application works correct. But if collapse application and lock the phone, and then unlock and press "Overview" android button application crash (only on the Xiaomi phone).
I think android kills activity and then unsuccessfully trying to recreate it.
Could you, please explain me, what I do wrong? How to fix it?
Stacktrace:
Android.Runtime.RaiseThrowableEventArgs MvvmCross.Platform.Exceptions.MvxException: Failed to construct and initialize ViewModel for type Test.Core.ViewModels.MainMenuViewModel from locator MvxDefaultViewModelLocator - check InnerException for more information ---> MvvmCross.Platform.Exceptions.MvxException: Problem creating viewModel of type MainMenuViewModel ---> MvvmCross.Platform.Exceptions.MvxIoCResolveException: Failed to construct MainMenuViewModel ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Java.Lang.LinkageError: no static method "Landroid/text/Html;.fromHtml(Ljava/lang/String;I)Landroid/text/Spanned;" at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
[0x0000c] in <89dd20b27a0b473a848558c84f1f086a>:0 at Java.Interop.JniEnvironment+StaticMethods.GetStaticMethodID (Java.Interop.JniObjectReference type, System.String name, System.String signature)
[0x00068] in :0 at Java.Interop.JniType.GetStaticMethod (System.String name, System.String signature)
[0x0000d] in :0 at Java.Interop.JniPeerMembers+JniStaticMethods.GetMethodInfo (System.String encodedMember)
[0x0003c] in :0 at Java.Interop.JniPeerMembers+JniStaticMethods.InvokeObjectMethod (System.String encodedMember, Java.Interop.JniArgumentValue* parameters)
[0x00001] in :0 at Android.Text.Html.FromHtml (System.String source, Android.Text.FromHtmlOptions flags)
[0x0003b] in :0 at Test.Droid.Providers.DialogProvider.ShowSnackbar () [0x00017] in <0fa1f44ee2f748fe816e790571c2153b>:0 at Test.Core.ViewModels.BaseViewModel..ctor ()
[0x0006d] in :0 at Test.Core.ViewModels.MainMenuViewModel..ctor (Test.Core.Managers.IExceptionManager exceptionManager)
[0x0000b] in :0 at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&) at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters)
[0x00002] in <89dd20b27a0b473a848558c84f1f086a>:0 --- End of inner exception stack trace --- at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters)
[0x0001c] in <89dd20b27a0b473a848558c84f1f086a>:0 at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
[0x00089] in <89dd20b27a0b473a848558c84f1f086a>:0 at System.Reflection.MonoCMethod.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
[0x00000] in <89dd20b27a0b473a848558c84f1f086a>:0 at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters)
[0x00000] in <89dd20b27a0b473a848558c84f1f086a>:0 at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type)
[0x0003a] in <4ddde23419c5494288c799fcdbb0f189>:0 --- End of inner exception stack trace --- at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type)
[0x0005b] in <4ddde23419c5494288c799fcdbb0f189>:0 at MvvmCross.Platform.Mvx.IocConstruct (System.Type t)
[0x00005] in <4ddde23419c5494288c799fcdbb0f189>:0 at MvvmCross.Core.ViewModels.MvxDefaultViewModelLocator.Load (System.Type viewModelType, MvvmCross.Core.ViewModels.IMvxBundle parameterValues, MvvmCross.Core.ViewModels.IMvxBundle savedState)
[0x00000] in <69bce0378e8e413982d3b552d7e387a8>:0 --- End of inner exception stack trace --- at MvvmCross.Core.ViewModels.MvxDefaultViewModelLocator.Load (System.Type viewModelType, MvvmCross.Core.ViewModels.IMvxBundle parameterValues, MvvmCross.Core.ViewModels.IMvxBundle savedState)
[0x00027] in <69bce0378e8e413982d3b552d7e387a8>:0 at MvvmCross.Core.ViewModels.MvxViewModelLoader.LoadViewModel (MvvmCross.Core.ViewModels.MvxViewModelRequest request, MvvmCross.Core.ViewModels.IMvxBundle savedState, MvvmCross.Core.ViewModels.IMvxViewModelLocator viewModelLocator)
[0x00015] in <69bce0378e8e413982d3b552d7e387a8>:0 --- End of inner exception stack trace --- at MvvmCross.Core.ViewModels.MvxViewModelLoader.LoadViewModel (MvvmCross.Core.ViewModels.MvxViewModelRequest request, MvvmCross.Core.ViewModels.IMvxBundle savedState, MvvmCross.Core.ViewModels.IMvxViewModelLocator viewModelLocator)
[0x00046] in <69bce0378e8e413982d3b552d7e387a8>:0 at MvvmCross.Core.ViewModels.MvxViewModelLoader.LoadViewModel (MvvmCross.Core.ViewModels.MvxViewModelRequest request, MvvmCross.Core.ViewModels.IMvxBundle savedState)
[0x00020] in <69bce0378e8e413982d3b552d7e387a8>:0 at MvvmCross.Droid.Views.MvxAndroidViewsContainer.ViewModelFromRequest (MvvmCross.Core.ViewModels.MvxViewModelRequest viewModelRequest, MvvmCross.Core.ViewModels.IMvxBundle savedState)
[0x00005] in :0 at MvvmCross.Droid.Views.MvxAndroidViewsContainer.CreateViewModelFromIntent (Android.Content.Intent intent, MvvmCross.Core.ViewModels.IMvxBundle savedState)
[0x00027] in :0 at MvvmCross.Droid.Views.MvxAndroidViewsContainer.Load (Android.Content.Intent intent, MvvmCross.Core.ViewModels.IMvxBundle savedState, System.Type viewModelTypeHint)
[0x00089] in :0 at MvvmCross.Droid.Views.MvxActivityViewExtensions.LoadViewModel (MvvmCross.Droid.Views.IMvxAndroidView androidView, MvvmCross.Core.ViewModels.IMvxBundle savedState)
[0x0006a] in :0 at MvvmCross.Droid.Views.MvxActivityViewExtensions+<>c__DisplayClass1_0.b__1 ()
[0x0000a] in :0 at MvvmCross.Core.Views.MvxViewExtensionMethods.OnViewCreate (MvvmCross.Core.Views.IMvxView view, System.Func`1[TResult] viewModelLoader)
[0x00012] in <69bce0378e8e413982d3b552d7e387a8>:0 at MvvmCross.Droid.Views.MvxActivityViewExtensions.OnViewCreate (MvvmCross.Droid.Views.IMvxAndroidView androidView, Android.OS.Bundle bundle)
[0x00062] in :0 at MvvmCross.Droid.Views.MvxActivityAdapter.EventSourceOnCreateCalled (System.Object sender, MvvmCross.Platform.Core.MvxValueEventArgs`1[T] eventArgs)
[0x0000c] in :0 at (wrapper delegate-invoke) System.EventHandler`1[MvvmCross.Platform.Core.MvxValueEventArgs`1[Android.OS.Bundle]]:invoke_void_object_TEventArgs (object,MvvmCross.Platform.Core.MvxValueEventArgs`1) at MvvmCross.Platform.Core.MvxDelegateExtensionMethods.Raise[T] (System.EventHandler`1[TEventArgs] eventHandler, System.Object sender, T value)
[0x0000b] in <4ddde23419c5494288c799fcdbb0f189>:0 at MvvmCross.Platform.Droid.Views.MvxEventSourceActivity.OnCreate (Android.OS.Bundle bundle)
[0x00014] in <4230cf00b647458f9b3e211590e6cc9d>:0 at Test.Droid.Views.BaseView`1[TViewModel].OnCreate (Android.OS.Bundle bundle)
[0x00000] in <0fa1f44ee2f748fe816e790571c2153b>:0 at Test.Droid.Views.MainMenuView.OnCreate (Android.OS.Bundle bundle)
[0x00000] in <0fa1f44ee2f748fe816e790571c2153b>:0 at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState)
[0x00011] in :0 at (wrapper dynamic-method) System.Object:e6d6ef29-3746-4c8e-b037-c64f6dfe49f0 (intptr,intptr,intptr)
This is a linking issue. You have some kind of DialogProvider, which uses the Android.Text.Html.FromHtml method, which seems to be stripped out.
You can add expressions to LinkerPleaseInclude.cs to prevent the linker from removing code or you can add [Preserve(AllMembers = true)] attribute on your DialogProvider class.
I'm new in Xamarin. I want to add a side menu, so I took this code on GitHub and implement in my project.
I have this AccountsView page:
public partial class AccountsView : ContentPage
{
public AccountsView()
{
InitializeComponent();
GetAccountsAsync();
}
private async void GetAccountsAsync()
{
var accounts = await SQLHelper.Connection.Table<Account>().ToListAsync();
lvwAccounts.ItemsSource = accounts;
}
async void Handle_Clicked(object sender, System.EventArgs e)
{
var account = (Account)((Button)sender).BindingContext;
await Navigation.PushAsync(new AccountDetailsView(account.Id));
}
}
When i click on the menu to go this page i get an exception "object reference not set to an instance of an object" and the inner exception:
" at project.Views.AccountsView+d__2.MoveNext () [0x0000f] in /Users/user/Projects/project/project/Views/AccountsView.xaml.cs:24 \n--- End of stack trace from previous location where exception was thrown ---\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152 \n at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 \n at UIKit.UIKitSynchronizationContext+c__AnonStorey0.<>m__0 () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/UIKit/UIKitSynchronizationContext.cs:24 \n at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/Foundation/NSAction.cs:163 \n--- End of stack trace from previous location where exception was thrown ---\n at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)\n at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/UIKit/UIApplication.cs:79 \n at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/UIKit/UIApplication.cs:63 \n at project.iOS.Application.Main (System.String[] args) [0x00001] in /Users/user/Projects/project/iOS/Main.cs:17 "
If I remove the GetAccountsAsync() method and it calls from ctor all works fine but with no data.
Something with the async call to DB cause the problem.
If I don't implement side menu and master page my code works fine.
Can someone help me with this?
I'm developing mobile app for IOS using Xamarin IOS and when I try to post image to server using xamarin PostAsync() method, unfortunately it raises an exception.
public async Task<string> PostCreatePlatform(Platform new_platform, Byte[] image)
{
new_platform.id = null;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", String.Format("Token {0}", TokenInit.Token));
var response = await client.PostAsync(b + "platforms/", new StringContent(JsonConvert.SerializeObject(new_platform, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }).ToString(),Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
if (image != null)
{
var TempPlatform = await response.Content.ReadAsStringAsync();
var final = JsonConvert.DeserializeObject<Platform>(TempPlatform);
MultipartFormDataContent form = new MultipartFormDataContent();
new_platform.id = final.id.Value;
form.Add(new ByteArrayContent(image, 0, image.Count()), "file");
var response2 = await client.PostAsync(String.Format(b + "platform/{0}/attachments/upload/", new_platform.id.Value), form);
if (response2.IsSuccessStatusCode)
{
return "success";
}
else
{
return "failed";
}
}
return "success";
}
else
{
return "failed";
}
}
InnerException: System.ObjectDisposedException
System.AggregateException: One or more errors occurred. —-> System.AggregateException: One or more errors occurred. —-> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.
at System.Net.WebConnectionStream.EndWrite (System.IAsyncResult r) [0x000b2] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/System/System.Net/WebConnectionStream.cs:616
at System.IO.Stream+<>c.<BeginEndWriteAsync>b__53_1 (System.IO.Stream stream, System.IAsyncResult asyncResult) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/io/stream.cs:729
at at (wrapper delegate-invoke) System.Func`3[System.IO.Stream,System.IAsyncResult,System.Threading.Tasks.VoidTaskResult]:invoke_TResult_T1_T2 (System.IO.Stream,System.IAsyncResult)
at System.Threading.Tasks.TaskFactory`1+FromAsyncTrimPromise`1[TResult,TInstance].Complete (TInstance thisRef, System.Func`3[T1,T2,TResult] endMethod, System.IAsyncResult asyncResult, System.Boolean requiresSynchronization) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/FutureFactory.cs:1441
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:447
at System.Net.Http.MultipartContent+<SerializeToStreamAsync>d__8.MoveNext () [0x00265] in <eadf07f9d3724ef0a06ee9064ef34579>:0
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult () [0x00000] in
/Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:447
at System.Net.Http.HttpClientHandler+<SendAsync>d__63.MoveNext () [0x0031d] in <eadf07f9d3724ef0a06ee9064ef34579>:0
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535
at System.Net.Http.HttpClient+<SendAsyncWorker>d__48.MoveNext () [0x000ca] in <eadf07f9d3724ef0a06ee9064ef34579>:0
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357
at FirstApp.ApiInteracting+<PostCreatePlatform>d__7.MoveNext () [0x003e2] in /Users/team1/Downloads/testiOSbayards2-6d24bc43fee6abd445ebcaea12b76c61649468a6/FirstApp/API/ApiInteracting.cs:176
—- End of inner exception stack trace —-
at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2157
at System.Threading.Tasks.Task`1[TResult].GetResultCore (System.Boolean waitCompletionNotification) [0x0002b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Future.cs:562
at System.Threading.Tasks.Task`1[TResult].get_Result () [0x00000] in
/Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Future.cs:532
at FirstApp.AddPlatformTableViewController+<>c__DisplayClass247_0.<CreatePlatformButtonUpInside>b__0 () [0x00000] in /Users/team1/Downloads/testiOSbayards2-6d24bc43fee6abd445ebcaea12b76c61649468a6/FirstApp/UIClasses/AddPlatformTableViewController.cs:115
at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Future.cs:680
at System.Threading.Tasks.Task.Execute () [0x00010] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2502
—- End of inner exception stack trace —-
at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2157
at System.Threading.Tasks.Task`1[TResult].GetResultCore (System.Boolean waitCompletionNotification) [0x0002b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Future.cs:562
at System.Threading.Tasks.Task`1[TResult].get_Result () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.33/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Future.cs:532
at FirstApp.AddPlatformTableViewController+<>c__DisplayClass247_1.<CreatePlatformButtonUpInside>b__2 () [0x00001] in /Users/team1/Downloads/testiOSbayards2-6d24bc43fee6abd445ebcaea12b76c61649468a6/FirstApp/UIClasses/AddPlatformTableViewController.cs:119
at Foundation.NSActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/4691/3e5ac5ff/source/xamarin-macios/src/Foundation/NSAction.cs:57
at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/4691/3e5ac5ff/source/xamarin-macios/src/UIKit/UIApplication.cs:79
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/4691/3e5ac5ff/source/xamarin-macios/src/UIKit/UIApplication.cs:63
at FirstApp.Application.Main (System.String[] args) [0x00001] in /Users/team1/Downloads/testiOSbayards2-6d24bc43fee6abd445ebcaea12b76c61649468a6/FirstApp/Main.cs:12
Moreover, I reproduced this method using .NET C# compiler(not Mono) and manually with web debugging tool(Fiddler) and everything is ok.
Update:
The problem was resolved. Size of image was too big - 18mb.
Try following code. it works for me. Hope it helps.
public class PostImageData
{
public int success { get; private set; }
public async Task postData(string username, string password,string documentid, StreamContent streamContent)
{
UploadPhoto details = new UploadPhoto();
try
{
System.Diagnostics.Debug.WriteLine("uplod done");
var clinet = new HttpClient();
var content = new MultipartFormDataContent();
content.Add(streamContent, "file", "test.jpeg");
System.Net.Http.HttpResponseMessage r =await clinet.PostAsync(YOUR_URL, content);
string json =await r.Content.ReadAsStringAsync();
details = JsonConvert.DeserializeObject<UploadPhoto>(json);
if (details.message == "Upload success")
{
success = 200;
}
success = 200;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
success = 400;
}
}
public class UploadPhoto
{
public string message { get; set; }
}
}
On Android I am getting this error Java.Lang.IllegalArgumentException:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2058/58099c53/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.m__0 (object) [0x00000] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1006
at Android.App.SyncContext/c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:18
at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36
at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Java.Lang.IRunnable.cs:71
at (wrapper dynamic-method) object.7dac782f-30e4-45cb-a248-e0c4c79fbcad (intptr,intptr)
--- End of managed exception stack trace --- java.lang.IllegalArgumentException at
android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:169) at dalvik.system.NativeStart.run(Native Method)
using this code to retrive *.mp3 file duration:
public async Task<string> GetTrackDuration(string pathToFile)
{
MediaMetadataRetriever reader = new MediaMetadataRetriever();
await reader.SetDataSourceAsync (global::Android.App.Application.Context, Android.Net.Uri.Parse (pathToFile));
return reader.ExtractMetadata(MetadataKey.Duration).ToString();
}
where is the problem? I have tried all of the posible constructors