WCF休息与SOAP Web服务之间存在不明原因的性能差异

时间:2013-05-16 23:01:56

标签: wcf web-services rest soap wcf-rest

我们正在探索从SOAP Web服务切换到REST是否值得。我创建了一个REST Web服务,其中包含以下信息:

 [ServiceContract]
 public interface IRestServiceImpl
 {
    [OperationContract]
    [WebInvoke(Method = "PUT", ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "Execute")]
    ExecuteResponse Execute(ExecuteRequest request);

    [OperationContract]
    [WebInvoke(Method = "PUT", ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "ExecutePutJSON")]
    ExecuteResponse ExecutePutJSON(ExecuteRequest request);
}

背后的实现代码(RestServiceImpl.svc.cs)如下:

public class RestServiceImpl : IRestServiceImpl
{
    public ExecuteResponse Execute(ExecuteRequest request)
    {
       //processing code that returns ExecuteResponse
    }

    public ExecuteResponse Execute(ExecuteRequest request)
    {
       //processing code that returns ExecuteResponse
    }
}

RestServiceImpl.svc如下:

<%@ ServiceHost Language="C#" Debug="true" Service="CICJIS.IWS.RestServiceImpl" 
CodeBehind="RestServiceImpl.svc.cs" %>

Web.config:

<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
      switchValue="Information, ActivityTracing">
        <listeners>
          <add name="messages" />
        </listeners>
    </source>
  <source name="System.ServiceModel.MessageLogging">
    <listeners>
      <add name="messages" />
    </listeners>
  </source>
 </sources>
 <sharedListeners>
  <add name="messages"
     type="System.Diagnostics.XmlWriterTraceListener"
     initializeData="C:\Logs\RestService.svclog" />
 </sharedListeners>
 <trace autoflush="true" />
 </system.diagnostics>
<system.web>
    <compilation debug="true" defaultLanguage="c#" targetFramework="4.0" />
    <httpRuntime maxRequestLength="999999" maxQueryStringLength="999999"
    executionTimeout="999"/>
</system.web>
<system.serviceModel>
   <diagnostics>
      <messageLogging
        logEntireMessage="true"
        logMalformedMessages="true"
        logMessagesAtServiceLevel="true"
        logMessagesAtTransportLevel="true"
        maxMessagesToLog="3000"
        maxSizeOfMessageToLog="10000000" />
    </diagnostics>

   <services>
      <service name="RestServiceImpl" behaviorConfiguration="ServiceBehavior">
        <endpoint address="" binding="webHttpBinding" contract="IRestServiceImpl" 
          behaviorConfiguration="web">

        </endpoint>
      </service>
    </services>

    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

后面的实现代码与SOAP webservice的作用相同,并返回相同的ExecuteResponse对象。 我通过fiddler使用这两个服务来获取有关获取响应所需时间的统计信息。 SOAP Web服务使用ws-security,但REST Web服务没有实现任何安全性。 我发现SOAP Web服务返回响应的速度比REST Web服务快得多。

我不确定我们所拥有的场景是否不适合REST服务,或者我是否错误地实现了REST Web服务? 我也尝试调试REST和SOAP服务,发现当它在方法中断时,两种方法中的处理代码以相同的速率完成,但响应返回REST客户端需要的时间比SOAP长客户。在序列化对象时,REST的WCF api是否可能比SOAP的WCF api慢?

以下是来自fiddler的统计数据示例:

对于SOAP: 请求数:1 发送的字节数:13,693(标题:299;正文:13,394) 收到的字节数:2,651,288(标题:235;正文:2,651,053) ClientConnected:16:12:39.775 ClientBeginRequest:16:12:39.775 GotRequestHeaders:16:12:39.775 ClientDoneRequest:16:12:40.120 确定网关:0ms DNS查找:0ms TCP / IP连接:1ms HTTPS握手:0ms ServerConnected:16:12:40.122 FiddlerBeginRequest:16:12:40.122 ServerGotRequest:16:12:40.122 ServerBeginResponse:16:12:40.123 GotResponseHeaders:16:13:25.744 ServerDoneResponse:16:13:26.863 ClientBeginResponse:16:13:25.744 ClientDoneResponse:16:13:26.863 总体时间:00:00:47.0877083 application / soap + xml:2,651,053 〜头〜:235

对于REST: 请求数:1 发送的字节数:2,369(标题:298;正文:2,071) 收到的字节数:1,982,735(标题:230;正文:1,982,505) ClientConnected:16:12:07.728 ClientBeginRequest:16:12:32.427 GotRequestHeaders:16:12:32.427 ClientDoneRequest:16:12:32.428 确定网关:0ms DNS查找:0ms TCP / IP连接:2ms HTTPS握手:0ms ServerConnected:16:12:32.430 FiddlerBeginRequest:16:12:32.430 ServerGotRequest:16:12:32.431 ServerBeginResponse:16:12:32.435 GotResponseHeaders:16:20:06.914 ServerDoneResponse:16:20:07.889 ClientBeginResponse:16:20:06.914 ClientDoneResponse:16:20:07.889 整体经历: 00:07:35.4626091 application / xml:1,982,505       〜头〜:230

这两项服务之间的最大区别在于ServerBeginResponse到GotResponseHeaders。 我已经多次重复这个测试并得到了类似的结果。

任何人都有同样的发现吗?

1 个答案:

答案 0 :(得分:1)

它可能取决于用于实现REST服务的框架。 我最近在基于JAXWS RI(Metro)的应用程序上工作,后来将其移植到JAX RS(Jersey)框架中。 对于基于Rest的实现,我可以为每个服务调用获得更好的响应时间