wcf使用代码的通信错误

时间:2014-02-12 12:24:38

标签: c# wcf

我有以下主机配置文件:

<system.serviceModel>
  <services>
    <service name="VersionManager.Services.ClientRepository">
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8740/ClientRepository/" />
        </baseAddresses>
      </host>
      <endpoint address="" binding="wsDualHttpBinding" contract="VersionManager.Infrastructure.Interfaces.IClientRepository">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost:8740/ClientRepository/" />
        <serviceDebug includeExceptionDetailInFaults="True" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

在客户端,因为我想从文件动态设置地址,我正在使用代码 连接。 但它无法连接......

这是我正在尝试使用的代码:

Callback = new ClientRepositoryCallback();

_context = new InstanceContext(Callback);

var proxy = new DuplexChannelFactory<IClientRepository>(_context,
                                                        new WSDualHttpBinding(
                                                            "WSDualHttpBinding_IClientRepository"),
                                                        new EndpointAddress(new Uri(serverAddress)));

var channel = proxy.CreateChannel();
channel.Ping(Station);

似乎通道已成功创建,但是当我尝试在合同上使用Ping()方法时,它会因Timeout异常而失败。

{System.TimeoutException: This request operation sent to http://localhost:8740/ClientRepository/ did not receive a reply within the configured timeout (00:00:59.9830000).  The time allotted to this operation may have been a portion of a longer timeout.  This may be because the service is still processing the operation or because the service was unable to send a reply message.  Please consider increasing the operation timeout (by casting the channel/proxy to IContextChannel and setting the OperationTimeout property) and ensure that the service is able to connect to the client.

Server stack trace: 
   at System.ServiceModel.Dispatcher.DuplexChannelBinder.SyncDuplexRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Dispatcher.DuplexChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at DirectoryScanner.Infrastrcuture.ServerProxy.IClientRepository.Ping(Station station)
   at DirectoryScanner.Services.ClientRepository..ctor() in ClientRepository.cs:line 96}

客户端app.config文件如下:

<system.serviceModel>
    <bindings>
      <wsDualHttpBinding>
        <binding name="WSDualHttpBinding_IClientRepository" />
      </wsDualHttpBinding>
    </bindings>
    <client>
      <endpoint binding="wsDualHttpBinding"
          bindingConfiguration="WSDualHttpBinding_IClientRepository"
          contract="ServerProxy.IClientRepository" name="WSDualHttpBinding_IClientRepository">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>

使用客户端上自动生成的app.config可以很好地工作,但由于我想使用主机的参数,我需要使用代码......

0 个答案:

没有答案