2012-04-03 86 views
1

我想在我的C#应用​​程序中实现REST服务以与Android客户端进行通信。我与C#部分斗争。REST服务返回对象异常终止

我跟着这篇文章:http://msdn.microsoft.com/en-us/library/bb412178

这是我跑我的服务:

WebServiceHost host = new WebServiceHost(typeof(RestService), new Uri("http://localhost:8000/")); 
ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IRestService), new WebHttpBinding(), ""); 
host.Open(); 

非常喜欢的文章上面我挂有IRestService接口和RestService与方法EchoWithGet(字符串s)EchoWithPost我不使用,我不会再谈论它。我想返回UserClientInfo类,这是我将在稍后描述的问题的原因。

[ServiceContract] 
interface IRestService 
{ 
    [OperationContract] 
    [WebGet] 
    UserClientInfo EchoWithGet(string s); 
} 

这里是EchoWithGet(...)的实现。它连接到另一个服务并调用UserLog,该用户日志应该返回UserClientInfo

public UserClientInfo EchoWithGet(string s) 
{ 
    service = (IService)Activator.GetObject(typeof(IService), "tcp://127.0.0.1:7878/" + "Service"); 
    service.UserLog("username", "password", out uci); 
    return uci; 
} 

UserClientInfo是相当复杂的对象。我不确定我是否可以通过服务返还。它是包含另一个结构的结构,其中包含另一个结构。

[Serializable] 
public struct UserClientInfo 
{ 
    public ulong ClientId; 
    public DatabaseManager DBManager; 
    public string DisplayedName; 
    public ChISDispositions Dispositions; 
    public RegistrationInfo RegistrationInfo; 
    public uint Right1; 
    public uint Right2; 
    public uint Right3; 
    public uint Right4; 
    public string SessionId; 
    public string UserId; 
} 

当我跑我的服务,并指出我的浏览器的http://本地主机:8000/EchoWithGet S =的TestString我得到连接所中断的错误。 Firebug显示状态在网络选项卡上中止。服务以某种方式崩溃。我可以调试它,并看到UserLog()重新调用uci对象,服务返回它,但在浏览器和服务之间的某处失败。

我试图返回完全地新的对象:

UserClientInfo uci = new UserClientInfo(); 

这一工程,并与普通的对象返回的XML。


有什么我做错了吗?

我应该知道的服务有一些限制吗?

是否有任何日志,我可以看到什么是错的?

感谢您的任何答案。

+0

检查以确保您的UserClientInfo数据结构中没有循环依赖项。如果你这样做,它会在序列化过程中窒息。这可能是为什么一个新的UserClientInfo会通过但实际数据填充一个。 – 2012-04-03 19:09:19

+1

您的问题很可能与以下三种类型之一有关:DatabaseManager,ChISDispositions,RegistrationInfo。 DatabaseManager是一个类吗?如果是这样,你有没有尝试将其设置为空? – JamieSee 2012-04-03 22:07:01

+0

正如你所说 - 问题在ChiSDispositions。如果我将它设置为空,我会得到输出结果。 ChiSDispositions中有一些方法和常量。我还发现有引用类型的枚举。这可能是原因吗?然而,我在那里发现的一切都是可序列化 – 2012-04-05 12:00:09

回答

0

问题出在复杂的复杂对象的某些部分。解决方案是将其转换为DTO。

0

是否有任何日志,我可以看到什么是错的?

您可以在您的web.config文件中添加WCF logging。然后您可以使用S ervice Trace Viewer Tool来读取/监视它。

<system.diagnostics> 
<trace autoflush="true" /> 
<sources> 
     <source name="System.ServiceModel" 
       switchValue="Information, ActivityTracing" 
       propagateActivity="true"> 
     <listeners> 
      <add name="sdt" 
       type="System.Diagnostics.XmlWriterTraceListener" 
       initializeData= "SdrConfigExample.e2e" /> 
     </listeners> 
    </source> 
</sources> 
</system.diagnostics>