current OperationContext is null in WCF Windows Service

后端 未结 5 1627
感情败类
感情败类 2020-12-19 03:20

I am trying to set up a Publish/Subscribe system using WCF and where the WCF server is in a Windows service. The binding is net.TCP. The service is providing a \"Subscribe

5条回答
  •  难免孤独
    2020-12-19 04:14

    I've faced this issue and non of the solutions worked and Most Important thing is if you're using

    async await 
    OperationContext.Current; will be null
    

    My usage is to get Ip so used it like this before any awaitable call

    var clientIpAddress = System.Web.HttpContext.Current?.Request?.UserHostAddress;
    

    After the first await statement in your async service operation, OperationContext.Current could be null because the rest of the method body may be running on a different thread (and OperationContext does not flow between threads

    So to get it you can write your code before any awaitable action

    May be it'll help someone :)

提交回复
热议问题