2

我们正在探索从 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 网络服务比 REST 网络服务更快地返回响应。

我不确定我们的场景是否不太适合 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/soap3+xml: 2,651, 〜标题〜: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。我已经多次重复这个测试并得到了类似的发现。

有人遇到同样的发现吗?

4

1 回答 1

1

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

于 2013-07-09T20:29:24.103 回答