WCF服务 - 无法调用服务

时间:2015-03-16 19:41:11

标签: c# wcf soap

我的WCF服务出现问题,方法本身会收集正确的数据并返回正确的数据类型但是无论我使用哪种绑定类型的端点,我总会得到相同的错误。我尝试过TCP,HTTP和NamedPipe。顶部的一般错误如下:

  

无法调用该服务。可能的原因:服务处于脱机状态   或无法进入;客户端配置与   代理;现有代理无效。请参阅堆栈跟踪   更多详情。您可以尝试通过启动新代理进行恢复,然后进行恢复   默认配置或刷新服务。

发布的内容来自HTTP绑定。

 An error occurred while receiving the HTTP response to http://localhost:8732/ClubService/. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

Server stack trace: 
   at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ClientReliableChannelBinder`1.RequestClientReliableChannelBinder`1.OnRequest(TRequestChannel channel, Message message, TimeSpan timeout, MaskingMode maskingMode)
   at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout, MaskingMode maskingMode)
   at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Security.SecuritySessionClientSettings`1.SecurityRequestSessionChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.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.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 IAndereBoekenService.BoekBeschikbaar(String isbn)
   at AndereBoekenServiceClient.BoekBeschikbaar(String isbn)

Inner Exception:
The underlying connection was closed: An unexpected error occurred on a receive.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

Inner Exception:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)

Inner Exception:
An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

的App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.serviceModel>
    <services>
      <service name="ClubServiceLibrary.ClubService">
        <host>
          <baseAddresses>
            <add baseAddress="net.pipe://localhost/ClubService" />
            <add baseAddress ="net.tcp://localhost/ClubService" />
            <add baseAddress="http://localhost:8732/ClubService/" />
          </baseAddresses>
        </host>
        <endpoint name ="namedPipeClub"
                  binding="netNamedPipeBinding"
                  contract="ClubServiceLibrary.IClubServiceLibrary" />
        <endpoint name="namedPipeClubMEX"
                  address="mex"
                  binding="mexNamedPipeBinding"
                  contract="IMetadataExchange" />
        <endpoint name ="tcpClub"
                  binding="netTcpBinding"
                  contract="ClubServiceLibrary.IClubServiceLibrary" />
        <endpoint name="tcpClubMEX"
                  address="mex"
                  binding="mexTcpBinding"
                  contract="IMetadataExchange" />
        <endpoint binding="wsHttpBinding"
                  name="httpClub"
                  contract="ClubServiceLibrary.IClubServiceLibrary" />
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  name="httpClubMEX"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

我从一个字典中获取数据,该字典在另一个名为“DummyData”的类中填充了虚拟数据,这确实充满了数据。

 private static Dictionary<int, Club> clubsValue;

 public static Dictionary<int, Club> Clubs
        {
            get { return clubsValue; }
        }

在我的 ClubService课程中,我执行以下操作:

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class ClubService : IClubService
    {
        public List<Club> GetClubByName(string search)
        {
            var searchToLower = search.ToLower();

            List<Club> test = DummyData.Clubs.Values.Where(b => b.Titel.ToLower().Contains(searchToLower)).ToList();

            return test;
        }
    }

我的 IClubInterface 如下所示:

[ServiceContract(Namespace = "http://club.com/clubservice")]
interface IClubService
{
    [OperationContract]
    List<Club> GetClubByName(string search);
}

因此,当调试列表中的所有数据并返回正确的列表时..但每次我收到此错误消息,我不知道如何解决此问题。

1 个答案:

答案 0 :(得分:3)

如果我不得不冒险猜测,我会说这会在返回时序列化或反序列化数据时抛出异常。追踪这个&amp;的最简单方法大多数其他WCF错误是启用WCF日志文件并查看它使用WCF日志文件查看器生成的内容:

将以下部分添加到应用程序配置文件中,然后确保它正在写入的文件夹跟踪存在(下例中的c:\ temp \ WCFLogs)。 您应该开始看到一个ServerWCFTraces.svclog文件被创建 - 它可能会说长度为0个字节,直到您停止服务或它决定刷新到磁盘。

重现失败案例后,在打开Visual Studio的计算机上打开日志文件(或从此处下载查看器:SvcTraceViewer from MSDN。)

有一些说明可以在MSDN链接上使用该工具,但实际上您可以在活动窗格(左侧)中查找错误(以红色突出显示),然后查看失败的操作(右上方窗格)和详细信息部分(右下方窗格),将会抛出正在抛出的内部异常的描述,它几乎总是告诉您抛出的异常是什么,并为您提供有关如何解决它的更多信息。

<system.diagnostics>
  <sources>
     <source propagateActivity="true" name="System.ServiceModel" switchValue="Information, ActivityTracing">
        <listeners>
           <add name="ServiceModelMessageLoggingListener">
              <filter type="" />
           </add>
        </listeners>
     </source>
     <source name="System.ServiceModel.MessageLogging" switchValue="Warning,ActivityTracing">
        <listeners>
           <add name="ServiceModelMessageLoggingListener">
              <filter type="" />
           </add>
        </listeners>
     </source>
  </sources>
  <sharedListeners>
     <add initializeData="C:\temp\WCFLogs\ServerWCFTraces.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
        <filter type="" />
     </add>
  </sharedListeners>
</system.diagnostics>