是否可以从 WCF 服务中的静态构造函数调用实例方法?

发布于 2024-08-24 21:46:57 字数 587 浏览 2 评论 0原文

是否可以从 WCF 服务中的静态构造函数调用实例方法?是否有类似当前上下文之类的东西,我可以通过它获取 MyService 的当前实例?

public class MyService : IMyService
{
    static MyService()
    {
        //how to call Func?
    }

    private void Func()
    {
    }
}

编辑:

这个问题是 WCF 问题,而不是关于从静态方法调用实例方法的简单语言问题。以下是 Web 应用程序中类似情况的示例:

public class MyPage : Page
{
    static MyPage()
    {
        var page = (MyPage)HttpContext.Current.Handler;
        page.Func();
    }

    private void Func()
    {
    }

}

因此,我希望在 WCF 中,当调用服务时,存在一些具有当前正在执行的 MyService 实例的全局上下文。

Is it possible to call an instance method from a static constructor in WCF service? Is there something like current context through which I can get the current instance of MyService?

public class MyService : IMyService
{
    static MyService()
    {
        //how to call Func?
    }

    private void Func()
    {
    }
}

EDIT:

This question is WCF question, not a simple language one about calling an instance method from a static one. Here is an example of similar case in web application:

public class MyPage : Page
{
    static MyPage()
    {
        var page = (MyPage)HttpContext.Current.Handler;
        page.Func();
    }

    private void Func()
    {
    }

}

So I expect that in WCF while a call to a service exist some global context that has the currently executing instance of MyService.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一曲琵琶半遮面シ 2024-08-31 21:46:57

这里拿出WCF服务——这不是一个WCF问题,它是一个纯粹的基本C#语言问题。与该类作为服务完全无关。

答案是否定的。

静态构造函数没有调用实例函数的业务——它没有实例的引用。更改类设置,这样就不需要了。设计错误。最有可能的是,代码不应该在静态构造函数中,而应该在实例构造函数中。

Take out the WCF service here- this is not a WCF qwuestion, it is a pure basic C# langauge question. Has nothing to do with the class being a service at all.

The answer is NO.

The static constructor has no business calling an instance function - it has no reference of an instance. Change class setup so that is not required. Design error. Most likely the code should not be i na STATIC contructor but in the instance constructor.

赴月观长安 2024-08-31 21:46:57

无法从静态构造函数调用实例方法。您不知道 CLR 何时会调用这个静态构造函数。您所知道的是,它将在创建该对象的任何实例之前调用。如果没有对象的实例,则无法调用实例方法。

It is not possible to call an instance method from a static constructor. You don't know when the CLR will invoke this static constructor. All you know is that it will be invoked before any instances of this object has been created. And you cannot call an instance method without having an instance of the object.

○闲身 2024-08-31 21:46:57

嗯,这是可能的。你能解释一下为什么你需要这个吗?

public class MyService : IMyService
    {
        static MyService()
        {
            new MyService().Func();
        }

        private void Func()
        {
        }
    }

Well, it is possible. Can you explain why do you need this?

public class MyService : IMyService
    {
        static MyService()
        {
            new MyService().Func();
        }

        private void Func()
        {
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文