【问题标题】:Allowing operations to access shared context (such as OperationContext in WCF)允许操作访问共享上下文(如 WCF 中的 OperationContext)
【发布时间】:2012-06-22 11:28:40
【问题描述】:

我想在我的应用程序(.NET、C#)中为正在进行的操作实现特定上下文,以便应用程序启动和执行的代码可以访问它。

问题是上下文没有像某些 API 那样作为对象传递。

我想在 WCF 中实现类似于 OperationContext 的东西,这个静态类可以为每个操作提供其上下文。

如何实现这样的系统?

示例用法:

我们的应用程序加载并执行由有权访问我们 SDK 的任何人创建的第 3 方“插件”。

这样的用户代码可能如下所示:

public void SomeMethod(string a, string b)
{
    // NEEDED: something like this
    Context.Results = new Results();

    // Do some stuff here

    // Call another method (without passing Results to it)
    AnotherMethod();
}

public void AnotherMethod()
{
     // NEEDED: Access the context (Results) somehow to add more info to it.
     Context.Results.Add(something);
}

根据这个例子,我只需要一些静态上下文类,但我如何确保它对于我的应用程序中执行的每个操作都是唯一的?

【问题讨论】:

  • 网络表单应用程序?表格? WPF? ASP.NET MVC? CF? WP7?

标签: c# .net winforms operationcontext


【解决方案1】:

作为一种选择,如果您要在解决方案中引入像 Autofac 这样的 IoC 容器,它可以在创建对象时提供对共享上下文的访问。

参见this SO question 的示例,它指向this article 关于对象关系类型。它应该让您对这种方法的可能性有一些想法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    相关资源
    最近更新 更多