FubarDevelopment / restsharp.portable

Some kind of a RestSharp port to PCL
BSD 2-Clause "Simplified" License
96 stars 33 forks source link

ArgumentException: The 'User-Agent' header cannot be modified directly. #103

Closed icnocop closed 6 years ago

icnocop commented 6 years ago

Visual Studio 2015 Update 3 Silverlight SDK 5.0.61118.0 Silverlight Unit Test Project run with statlight 1.6.4375.31943 FubarCoder.RestSharp.Portable 3.3.0 Silverlight 5.1.50907.0

RestClient client = new RestClient("https://localhost/MyWebApplication/api/Users");

IRestRequest request = new RestRequest(Method.POST)
    .AddParameter("Content-Type", "application/json", ParameterType.HttpHeader)
    .AddParameter("application/json", user, ParameterType.RequestBody);

Task<IRestResponse> task = client.Execute(request);

this.EnqueueConditional(() => task.IsFaulted || task.IsCompleted);
this.EnqueueCallback(() =>
{
    Assert.IsNull(task.Exception, task.Exception.ToString());

    IRestResponse response = task.Result;
    Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, $"{response.StatusCode} {response.StatusDescription} {response.Content}");
});

Exception:

Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException: Assert.IsNull failed. System.AggregateException: One or more errors occurred. ---> System.ArgumentException: The 'User-Agent' header cannot be modified directly. Parameter name: name at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task) at RestSharp.Portable.HttpClient.RestClient.d5.MoveNext() --- End of inner exception stack trace --- ---> (Inner Exception #0) System.ArgumentException: The 'User-Agent' header cannot be modified directly. Parameter name: name at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task) at RestSharp.Portable.HttpClient.RestClient.d5.MoveNext()<---

at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.HandleFail(String assertionName, String message, Object[] parameters) at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsNull(Object value, String message) at MyWebApplication.Tests.Silverlight.UserTests.CreateUserTest() at Microsoft.Silverlight.Testing.Harness.CallbackWorkItem.Invoke() at Microsoft.Silverlight.Testing.Harness.CompositeWorkItem.Invoke(WorkItem& usedWorkItem)

If I downgrade to FubarCoder.RestSharp.Portable 2.4.5 the exception is not thrown.

fubar-coder commented 6 years ago

Ugh, this is a difficult thing, because sometimes the property isn't available, but this exception is still thrown. This project is currently on stand-by, so please consider using the HttpClient directly, as this library is effectively a wrapper around HttpClient.