【问题标题】:WCF Error - Object reference not set to an instance of an object. Production service not workingWCF 错误 - 对象引用未设置为对象的实例。生产服务不工作
【发布时间】:2013-10-16 15:21:47
【问题描述】:

我是写问题的新手,老读者。如果我遗漏了什么,请告诉我。

我查看了许多不同的方案和可能的修复,但无法让我的 WCF 服务正常工作。

我的服务负责将来自许多不同集合的数据传递到主存储库。客户端在本地集合级别收集数据并将其传递给插入数据并传回结果消息的服务。再次在测试环境中,这正常工作。

在生产中,我将服务添加到异地服务器并在远程集上配置客户端。客户端能够配置和接收来自服务的更新。到目前为止,一切正常。但是,一旦客户端尝试通过它传输数据,就会收到以下错误“对象引用未设置为对象的实例。”

通过错误检查,我确认数据库没有连接字符串问题。我在我的测试环境中再次运行相同的数据传输,没有任何问题。我能够连接到本地集上的 .svc url。

我已通过数据协定方法调用在不同点添加了日志记录,但这些日志均未触发任何结果。我还在测试应用程序上测试了写入功能,确认写入临时文件夹的凭据没有问题。例如:

public Result InsertVenueRecord(Venue v)
{
        System.IO.File.WriteAllText(@"C:\temp\MadeItToVenue" + DateTime.Now.Ticks.ToString() + ".log.txt", "insertVenue()\r\n\r\n");
        int oldId = v.VenueId;
        try
        {
            System.IO.File.WriteAllText(@"C:\temp\MadeItToVenue" + DateTime.Now.Ticks.ToString() + ".log.txt", "insertVenue()\r\n\r\n");
            //Check Address 
            if (v.Address.AddressId != 0)

客户端 app.Config 如下所示:

    <configuration>
    <system.serviceModel>
      <bindings>
        <basicHttpBinding>
          <binding name="BasicHttpBinding_placeholder" />
        </basicHttpBinding>
      </bindings>  
      <client>
        <endpoint address="Removed" binding="basicHttpBinding"
          bindingConfiguration="BasicHttpBinding_placeholder" contract="placeholder.placeholder"
          name="BasicHttpBinding_placeholder" />
      </client>
    </system.serviceModel>
  <appSettings>
    <add key="DTFirstWave" value="Venue|Event|EventSurveyLocation|Agent|LoginHistory|LoginUsed"/>
  </appSettings>
  <connectionStrings>
    <!-- Removed Connection Strings -->

  </connectionStrings>
</configuration> 

服务 webconfig 如下所示:

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
        <httpRuntime/>
    </system.web>
    <system.serviceModel>
        <bindings>
             <basicHttpBinding>
                  <binding allowCookies="true" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
      <readerQuotas maxDepth="32" maxStringContentLength="52428800" maxArrayLength="52428800" maxBytesPerRead="52428800" maxNameTableCharCount="52428800"/>
    </binding>
  </basicHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>

 <directoryBrowse enabled="true"/>
 </system.webServer>
 <connectionStrings>
 <!--Removed -->
 </connectionStrings>
</configuration>

我不知道为什么这不起作用。

【问题讨论】:

  • 尝试更改服务器配置:&lt;serviceDebug includeExceptionDetailInFaults="true"/&gt; 然后任何服务器端异常都应发送到客户端
  • 我最初将其标记为 true。由于此错误,我将其更改为 false:该服务无法激活,因为它不支持 ASP.NET 兼容性。为此应用程序启用了 ASP.NET 兼容性。在 web.config 中关闭 ASP.NET 兼容模式,或将 AspNetCompatibilityRequirements 属性添加到服务类型,RequirementsMode 设置为“Allowed”或“Required”。
  • 我将尝试解决该问题以获取有关异常的更多详细信息。谢谢!
  • 我添加了 true 标志。没有给出新的细节。仍然在同一条船上。
  • NullReferenceException 的几乎所有情况都是相同的。请参阅“What is a NullReferenceException in .NET?”获取一些提示。这可能是一个简单的服务编程错误,与配置无关。

标签: c# wcf nullreferenceexception


【解决方案1】:

请通过在您的服务配置中添加 system.daignostics 来启用您的服务 SVC 日志。它会在生产中为您提供适当的错误。

它将在您的服务目录中创建“App_tracelog.svclog”文件。

<system.diagnostics>
<sources>
  <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
    <listeners>
      <add name="ServiceModelTraceListener" />
    </listeners>
  </source>
  <source name="System.ServiceModel" switchValue="Verbose,ActivityTracing">
    <listeners>
      <add name="ServiceModelTraceListener" />
    </listeners>
  </source>
  <source name="System.Runtime.Serialization" switchValue="Verbose,ActivityTracing">
    <listeners>
      <add name="ServiceModelTraceListener" />
    </listeners>
  </source>
</sources>
<sharedListeners>
  <add initializeData="App_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelTraceListener" traceOutputOptions="Timestamp" />
</sharedListeners>

【讨论】:

  • 您还可以使用 Windows SDK 工具中的服务配置编辑器和服务跟踪查看器。它使配置和查看服务跟踪变得容易(这些工具作为 Visual Studio iirc 的一部分安装)
  • System.ServiceModel.ProtocolException:从网络接收的 XML 存在问题。有关更多详细信息,请参阅内部异常。 ---> System.Xml.XmlException:无法读取消息正文,因为它是空的。 --- 内部异常堆栈跟踪结束 ---
  • 我猜这是一个序列化问题?我已经验证从客户端发送到服务器的对象不为空。
  • 我不认为这是序列化问题,因为 SVC 日志应该为您提供适当的序列化问题消息。在您的服务方法参数中是否有任何没有“0”值的枚举?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 2012-08-31
  • 2011-11-21
  • 2012-10-01
  • 1970-01-01
相关资源
最近更新 更多