spring.net与wcf返回List或者类出错

truecoffeefox 2011-05-31 01:59:18
环境是win7 x64+vs2010用的spring.net+nhibernate
大体架构如图

契约和实现在service层,web层通过spring.net管理的服务,通过jquery调用
现在问题是这样:
服务实现PrivateCmsService.cs的方法,返回bool,string这样的简单类型页面可以得到正确的返回值
如果返回值是类则不执行,直接500 System.ServiceModel.ServiceActivationException,如果返回list<T>通过断点发现执行正常,但是执行完毕没有返回值,firebug看状态是Aborted
实在不知道该咋整,网上也没搜到相关的例子,但是不用spring.net,单纯的wcf服务可以返回List《t》啊
web.config配置如下
<system.serviceModel>
<services>
<service name="PrivateCmsService" behaviorConfiguration="EnableMetadataBehaviors">
<endpoint address="" binding="webHttpBinding" contract="CMS.Service.Contract.IPrivateCmsServiceContract" behaviorConfiguration="PrivateCmsServiceBehavior"/>
<!--bindingConfiguration="HttpJsonpBinding"-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="PrivateCmsServiceBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="EnableMetadataBehaviors">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<!--<bindings>
<webHttpBinding>
-->
<!--crossDomainScriptAccessEnabled make the WCF 4 service support the JSONP-->
<!--
<binding name="HttpJsonpBinding" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>-->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
...全文
322 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
truecoffeefox 2011-06-02
  • 打赏
  • 举报
回复
感觉快成gfw了
truecoffeefox 2011-06-02
  • 打赏
  • 举报
回复
newskind用了Serializable标记,也不能说是复杂了,只是个联合主键,有几个ilst的成员

现在用getnewsKind的话,firefug看还是aborted

POST http://www.mycms.com:85/PrivateCmsService.svc/GetNewsKindByApp HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: text/json;
X-Requested-With: XMLHttpRequest
Referer: http://www.mycms.com:85/ccenter/opmanager.aspx
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: www.mycms.com:85
Content-Length: 17
Connection: Keep-Alive
Pragma: no-cache
Cookie: ASP.NET_SessionId=w22dj54gi1gz4hdd30qlvna0

{"appIndex":"28"}


HTTP/1.1 504 Fiddler - Receive Failure
Content-Type: text/html
Connection: close
Timestamp: 10:39:19.853

ReadResponse() failed: The server did not return a response for this request.
机器人 2011-06-02
  • 打赏
  • 举报
回复
看你的类型也还不像 EntityObject 那么复杂,而且就算使用复杂类型只要是可序列化的。
Xml格式的都不会有问题。比如:EntityObject只是序列化成json会失败。

你可以先试试你现在的类(应该没事,因为之前我也没想到是Factory的事,所以让你试试POCO的)
。。。如果不行,那么就需要改写这个实体类了。
truecoffeefox 2011-06-02
  • 打赏
  • 举报
回复
再次感谢一下JustACoder
truecoffeefox 2011-06-02
  • 打赏
  • 举报
回复
哇哈哈哈哈哈哈哈哈,果然如此
我咋就没想到这个
还是基础不好,汗~~~~~~
rart2008 2011-06-02
  • 打赏
  • 举报
回复
帮顶......
机器人 2011-06-02
  • 打赏
  • 举报
回复
hoho问题在这哦,改下、这个和spring没关系。。。

你要想让WCF支持REST,并指定是json/xml序列化response,需要配合WebHttpBinding和WebServiceHostFactory哦
truecoffeefox 2011-06-02
  • 打赏
  • 举报
回复
<%@ ServiceHost Language="C#" Debug="true" Service="PrivateCmsService" Factory="Spring.ServiceModel.Activation.ServiceHostFactory" %>

用的spring管理的wcf
机器人 2011-06-02
  • 打赏
  • 举报
回复
对了,你的.svc文件的markup是用的WebServiceHostFactory么?
<%@ ServiceHost Language="C#" Debug="true" Service="WcfRESTfulSvc1.TaskService" CodeBehind="TaskService.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>
truecoffeefox 2011-06-02
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 fangxinggood 的回复:]

POCO就是特纯洁的CLR类型。只有简单类型的get,set属性。

[/Quote]
昨天下乡六一去了,哈哈,今天继续


项目里有个简单的类OpSecurity只有get和set方法,我把equals和gethashcode等方法都去掉了重试也还是500错误
truecoffeefox 2011-06-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fangxinggood 的回复:]

500 看样子是序列化出错了。

IPrivateCmsServiceContract 贴出来看看?
[/Quote]

不过ServiceActivationException,msdn解释说是此异常派生自 CommunicationException,它表示在终结点间进行通信时可能引发的一类可恢复的错误,并且需要可靠的 Windows Communication Foundation (WCF) 客户端和服务应用程序来进行处理。若要防止更一般的 CommunicationException 处理程序捕获更具体的 ActionNotSupportedException,请在处理 CommunicationException 之前捕获此异常。

不过我都不知道在那去catch,调试也没见蹦出异常来
houyanjun 2011-05-31
  • 打赏
  • 举报
回复
[关注]
机器人 2011-05-31
  • 打赏
  • 举报
回复
POCO就是特纯洁的CLR类型。只有简单类型的get,set属性。

这样你先做个简单的类,返回下,以排除这个问题:
public class POCO_NewsKind
{
public virtual string KindName {get;set;}
public virtual string StatusCode {get;set;}
}


另外,参考:http://blog.csdn.net/fangxinggood/archive/2011/05/28/6452549.aspx
以前我直接返回 EntityObject 的时候,会返回 504 的错误,估计类似。
truecoffeefox 2011-05-31
  • 打赏
  • 举报
回复
下班了,明天再来
先感谢一下子夜和JustACoder
truecoffeefox 2011-05-31
  • 打赏
  • 举报
回复
自言自语一下
并没看到我的需求说明书,不知道我这边开发团队的情况,不了解这个项目的意图却能妄下结论
我有必要和这样的人对话?
truecoffeefox 2011-05-31
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 fangxinggood 的回复:]

哦?REST WCF 的。

没有定义uriTemplate?

[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "GetNewsK……
[/Quote]
还是500 System.ServiceModel.ServiceActivationException

poco类是啥意思
using System;
using Iesi.Collections.Generic;

namespace CMS.Domain
{
[Serializable]
public partial class NewsKind : StatusChange
{
public NewsKind()
{
News = new HashedSet<News>();
}
public virtual NewsKindId NewsKindId
{
get;
set;
}
public virtual string KindName
{
get;
set;
}
public virtual string StatusCode
{
get;
set;
}
public virtual App App
{
get;
set;
}
public virtual ISet<News> News
{
get;
set;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj))
return true;

return Equals(obj as NewsKind);
}

public virtual bool Equals(NewsKind obj)
{
if (obj == null) return false;

if (Equals(NewsKindId, obj.NewsKindId) == false)
return false;

if (Equals(KindName, obj.KindName) == false)
return false;

if (Equals(StatusCode, obj.StatusCode) == false)
return false;

return true;
}

public override int GetHashCode()
{
int result = 1;
result = (result * 397) ^ (NewsKindId != null ? NewsKindId.GetHashCode() : 0);
result = (result * 397) ^ (KindName != null ? KindName.GetHashCode() : 0);
result = (result * 397) ^ (StatusCode != null ? StatusCode.GetHashCode() : 0);
return result;
}
}
}
jhdxhj 2011-05-31
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 truecoffeefox 的回复:]
引用 4 楼 jhdxhj 的回复:

就是一堆垃圾


像你这种吃货用什么都已一堆垃圾
[/Quote]
当你这样用的时候你知道这样用究竟能有什么好处吗?能解决什么问题吗?否则就是一堆垃圾
机器人 2011-05-31
  • 打赏
  • 举报
回复
哦对了,NewsKind 是POCO对象吗?还是EntityObject。
机器人 2011-05-31
  • 打赏
  • 举报
回复
哦?REST WCF 的。

没有定义uriTemplate?

[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "GetNewsKindByApp/{appindex}")]
List<NewsKind> GetNewsKindByApp(string appIndex);


truecoffeefox 2011-05-31
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fangxinggood 的回复:]

500 看样子是序列化出错了。

IPrivateCmsServiceContract 贴出来看看?
[/Quote]

接口定义
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string[] GetNewsKindByApp(int appIndex);//IList<NewsKind>

实现类的声明
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
// [JavascriptCallbackBehavior(UrlParameterName = "jsoncallback")]
public class PrivateCmsService : IPrivateCmsServiceContract
加载更多回复(9)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧