WCF慢ServiceHost.Open()调用 [英] WCF slow ServiceHost.Open() call

查看:100
本文介绍了WCF慢ServiceHost.Open()调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是与此类似的问题: 用于WCF服务的Win32Exception @ ServiceHost.Open().

This is a similar question as this one: Win32Exception @ ServiceHost.Open() for WCF service.

我的一台机器在下面的ServiceHost.Open调用上非常慢.每次都需要7秒钟左右才能打开服务.这台机器是我的家庭电脑,它是域的一部分.

I have a machine that is very slow on the ServiceHost.Open call below. It consistently takes 7 seconds or so to open the service, every time. This machine is my home box, and it is not part of a domain.

我可以在属于域的另一个框(我的工作框)上运行相同的代码,并且服务主机在第一次调用时会在3-4秒内打开,但是如果我运行程序再次在大约1秒或更短的时间内打开服务主机.

I can run the same code on another box (my work box) which is part of a domain and the service host opens in about 3-4 seconds on the first call, but if I run the program again the service host opens in about 1 second or less.

我已经在此方面获得了MS支持,并且我们生成了跟踪日志,并且挂起的部分在哪里出了,并尝试连接到域,即使在不属于域的机器上也是如此.并得到指定的域不存在或无法联系."跟踪日志中的异常,这就是所有时间都被吃光的地方.

I have worked with MS support on this, and we generated trace logs and the part it's hanging in is where is goes out and tries to connect to a domain, even on the machine that isn't part of a domain. And it gets the "The specified domain either does not exist or could not be contacted." exception in the trace log, and that's where all the time is getting eaten up.

但是真正奇怪的是,即使在我的工作计算机上,如果我没有连接到域(例如,如果我不在工作网络中并且只是在家中运行),我仍然不要得到延迟.

But what's really weird is that even on my work machine, if I'm not connected to a domain (like if I'm not on my work network and just running from home) I still don't get the delay.

我使用64位Windows 7重建了机器,并且发生了相同的行为(运行XP SP3,当Windows 7似乎无法解决问题时,我将其还原了.)

I rebuilt my machine using Windows 7 64-bit, and the same behavior occurs (was running XP SP3, which I restored when Windows 7 didn't seem to fix the problem).

我只是想知道是否有人对导致这种情况的原因有任何想法.顺便说一句,如果我禁用"Microsoft网络客户端",则打开ServiceHost的时间大约为4秒,但这仍然不如以前的机器能够打开服务的速度快.以某种方式,它认为它应该是域或其他内容的一部分.

I just wondered if anyone had any ideas of what could cause this. By the way, if I disable "Client for microsoft networks", the time is like 4 seconds to open the ServiceHost, but that's still not as fast as this machine used to be able to open the service. Somehow, it thinks it's supposed to be part of a domain or something.

    static void RunServiceWithinEXE()
    {
        Uri baseAddress = new Uri("http://localhost:11111/Demo");
        ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);

        try
        {
            // Add a service endpoint
            serviceHost.AddServiceEndpoint(
                typeof(ICalculator),
                new WSHttpBinding(),
                "CalculatorService");

            // Enable metadata exchange
            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.");

            // Close the ServiceHostBase to shutdown the service.
            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;
    }

这是我的app.config,但其中没有用于该服务的任何特殊安全配置设置,只有一些诊断设置可启用WCF跟踪.

Here is my app.config, but I don't have any special security configuration settings for the service in there, only some diagnostic settings to enable the WCF trace.

<?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>

还请注意,我的服务定义具有必需的SessionMode(请参见下文).如果我取消SessionMode要求,我就不会延迟.

Note also that my service definition has SessionMode required (see below). If I take the SessionMode requirement out, I don't get the delays.

using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Microsoft.ServiceModel.Samples
{
    // Define a service contract.
    [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);

    }
}

推荐答案

事实证明,如果我在网络连接上禁用了netbios,则不会在ServiceHost.Open调用上出现延迟.我不确定为什么,但这似乎可以解决问题.

It turns out that if I disable netbios on my network connections, I don't get the delays on the ServiceHost.Open calls. I am not sure why, but this seemed to fix the problem.

奇怪的是,当我进行XP的全新安装时,即使启用了Netbios,我也没有延迟,所以我不知道为什么在现有安装中会发生这种情况(我在同一位置进行了全新安装计算机,然后从映像还原我的备份以运行这些测试.

What's weird is that when I did a clean install of XP, I didn't have the delays even with the Netbios enabled, so I have no idea why it happens with my existing installation (I did the clean install on this same machine, and then restored my backup from an image to run these tests).

这篇关于WCF慢ServiceHost.Open()调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆