开发者

WCF "Instance already exists in CounterSet" error when reopening ServiceHost

开发者 https://www.devze.com 2023-01-02 21:13 出处:网络
I have a working ServiceHost with a single NetTcpBinding and a single endpoint. I .Close() it. Then, I create a new ServiceHost instance with the exact same configuration as the first one. Then, when

I have a working ServiceHost with a single NetTcpBinding and a single endpoint.

I .Close() it. Then, I create a new ServiceHost instance with the exact same configuration as the first one. Then, when I try to .Open() the new instance I'm getting this very awkward exception:

System.ArgumentException occurred
  Message=Instance 'LobbyService@net.tcp:||localhost:2718|game|' already exists in CounterSet 'e829b6db-21ab-453b-83c9-d980ec708edd'.
Parameter name: InstanceName
  Source=System.Core
  ParamName=InstanceName
  StackTrace:
       at System.Diagnostics.PerformanceData.CounterSetInstance..ctor(CounterSet counterSetDefined, String instanceName)

Has anybody seen that before? Is it a bug in the .NET Framework (I'm using 4.0, by the way)?

Probably relevant info about my ServiceHost:

  • No clients are connected to the host, when it is first closed;
  • A custom IInstanceProvider is used to create instances;
  • The binding's ReliableSession is turned on;
  • The service type is marked with the ServiceBehavior below;

.

[ServiceBehavior(
IncludeExceptionDetailInFaults = true,
InstanceContextMode=InstanceContextMode.PerSession,
ConcurrencyMode=ConcurrencyMode.Reentrant,
UseSynchronizationContext = false
)]开发者_开发问答

I'm open to reveal any extra info you might want to know about the application.

Update 1 I compiled the application targeting .NET 3.5 and the error did NOT happened. Unfortunately, I have to deactivate everything that relied in Task's.

Update 2 I logged a bug at Microsoft Connect about this issue. I guess this question is already answered now.


This problem has no relation to WCF service itself, but to System.ServiceModel.Diagnostics, that allows to monitor service by Performance counters. It creates counter set for each service host and generate name for set by ServiceHost parameters. And, if host with same parameters already exists - it will cause this exception. See Microsoft sources here (was found by GUID). It's easy to aviod it: just turn off performance counters for services:

  1. By app.config (tested)

    <configuration>
        <system.serviceModel>
            <diagnostics performanceCounters="Off" />
        </system.serviceModel>
    </configuration>
    
  2. In runtime (not tested)

    using System.Configuration;
    using System.ServiceModel.Configuration;
    using System.ServiceModel.Diagnostics;
    
    Configuration config = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None);
    ServiceModelSectionGroup sg = ServiceModelSectionGroup.GetSectionGroup(config);
    sg.Diagnostic.PerformanceCounters = PerformanceCounterScope.Off;
    config.Save();
    

P.S. I got this problem on VS 2012, .Net 4.5 and VS 2010, .Net 4.0. I think, it's related to software configuration (VS?), but I have no idea to which software and to which parameter. My coworkers has no such problem using, in general, same environment.


This is a bug with .NET Framework 4.0. I logged a bug at Microsoft Connect about it.

Here's the answer from Microsoft:

This looks like a known problem caused by timing issues in tight Close/open sequence of ServiceHost. ServiceHost maintains some performance counters that might not get garbage collected leading to this exception. I assume you are using .Net Framework 4.0? Please try to workaround by doing a forced GC before you open the second ServiceHost:

GC.Collect() 
GC.WaitForPendingFinalizers()

Doing what they advised solved the problem. I hope it gets fixed in a later release.

0

精彩评论

暂无评论...
验证码 换一张
取 消