2009-06-30 82 views
12

如果我在特定服務實例和操作的上下文中執行,如何訪問當前正在執行的服務實例?服務實例不從任何特定的公共基類或接口,唯一的途徑在現有的情況下,我能找到繼承是:如何在當前上下文中訪問WCF服務實例?

OperationContext.Current 

,但我似乎無法找到引用的實際服務的任何屬性實例本身,以便我可以將其轉換爲我應該知道的內容並對其執行操作。

沒有探討爲什麼我這樣做(這是無關緊要的),請讓我知道是否有任何方法可以找到我正在尋找的參考。

編輯:

[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. 
    } 
} 
+0

對不起的屬性,你能解釋多一點正是你在找什麼? OperationContext.Current應該以這種或那種方式提供您需要的一切。 – 2009-06-30 08:47:46

回答

18
var myService = OperationContext.Current.InstanceContext.GetServiceInstance(); 
0

OperationContext.Current應該有這個IIRC

相關問題