我遵循本指南創建自定義格式程序,因此我可以使用Newtonsoft Json.NET進行對象序列化,因爲內置的Microsoft不支持來自父/子的循環關係。WCF WebHttp,服務,行爲,端點,自定義Formater
http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx
在他的榜樣,他的手工創建他的ServiceHost。我利用路線和本指南向我教授的WebServiceFactory。
http://blogs.msdn.com/b/endpoint/archive/2010/01/06/introducing-wcf-webhttp-services-in-net-4.aspx
從我可以告訴我只需要想出一個辦法來適當的行爲添加到我的服務端點。任何幫助指引我在正確的方向將不勝感激。
以下爲便於參考一些代碼片段...
在我的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團隊正在努力解決的一些問題之外,預覽版4還是非常出色的。對於JSON.NET支持查看我的帖子在這裏:http://wcf.codeplex.com/discussions/255870 – Daniel