When using "Right-click >> Add >> REST Api client" in vs 2015 I've created client classes for my rest api. It's convenient for other guys to quickly get going with my api. But we have to use windows credentials.
So when the class is generated looks like that:
public partial class MarvalWebApi : ServiceClient<MarvalWebApi>, IMarvalWebApi
{
private Uri _baseUri;
/// <summary>
/// The base URI of the service.
/// </summary>
public Uri BaseUri
{
get { return this._baseUri; }
set { this._baseUri = value; }
}
private ServiceClientCredentials _credentials;
/// <summary>
/// Credentials for authenticating with the service.
/// </summary>
public ServiceClientCredentials Credentials
{
get { return this._credentials; }
set { this._credentials = value; }
}
...
But I can't find a way to set this Credentials to anything like Windows Credentials.
I tried to set the credentials in web request handler:
var client = new MarvalWebApi();
var webRequest = client.HttpMessageHandlers.OfType<WebRequestHandler>().First();
webRequest.UseDefaultCredentials = true;
//client.HttpMessageHandlers
var res = client.OpImport
.PostAsync(new MyParams())
.ContinueWith(t =>
{
var e = t.Exception;
});
Task.WaitAll(res);
I get exception when executing the request.
One or more errors occurred. Error while copying content to a stream. The request was aborted: The request was canceled.
--- Error while copying content to a stream.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at ConsoleApplication1.OpImport.<PostWithOperationResponseAsync>d__4.MoveNext() in C:\Git\ConsoleApplication1\ConsoleApplication1\MarvalWebApi\OpImport.cs:line 111
--- 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.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at ConsoleApplication1.OpImportExtensions.<PostAsync>d__1.MoveNext() in C:\Git\ConsoleApplication1\ConsoleApplication1\MarvalWebApi\OpImportExtensions.cs:line 42
--- The request was aborted: The request was canceled.
at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
at System.Net.Http.StreamToStreamCopy.StartRead()
Can you help me understand any of these, please?