我如何可以访问WCF服务实例,在当前的背景下? [英] How do I get access to the WCF service instance in the current context?

查看:121
本文介绍了我如何可以访问WCF服务实例,在当前的背景下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我是一个特定的服务实例和操作的范围内执行的,我如何才能访问到当前执行的服务实例?服务实例不从任何特定的公共基类或接口,唯一的途径到现有的情况下,我能找到继承是:

  OperationContext.Current
 

但我似乎无法找到引用实际的服务实例本身的任何属性,这样我可以把它转换为我所知道应该和在其上执行操作。

如果没有探索,为什么我这样做(这是不相关的),请让我知道,如果有任何的方式找到我要找的参考。

编辑:

  [的ServiceContract]
公共接口IInventory
{
    [OperationContract的]
    名单< D​​ealInfo> ListDeals(DealQueryOptions选项);
}

//这是对象我需要的当前实例访问
公共类库存:ServiceBase的<库存>中IInventory
{
    公开名单< D​​ealInfo> ListDeals(DealQueryOptions选项)
    {
        VAR OBJ =新的不管(); // 见下文
    }
}

公共类无论
{
    公共不管()
    {
        //我怎么可以访问这里的服务实例?
        //假设,在这方面,我们不能
        //通过服务实例这类人。这个类
        //必须自动发现实例本身。
    }
}
 

解决方案

  VAR为myService = OperationContext.Current.InstanceContext.GetServiceInstance();
 

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.
    }
}

解决方案

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

这篇关于我如何可以访问WCF服务实例,在当前的背景下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆