-2

I have WCF service and inside this service using MEF i am calling runtime class library.

I have implemented DI(castle Windsor) in this class library and it work ok in all request but instantly some time it return error like below

"HttpContext.Current is null. PerWebRequestLifestyle can only be used in ASP.Net"

We have apply all internet solution like install Asp.net feature, change DI Lifecycle from LifestylePerWebRequest to scope , hibridgeLifeStyle etc But not found proper result below is my code

enter image description here


enter image description here

Can any one plese help me solve this issue. As i am new in DI and don't this is Life cycle issue or MEF issue or threading issue because i am calling this class libreary in to thread

Thanks in advance

Mansinh
  • 1,365
  • 4
  • 19
  • 47

1 Answers1

0

You need to run WCF in ASP.Net compat mode to get access to HttpContext. Not sure why you need it though because then you can only run the WCF services on IIS in asp.net compat mode and not within a different hosting environment like a windows service.

more information here: https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-services-and-aspnet which also tells you about contexts you should be using instead of HttpContext

Thomas Schmidt
  • 359
  • 2
  • 11
  • Thanks for replay and its already runningin ASP.net compact mode and yes we wanted this service on IIS only not in windows service – Mansinh Mar 19 '19 at 11:45
  • I can't really see you doing any DI in your example code as I can't see your constructors, but there is a facility for castle windsor to add WCF support: https://www.nuget.org/packages/Castle.WcfIntegrationFacility/ And the documentation: https://github.com/castleproject/Windsor/blob/master/docs/facilities.md An old blog post describing how to use it: http://prideparrot.com/blog/archive/2012/2/dependency_injection_in_wcf_using_castle_windsor – Thomas Schmidt Mar 19 '19 at 14:07