Check if you are on wcf service

I am using a WCF service and wondering if I can use OperationContract methods for the caller and for the service. So I would like to know how best to tell if the code is running in an application or in a service.

Like this:

[ServiceContract]
public interface IService
{
    [OperationContract]
    bool ServiceMethod(string param);
}

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single,
InstanceContextMode = InstanceContextMode.Single, UseSynchronizationContext=false)]
public class Service : IService
{
    bool ServiceMethod(string param)
    {
      if(!isInWcfService) //How to do this?
      {
        //Call this ServiceMethod in WCF Service
      }
      else
      {
        //Do the work
      }
    }
  }

      

Since the caller and the service both know this class, I think it would be easier if both just call this method and it decides for itself whether it should forward the call to the service or if it just might work.

Thank!

+3


source to share


1 answer


You can check if you are in a WCF service by checking OperationContext.Current

which is a WCF service class that is comparable to HttpContext.Current

in ASP.NET:



if (OperationContext.Current != null)
{
    // inside WCF
}
else
{
    // not
}

      

+2


source







All Articles