【发布时间】:2011-07-15 00:21:25
【问题描述】:
我按照本指南创建了我的自定义格式化程序,因此我可以使用 Newtonsoft Json.NET 进行对象序列化,因为内置的 Microsoft 不支持父/子关系的循环。
http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx
在他的示例中,他手动创建了他的 ServiceHost。我正在利用本指南教给我的 Routes 和 WebServiceFactory。
http://blogs.msdn.com/b/endpoint/archive/2010/01/06/introducing-wcf-webhttp-services-in-net-4.aspx
据我所知,我只需要想办法将适当的行为添加到我的服务端点。任何帮助我指出正确方向的帮助将不胜感激。
下面有一些代码sn-ps,方便参考...
在我的 Global.asax 中
WebServiceHostFactory webServiceHostFactory = new WebServiceHostFactory();
RouteTable.Routes.Add(new ServiceRoute(Accounts.Route, webServiceHostFactory, typeof(Accounts)));
如果我的 web.config
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
</webHttpEndpoint>
</standardEndpoints>
在他程序的主函数中
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(ITestService), new BasicHttpBinding(), "soap");
WebHttpBinding webBinding = new WebHttpBinding();
webBinding.ContentTypeMapper = new MyRawMapper();
host.AddServiceEndpoint(typeof(ITestService), webBinding, "json").Behaviors.Add(new NewtonsoftJsonBehavior());
【问题讨论】:
-
如果您希望支持 REST,如果您设计基于 REST 的 SOA,我绝对建议您采用新的 WCF WebApi 框架。在 wcf.codeplex.com 上找到的新位中的扩展点非常好,并且易于使用。除了 WCF 团队正在解决的一些问题之外,Preview 4 非常适合使用。对于 JSON.NET 支持,请查看我的帖子:wcf.codeplex.com/discussions/255870
标签: wcf service endpoint behavior webhttp