1

This week without any code change, our legacy WCF web service started to throw "The value of OperationContext.Current is not the OperationContext value installed by this OperationContextScope" error on our API management system.

We have checked installed .net version (.net 4.5.1) and other installations. All seems OK. When we have tried to debug on local computer, we can not reproduce same error.

My related code part is below:

internal static T InvokeService<T>(this CalculatorClient client, Func<CalculatorClient, T> method)
        {
            if (client.ClientCredentials.UserName.UserName == null)
            {
                client.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["APIMANAGEMENT_User"];
                client.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["APIMANAGEMENT_Pwd"];
            }

            T result;

            using (new OperationContextScope(client.InnerChannel))
            {
                string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(ConfigurationManager.AppSettings["APIMANAGEMENT_User"] + ":" + ConfigurationManager.AppSettings["APIMANAGEMENT_Pwd"]));
                HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
                requestMessage.Headers["Authorization"] = auth;
                OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
                result = method.Invoke(client);
            }

            return result;
        }

Do you have any idea about why it is failing?

Ibrahim Schi
  • 309
  • 1
  • 9
  • Have you ever used asynchronous methods, you need to delete asynchronous methods, you can see [this post](https://stackoverflow.com/questions/44192260/the-value-of-operationcontext-current-is-not-the-operationcontext-value-installe) with the same problem. – Lan Huang Mar 17 '22 at 08:34
  • Yes, I have checked. It is call by one asynchronous method. But, I did not get why it was working before and failed this week? – Ibrahim Schi Mar 17 '22 at 15:38

0 Answers0