HttpContext in .net standard library

前端 未结 7 512
南旧
南旧 2020-12-09 08:02

I am working on couple of projects one of which is an ASP.NET 4.5 application and other one is .Net Core API 1.1 project. The asp.net application i

7条回答
  •  有刺的猬
    2020-12-09 08:30

    I know you want HttpContext but maybe will little help to create any channel. Therefore I give an ContextChannel example. Because I'm interested http context similar to that.

    I just create ContextChannel and then I use OperationContext because If I've not created context scope Context Channel always is getting null.

    In my example, it works following way;

      ChannelFactory factory = channelFactory;
            factory.Endpoint.EndpointBehaviors.Add(new ClientMessagePropertyBehavior());
    
            var proxy = (IClientChannel)factory.CreateChannel();
    
      using (var contextScope = new OperationContextScope((IContextChannel)proxy))
                {
                    Stopwatch sw = Stopwatch.StartNew();
    
                    HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
                    requestMessage.Headers["Authorization"] = accessToken;
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
    
                    TResult result = codeBlock((T)proxy);
    
                    sw.Stop();
    
                    _tools.logInfo(string.Format(@"* Service client performance : {0} = {1} ms", codeBlock.Method.ToString(), sw.ElapsedMilliseconds));
    
                    success = true;
                    return result;
                }
    

提交回复
热议问题