Hello, I am using DNOA to communicate from an Outlook AddIn and a WebApi website.
I have the following code in my Outlook AddIn:
// snippet
var consumer = CreateConsumer();
HttpWebRequest req = consumer.PrepareAuthorizedRequest(new MessageReceivingEndpoint(WebApiAddress + ODataAddress + "/" + "TimesheetViews",
HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest), ActualAccessToken);
req.ContentLength = stream.Length;
req.ContentType = "application/json";
stream.CopyTo(req.GetRequestStream());
consumer.Channel.WebRequestHandler.GetResponse(req);
When the server returns a 500 internal error DNOA throws an ProtocolException and when I try to read the response like this:
var stringbuffer = new StringBuilder();
using (var stream = ((System.Net.WebException)ex.InnerException).Response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
String data = reader.ReadToEnd();
stringbuffer.Append(data);
}
}
It was already consumed and closed by DNOA. I searched a lot of sites and I found out that I need to implement my own IDirectWebRequestHandler.
So I did my class as:
public class CustomWebRequestHandler : IDirectWebRequestHandler
{
//code
}
And changed the request code to:
var consumer = CreateConsumer();
HttpWebRequest req = consumer.PrepareAuthorizedRequest(new MessageReceivingEndpoint(WebApiAddress + ODataAddress + "/" + "TimesheetViews",
HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest), ActualAccessToken);
req.ContentLength = stream.Length;
req.ContentType = "application/json";
stream.CopyTo(req.GetRequestStream());
consumer.Channel.WebRequestHandler = new CustomWebRequestHandler(); // here i tell to use my implementation of IDirectWebRequestHandler
consumer.Channel.WebRequestHandler.GetResponse(req);
Now I have 2 questions:
1 - When Andrew says to plug an IDirectWebRequestHandler implementation in DNOA is it as I am doing here? => consumer.Channel.WebRequestHandler = new CustomWebRequestHandler();
I don't know what I am doing wrong, any help will be really appreciated.
Thanks!