0

In one of our code, we are getting below error.

at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Web.Util.Misc.ThrowIfFailedHr(Int32 hresult)
   at System.Web.Hosting.IIS7WorkerRequest.SetUnknownRequestHeader(String name, String value, Boolean replace)
   at System.Web.HttpHeaderCollection.SetHeader(String name, String value, Boolean replace)
   at System.Web.HttpHeaderCollection.Add(String name, String value)

Code is as below:

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            string correlationId = newAuditTrail.GetCorrelationIdFromRequest(request).ToString();
            string url = newAuditTrail.GetUrlFromRequest(request).ToString();

            HttpContext.Current.Request.Headers.Add("CorrelatinId", correlationId);
            HttpContext.Current.Request.Headers.Add("Url", url);

Error is thrown on line:

HttpContext.Current.Request.Headers.Add("CorrelatinId", correlationId);

I noticed operation contract of method, it is defined as oneway.

[OperationContract(IsOneWay=true)]
VJOY
  • 3,752
  • 12
  • 57
  • 90

1 Answers1

0

If you want to add http header in the http request, please refer to the below code segements.

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request,  System.ServiceModel.IClientChannel channel)
{
    HttpRequestMessageProperty httpRequestMessage;
    object httpRequestMessageObject;
    if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
    {
        httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
        if (string.IsNullOrEmpty(httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER]))
        {
            httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER] = this.m_userAgent;
        }
    }
    else
    {
        httpRequestMessage = new HttpRequestMessageProperty();
        httpRequestMessage.Headers.Add(USER_AGENT_HTTP_HEADER, this.m_userAgent);
        request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
    }
    return null;
}

We could also use WebOperationContext to add http header on the client-side.
Add Request header to WCF when using ConfigurationChannelFactory.CreateChannel
Please refer to the below discussion.
How to add a custom HTTP header to every WCF call?
Feel free to let me know if the problem still exists.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22