Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正确清理System.ServiceModel.ServiceHost_C#_Wcf_Idisposable_Servicehost - Fatal编程技术网

C# 正确清理System.ServiceModel.ServiceHost

C# 正确清理System.ServiceModel.ServiceHost,c#,wcf,idisposable,servicehost,C#,Wcf,Idisposable,Servicehost,我对清理ServiceHost的最佳方法有点困惑。我意识到了代码中的问题,因为VisualStudio代码分析器发出CA1001警告,建议我为类实现IDisposable接口 我已经阅读了关于IDisposable的讨论,并且熟悉典型的用例,但是在这个例子中我发现自己很困惑。什么是确保ServiceHost被处置并可能满足CA1001要求的正确方法。谢谢 我的代码如下所示: public class MyClass { private ServiceHost host = null;

我对清理ServiceHost的最佳方法有点困惑。我意识到了代码中的问题,因为VisualStudio代码分析器发出CA1001警告,建议我为类实现IDisposable接口

我已经阅读了关于IDisposable的讨论,并且熟悉典型的用例,但是在这个例子中我发现自己很困惑。什么是确保ServiceHost被处置并可能满足CA1001要求的正确方法。谢谢

我的代码如下所示:

public class MyClass
{
    private ServiceHost host = null;

    public void StartListening(...)
    {
        // make sure we are not already listening for connections
        if (host != null && host.State != CommunicationState.Closed)
            StopListening();

        // create service host instance
        host = new ServiceHostWithData(typeof(ServiceHandler), address);

        // do a bunch of configuration stuff on the host

        host.Open();
    }

    public void StopListening()
    {
        // if we are not closed
        if ((host != null) && (host.State != CommunicationState.Closed))
        {
            host.Close();
            host = null;
        }
        else // we are null or closed
        {
            host = null; // if it isn't null, and it is already closed, then we set it to null
        }
    }
}

您的类应该实现。根据该MSDN页面中的示例:

public class MyClass : IDisposable
{
    private bool disposed = false;
    private ServiceHost host = null;

    public void StartListening(...)
    {
        // ....
    }

    public void StopListening()
    {
        // ...
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    { 
        if(!this.disposed)
        {
            if(disposing)
            {
                this.StopListening();
            }

            disposed = true;
        }
    }

    ~MyClass()
    {
        Dispose(false);
    }
}

我写了一个虚拟主机,做了你所做的。我没有得到警告。警告是否指向此类?您可以使用(ServiceHost)作为msdn示例在其examples@Dhawalk对VisualStudio代码分析器非常具体。您可能会对编译器警告感到困惑,在本例中没有任何警告。@ilansch在本例中,使用块有什么帮助?很明显,他希望在控件离开StartListening(…)方法后主机保持打开状态。谢谢。就ServerHost而言,调用ServerHost.Close相当于在MyClass Dispose函数中调用ServerHost.Dispose?Close和Dispose本质上是相同的(请参见此处的答案:),因此您可以使用其中一种。请注意,ServiceHost上的close/dispose会引发异常(!),因此您可能希望尝试/捕获该块,尤其是在MyClass dispose函数中执行其他工作时。