【问题标题】:HTTP Bad Request (400) Error with a WCF Service with Multiple Endpoints具有多个端点的 WCF 服务的 HTTP 错误请求 (400) 错误
【发布时间】:2011-12-26 11:56:41
【问题描述】:

我写了一个 WCF 服务,配置如下:

<system.serviceModel>
  <services>
     <service name="SyncService" behaviorConfiguration="SyncServiceBehavior">
        <endpoint name="DataBinding" address="/DataBinding" binding="wsHttpBinding" contract="ISyncService">
              <identity>
                <dns value="localhost"/>
              </identity>
        </endpoint>
        <endpoint name="FileBinding" address="/FileBinding" binding="basicHttpBinding" bindingConfiguration="httpLargeMessageStream" contract="ISyncService">
              <identity>
                <dns value="localhost"/>
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>
        </services>
        <bindings>
          <basicHttpBinding>
            <binding name="httpLargeMessageStream" maxReceivedMessageSize="2147483647" transferMode="Streamed" messageEncoding="Mtom" />
          </basicHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior name="SyncServiceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
              <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>

在客户端,我正在构建代理以动态使用服务:

private static RemoteFileProviderProxy CreateDynamicFileProxy(string endPointAddress)
        {
            //endPointAddress = http://localhost/SyncService.svc/FileBinding
            BasicHttpBinding binding = new BasicHttpBinding();

            binding.Name = "httpLargeMessageStream";
            binding.TransferMode = TransferMode.Streamed;
            binding.MessageEncoding = WSMessageEncoding.Mtom;
            binding.MaxReceivedMessageSize = int.MaxValue;

            ServiceEndpoint endPoint = new ServiceEndpoint(ContractDescription.GetContract (typeof(ISyncService)), binding, new EndpointAddress(endPointAddress));

            endPoint.Name = "FileBinding";

            ChannelFactory<ISyncService> channelFactory = new ChannelFactory<ISyncService>(endPoint);

            return new RemoteFileProviderProxy((ISyncService)channelFactory.CreateChannel());
        }

最初,我只有 DataBinding 端点,位于默认地址(不是它当前使用的 DataBinding 地址),我使用相同的客户端代码来使用该服务端点并且一切正常。当我添加 FileBinding 端点并将 DataBinding 端点移动到其新位置 http://localhost/SyncService.svc/DataBinding 时,我不再能够连接到任一端点,而是在第一次使用它时收到“错误请求 (400)”。

查看服务跟踪查看器,我看到以下有关引发 System.Xml.XmlException 的附加信息:

从网络接收的 XML 存在问题。有关详细信息,请参阅内部异常。

内部异常:

无法读取邮件正文,因为它是空的。

堆栈跟踪:

at System.ServiceModel.Channels.HttpRequestContext.CreateMessage()
at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, Action callback)
at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

当我在浏览器中查看 SyncService.svc 时,我看到“您已创建服务”页面并且一切看起来都正确。 wsdl 看起来也很合适。当我转到 http://localhost/SyncService.svc/FileBinding 时,会加载一个空白页面(与转到 http://localhost/SyncService.svc/XXX 之类的内容时的错误页面相反)。

我已经解决了许多类似的问题,但无济于事,包括:

HTTP Bad Request error when requesting a WCF service contract

Large WCF web service request failing with (400) HTTP Bad Request

有人对如何解决这个问题有任何想法或建议吗?

【问题讨论】:

  • 有趣的更新,即使在抛出错误请求时正在使用 FileBinding 的代理对象,服务跟踪查看器也会显示在 DataBinding 端点侦听的错误。通过翻转端点出现在服务器配置文件中的顺序,我可以在 FileBinding 端点监听时抛出相同的错误。
  • 从配置中的地址属性中删除斜线有什么影响吗?
  • @degorolls,没有。我已经尝试了两种方法并得到完全相同的结果。

标签: c# wcf


【解决方案1】:

事实证明,这个问题不是由合同对象引起的,而是由未找到它所引用的对象引起的。就我而言,我忘记在服务器上安装所有必需的 Microsoft Sync Framework 部分。

我想这个故事的寓意是这个错误非常具有误导性,并且也适用于所有子对象!

【讨论】:

    【解决方案2】:

    您能否尝试使用 Fiddler 等工具检查发送到服务器的请求。肥皂信封中一定有什么东西导致了这个问题。

    还可以先尝试发送小数据以确保您的服务可以访问,然后再尝试发送更大的数据,以便缩小问题所在。

    您是否实现了异步模式或者您的 Web 服务是否标记为 Async=True。

    如果您也可以发布您的服务将会很有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-27
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 2014-01-11
      相关资源
      最近更新 更多