This is a similar question, like this one:
Win32Exception @ ServiceHost.Open () for WCF service .
I have a machine that is very slow on calling ServiceHost.Open below. It constantly takes 7 seconds or so to open the service every time. This machine is my home box, and it is not part of the domain.
I can run the same code in another field (my work box), in which there is a part of the domain, and the service host opens after about 3-4 seconds on the first call, but if I run the program it starts the service host again after about 1 second or less .
I worked with MS support on this subject, and we created trace logs, and the part in which it hangs is where it goes and tries to connect to the domain, even on a machine that is not part of the domain. And he gets "The specified domain either does not exist or cannot be connected." an exception in the trace log and where it is eaten all the time.
But what is really strange is that even on my work computer, if I am not connected to the domain (for example, if I am not on my work network and just running away from home), I still do not get a delay.
I rebuilt my machine using the 64-bit version of Windows 7, and this happens in different ways (XP SP3 was launched, which I restored when Windows 7 did not seem to fix the problem).
I just wondered if anyone has any ideas on what might cause this. By the way, if I disable the "Client for microsoft networks", the time, like 4 seconds, will open ServiceHost, but it is still not as fast as this computer used to open the service. One way or another, he believes that it should be part of a domain or something like that.
static void RunServiceWithinEXE()
{
Uri baseAddress = new Uri("http://localhost:11111/Demo");
ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);
try
{
serviceHost.AddServiceEndpoint(
typeof(ICalculator),
new WSHttpBinding(),
"CalculatorService");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(smb);
serviceHost.Opening += new EventHandler(serviceHost_Opening);
serviceHost.Opened += new EventHandler(serviceHost_Opened);
serviceHost.Open();
Console.WriteLine("The service is ready.");
serviceHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occured: {0}", ce.Message);
serviceHost.Abort();
}
}
static void serviceHost_Opened(object sender, EventArgs e)
{
TimeSpan timeToOpen = DateTime.Now - shOpening;
Console.WriteLine("Time To Open: :" + timeToOpen.Seconds);
}
static void serviceHost_Opening(object sender, EventArgs e)
{
shOpening = DateTime.Now;
}
Here is my app.config, but I do not have special security configuration settings for this service, there are only some diagnostic settings to enable WCF tracing.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<diagnostics>
<messageLogging maxMessagesToLog="30000"
logEntireMessage="true"
logMessagesAtServiceLevel="false"
logMalformedMessages="true"
logMessagesAtTransportLevel="true">
<filters>
<clear/>
</filters>
</messageLogging>
</diagnostics>
</system.serviceModel>
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Warning, ActivityTracing" propagateActivity="true" >
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging" switchValue="Warning">
<listeners>
<add name="xml" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\Temp\Server.svclog" />
</sharedListeners>
<trace autoflush="true" indentsize="4">
<listeners>
<remove name="Default" />
<add name="ScottsConsoleListener" type="System.Diagnostics.ConsoleTraceListener" />
<add name="ScottsTextListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Temp\DebugLog.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Please also note that SessionMode is required for my service definition (see below). If I exit the SessionMode requirement, I do not get a delay.
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Microsoft.ServiceModel.Samples
{
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode = SessionMode.Required)]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
[OperationContract]
string PrintName(string firstName, string lastName);
[OperationContract]
Point MakePoint(double x, double y);
}
}