Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get access to the WCF service instance in the current context?

Tags:

c#

.net

wcf

If I am executing within the context of a particular service instance and operation, how do I get access to the currently-executing service instance? Service instances don't inherit from any specific common base class or interface and the only pathway into the existing context that I can find is:

OperationContext.Current

but I can't seem to find any properties that reference the actual service instance itself so that I can cast it to what I know it should be and perform operations on it.

Without exploring why I am doing this (it's irrelevant), please let me know if there is any way to find the reference I am looking for.

EDIT:

[ServiceContract]
public interface IInventory
{
    [OperationContract]
    List<DealInfo> ListDeals(DealQueryOptions options);
}

// This is the object I will need access to the current instance of
public class Inventory : ServiceBase<Inventory>, IInventory
{
    public List<DealInfo> ListDeals(DealQueryOptions options)
    {
        var obj = new Whatever(); // see below
    }
}

public class Whatever
{
    public Whatever()
    {
        // how do I get access to the service instance here?
        // assume that in this context we are not allowed to
        // pass the service instance to this class; this class
        // must automatically discover the instance itself.
    }
}
like image 932
Nathan Ridley Avatar asked Jun 30 '09 08:06

Nathan Ridley


1 Answers

var myService = OperationContext.Current.InstanceContext.GetServiceInstance();
like image 126
Nathan Ridley Avatar answered Oct 05 '22 23:10

Nathan Ridley