WCF客户端连接超时

时间:2018-07-11 16:17:31

标签: c# wcf

  

“ System.TimeoutException”类型的未处理异常发生在   mscorlib.dll附加信息:打开操作没有   在指定的00:01:00超时内完成。分配的时间   此操作可能是较长超时的一部分。

我有这样的主机设置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
    </startup>
  <system.serviceModel>
    <services>
      <!-- Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages -->
      <service name="ListPublishSubscribe.Service.PubSubService" behaviorConfiguration="returnFaults">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/PubSubService/Service.svc" />
          </baseAddresses>
        </host>
        <endpoint contract="ListPublishSubscribe.Service.IPubSubService" binding="wsDualHttpBinding" address="" />
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="returnFaults">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <system.webServer>
    <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>


<% @ServiceHost Language=C# Debug="true" Service="ListPublishSubscribe.Service.PubSubService" CodeBehind="Service.cs" %>

using System;
using System.ServiceModel;

namespace ListPublishSubscribe.Service
{
    [ServiceContract(Namespace = "http://ListPublishSubscribe.Service", SessionMode = SessionMode.Required, CallbackContract = typeof(IPubSubContract))]
    public interface IPubSubService
    {
        [OperationContract(IsOneWay = false, IsInitiating = true)]
        void Subscribe();
        [OperationContract(IsOneWay = false, IsInitiating = true)]
        void Unsubscribe();
        [OperationContract(IsOneWay = false)]
        void PublishNameChange(string Name);
    }

    public interface IPubSubContract
    {
        [OperationContract(IsOneWay = true)]
        void NameChange(string Name);
    }

    public class ServiceEventArgs : EventArgs
    {
        public string Name;
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class PubSubService : IPubSubService
    {
        public delegate void NameChangeEventHandler(object sender, ServiceEventArgs e);
        public static event NameChangeEventHandler NameChangeEvent;

        IPubSubContract ServiceCallback = null;
        NameChangeEventHandler NameHandler = null;

        public void Subscribe()
        {
            ServiceCallback = OperationContext.Current.GetCallbackChannel<IPubSubContract>();
            NameHandler = new NameChangeEventHandler(PublishNameChangeHandler);
            NameChangeEvent += NameHandler;
        }

        public void Unsubscribe()
        {
            NameChangeEvent -= NameHandler;
        }

        public void PublishNameChange(string Name)
        {
            ServiceEventArgs se = new ServiceEventArgs();
            se.Name = Name;
            NameChangeEvent(this, se);
        }

        public void PublishNameChangeHandler(object sender, ServiceEventArgs se)
        {
            ServiceCallback.NameChange(se.Name);

        }
    }
}

然后以如下方式在我的应用中启动此主机:

var currentHost = new ServiceHost(typeof(PubSubService));
currentHost.Open();

到目前为止,一切看起来都很不错。然后设置我的客户端:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="Zombie.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
    </startup>
    <userSettings>
        <Zombie.Properties.Settings>
            <setting name="CurrentVersion" serializeAs="String">
                <value>0.0.0.0</value>
            </setting>
        </Zombie.Properties.Settings>
    </userSettings>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.IO.Compression" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.serviceModel>
    <bindings>
      <wsDualHttpBinding>
        <binding name="WSDualHttpBinding_IPubSubService" closeTimeout="00:01:00"
                 openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                 bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                 maxBufferPoolSize="524288" maxReceivedMessageSize="65536" clientBaseAddress="http://localhost:8081/PubSubService/Service.svc"
                 messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00" />
          <security mode="None">
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
                     algorithmSuite="Default" />
          </security>
        </binding>
      </wsDualHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8080/PubSubService/Service.svc"
                binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IPubSubService"
                contract="PubSubClient.PubSubService.IPubSubService" name="WSDualHttpBinding_IPubSubService">
        <identity>
          <servicePrincipalName value="host/chaks-pc" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

这是我启动它的方式:

    InstanceContext context = new InstanceContext(new ServiceCallback());
    PubSubServiceClient client = new PubSubServiceClient(context);

    MyEventCallbackHandler callbackHandler = CallbackHandler;
    MyEventCallbackEvent += callbackHandler;

    client.Subscribe();

我收到超时错误,我不知道问题出在哪里。我正在同一台计算机上启动这两个应用程序(客户端和服务),因此localhost地址应该不是问题。由于它们在同一台计算机上,因此我看不到如何超过1分钟的超时时间。此连接应在几秒钟内建立。有人可以帮我吗?

干杯!

0 个答案:

没有答案