【问题标题】:WCF Rest Service broken after server restart服务器重新启动后 WCF Rest 服务中断
【发布时间】:2014-03-27 10:41:26
【问题描述】:

我创建了一个 WCF Rest Service 并将它附加到我服务器上的 IIS 7 上,它运行良好。突然,我们的硬盘出现故障,本应管理备份的人没有备份任何内容,因此我们丢失了服务器上的所有内容。

所以我开始备份所有内容,并将所有内容放在 IIS 上。我们有另一个 WCF 休息服务,它做了很多我做的事情,包括几个相同的身份验证方法等。但现在看来我的行不通了。我们的网站有效,我们的其他网络服务有效,但不是这个,我不知道为什么?我将它们设置为完全相同,并且我认为我最初这样做时没有改变任何东西。

帮助参考页面有效,但调用该方法时,我得到

"The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service."

我使用与以前相同的方法,使用相同的参数调用它们,这适用于每种方法,无论 GET 还是 POST。我在 chrome 中使用了一个 REST 扩展程序来调用它,以及我通常用来调用它的移动应用程序,以及浏览器,它们都出现了相同的错误。

这是它的 web.config 文件(我尝试添加一些在 stackoverflow 上找到的东西来解决问题,但实际上它什么也没做)。

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation targetFramework="4.0" />
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

  <system.serviceModel  >
    <behaviors>
    <serviceBehaviors>
        <behavior name="DataServiceBehavior">
            <serviceMetadata httpGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
    </serviceBehaviors>
  </behaviors>
    <bindings >
      <!-- pick whichever binding you want .... -->
      <basicHttpBinding>
        <!-- binding configuration with a name -->
        <binding name="ExtendedMaxSize"
            maxBufferSize="52428800" maxReceivedMessageSize="52428800"   >
          <readerQuotas maxDepth="52428800" maxStringContentLength="52428800" 
           maxArrayLength="52428800" maxBytesPerRead="52428800" maxNameTableCharCount="52428800"  />
        </binding>
      </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"  />
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name=""  helpEnabled="true" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" crossDomainScriptAccessEnabled="true" maxReceivedMessageSize="52428800" maxBufferSize="52428800" maxBufferPoolSize="52428800"  />
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>


</configuration>

打开跟踪后,查看器出现此错误:

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System 

xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131076</EventID><Type>3</Type><SubType Name="Error">0</SubType><Level>2</Level><TimeCreated SystemTime="2014-03-27T12:15:47.5749853Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{54672f7e-5a71-4dda-92a3-f07f7c94ccb9}" /><Execution ProcessName="w3wp" ProcessID="20820" ThreadID="9" /><Channel /><Computer>OHS-VMHOST-NEW</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord Severity="Error" Channel="Operational" xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord"><TraceIdentifier>http://msdn.microsoft.com/en-GB/library/System.ServiceModel.Diagnostics.TraceHandledException.aspx</TraceIdentifier><Description>Handling an exception. Exception details: System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
   at System.Guid.GuidResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument, String failureArgumentName, Exception innerException)
   at System.Guid.TryParseGuidWithNoStyle(String guidString, GuidResult&amp; result)
   at System.Guid.TryParseGuid(String g, GuidStyles flags, GuidResult&amp; result)
   at System.Guid..ctor(String g)
   at OHS.MobileAssessor.Services.MobileAssessorAPIService.GetProjectData(String token)
   at SyncInvokeGetProjectData(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</Description><AppDomain>/LM/W3SVC/1/ROOT/mobileassessor-1-130403961472161807</AppDomain><Exception><ExceptionType>System.FormatException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).</Message><StackTrace>   at System.Guid.GuidResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument, String failureArgumentName, Exception innerException)
   at System.Guid.TryParseGuidWithNoStyle(String guidString, GuidResult&amp;amp; result)
   at System.Guid.TryParseGuid(String g, GuidStyles flags, GuidResult&amp;amp; result)
   at System.Guid..ctor(String g)
   at OHS.MobileAssessor.Services.MobileAssessorAPIService.GetProjectData(String token)
   at SyncInvokeGetProjectData(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp;amp; outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp;amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp;amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp;amp; rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><ExceptionString>System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
   at System.Guid.GuidResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument, String failureArgumentName, Exception innerException)
   at System.Guid.TryParseGuidWithNoStyle(String guidString, GuidResult&amp;amp; result)
   at System.Guid.TryParseGuid(String g, GuidStyles flags, GuidResult&amp;amp; result)
   at System.Guid..ctor(String g)
   at OHS.MobileAssessor.Services.MobileAssessorAPIService.GetProjectData(String token)
   at SyncInvokeGetProjectData(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp;amp; outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp;amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp;amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp;amp; rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</ExceptionString></Exception></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent>

根据要求,我的 GetProjectData 方法(不,它无法将任何内容写入我的事件日志):

 /// <summary>
    /// 
    /// </summary>
    /// <param name="token"></param>
    /// <returns></returns>
    [WebGet(UriTemplate = "GetProjectData/{token}", ResponseFormat = WebMessageFormat.Json)]
    public GetProjectContainerResult GetProjectData(string token)
    {
        WriteLog("Getting project data...");
        GetProjectContainerResult result = new GetProjectContainerResult();
        ProjectContainer container = new ProjectContainer();

        MobileAssessorAuthenticationResults authResult = ValidateToken(new Guid(token));

        if (authResult == MobileAssessorAuthenticationResults.Success)
        {
            container.jobs = getJobs(token);
            container.buildings = getBuildings();
            container.floors = getFloors();
            container.locations = getLocations();
            result.ReturnValue = container;
            result.Result = MobileAssessorOperationResults.Success;
        }
        else
        {
            switch (authResult)
            {
                case MobileAssessorAuthenticationResults.Error:
                    result.Result = MobileAssessorOperationResults.Error;
                    result.ResultDescription = "An error occured.";
                    result.ReturnValue = new ProjectContainer();
                    break;

                case MobileAssessorAuthenticationResults.Expired:
                    result.Result = MobileAssessorOperationResults.Failed;
                    result.ResultDescription = "The specified token has expired.";
                    result.ReturnValue = new ProjectContainer();
                    break;

                case MobileAssessorAuthenticationResults.Invalid:
                    result.Result = MobileAssessorOperationResults.Failed;
                    result.ResultDescription = "The specified token is not valid.";
                    result.ReturnValue = new ProjectContainer();
                    break;
            }
        }
        return result;
    }

【问题讨论】:

  • 检查服务器日志。
  • 我不认为它没有日志。我已经实现了一个事件日志,但只有在方法有有效请求时才会调用它。
  • “服务器在处理请求时遇到错误”。此错误必须记录在某处。检查事件日志或启用 WCF 跟踪。
  • 我启用了 WCF 跟踪,我已经得到它,但是它是 巨大的,我在哪里粘贴它?
  • 在服务跟踪查看器中打开文件并检查异常发生的位置(红线)。

标签: wcf rest post service get


【解决方案1】:

查看日志中的堆栈跟踪,我认为您在解析 MobileAssessorAPIService.GetProjectData 方法中的 GUID 时遇到问题

 at System.Guid.TryParseGuidWithNoStyle(String guidString, GuidResult&amp;amp; result)
   at System.Guid.TryParseGuid(String g, GuidStyles flags, GuidResult&amp;amp; result)
   at System.Guid..ctor(String g)
   at OHS.MobileAssessor.Services.MobileAssessorAPIService.GetProjectData(String token)

我建议您在此方法中设置一个断点并将调试器附加到您的 REST 服务。

根据您发布的代码,可疑行是

MobileAssessorAuthenticationResults authResult = ValidateToken(new Guid(token));

如果您无法远程调试它,我建议您在此行之前添加日志记录以记录令牌的值。像

logger.Log("paramater token = " + token);

或者,使用wireshark 检查令牌的值。提取该 guid 的值后,通过 http://guid.us/Test/GUID 在线验证它

【讨论】:

  • 如何将调试器附加到它?它在我的其他 WCF Rest 服务中执行完全相同的方法。我尝试了同样的论点,结果很好。
  • 看看我在stackoverflow.com/questions/15809865/…的回答还有一个屏幕截图和更多链接
  • 我的VS环境在我的电脑上,我正在通过远程桌面部署到服务器,所以我无法附加进程,可以吗?
  • 通过一些调整,您可以...参考msdn.microsoft.com/en-us/library/vstudio/… 了解如何设置远程调试
  • 但是服务和我的配置都没有根本性的问题,因为它们都在我的服务器死机之前工作,并且它具有与我的其他 REST Web 服务相同的配置和几个相同的方法,确实可以工作!
猜你喜欢
  • 2011-04-21
  • 2013-11-20
  • 1970-01-01
  • 2015-12-29
  • 1970-01-01
  • 1970-01-01
  • 2013-11-24
  • 1970-01-01
相关资源
最近更新 更多