WCF:计算器异常 [英] WCF: StackoverFlow exception

查看:210
本文介绍了WCF:计算器异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想servialize的.NET ServiceController的类时得到一个例外。它系列化很好,当它是空,但一旦我填充它,我得到一个计算器例外。

I am getting an exception when trying to servialize the .NET ServiceController class. It serializes fine when it's null but once I populate it I get a stackoverflow exception.

所以这个作品:

    [DataMember]
    public ServiceController MyServiceController
    {
        get { return null; }
    }

但是,这给出了错误未处理的异常类型的System.StackOverflowException发生在System.ServiceProcess.dll:

But this gives the error "An unhandled exception of type 'System.StackOverflowException' occurred in System.ServiceProcess.dll":

public class TestClass
{
    private ServiceController _serviceController;
    [DataMember]
    public ServiceController MyServiceController
    {
        get { return ServiceController.GetServices()[0];
    }
}

一个奇怪的是,有一个在所有日志中没有错误。如果有错误,我可以看到它在日志中,所以它不是因为我的日志不能正常工作。下面是我的配置文件

A strange thing is that there is no error in the logs at all. When there is an error I can see it in the logs so it's not because my logs aren't working. Here is my config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyCompany.Wcf.RdbmsServer.RdbmsServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="MyCompany.Wcf.RdbmsServer.RdbmsServiceBehavior"
        name="MyCompany.Wcf.RdbmsServer.RdbmsService">
        <endpoint address="" binding="wsHttpBinding" contract="MyCompany.Wcf.RdbmsServer.IRdbmsService" bindingConfiguration="IncreaseMaxMessageSize">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8731/Design_Time_Addresses/MyCompany.Wcf.RdbmsServer/RdbmsService/" />
          </baseAddresses>
        </host>
      </service>
    </services>

    <bindings>
      <wsHttpBinding>
        <binding name="IncreaseMaxMessageSize" 
            maxReceivedMessageSize="655360000">
        </binding>
      </wsHttpBinding>
    </bindings>
  </system.serviceModel>

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="All"
              propagateActivity="true">
        <listeners>
          <add name="traceListener"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData= "c:\Traces.svclog" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

</configuration>

下面是我的服务接口

[ServiceContract]
public interface IRdbmsService
{
    [OperationContract]
    TestClass GetServiceControllerList();
}

和实现是:

    public TestClass GetServiceControllerList()
    {
       return new TestClass();
    }

任何想法?

推荐答案

正在导致串行递归的:

public class TestClass
{
    private ServiceController _serviceController;
    [DataMember]
    public ServiceController MyServiceController
    {
        get { return ServiceController.GetServices()[0]; // <-- this returns your service
    }
}

这是返回的服务是您 IRdbmsService ,返回的TestClass 。这就需要序列等。

The service this is returning is your IRdbmsService, which returns TestClass. This then needs to be serialized, etc.

编辑::要澄清:

在TestClass的序列化,序列化着眼于所有的数据成员属性,其中一个是的ServiceController 。然后它序列化这个属性,而做到这一点的 GET

When TestClass is serialized, the serializer looks at all of its DataMember properties, one of which is ServiceController. It then serializes this property, which does this on the get:

return ServiceController.GetServices()[0];

由于 IRdbmsService 是在你的范围内定义的唯一服务,它在指数0从 GetServices 打电话,所以这时必须被序列化。由于返回类型 GetServiceControllerList 的TestClass 的TestClass 绝然后被序列化,这使我们回到起点(因此,递归)。

Since IRdbmsService is the only service defined in your scope, it is at index 0 from the response of the GetServices call, so it then must be serialized. Since the return type of GetServiceControllerList is TestClass, TestClass must then be serialized, which brings us back to the beginning (hence, the recursion).

至于如何去解决这一点,这取决于你正在努力做的事情。这是什么服务在做,目前正在返回有关其自身给调用者,这是没有道理给我发信息;主叫方已经拥有这些信息在消费时(进行调用之前)。你能澄清你的意图是什么?

As far as how to go about solving this, it depends on what you are trying to do. What this service is doing at the moment is returning information about itself to the caller, which doesn't make sense to me; the caller already has this information at the time of consumption (before making the call). Can you clarify your intent?

这篇关于WCF:计算器异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆