2011-07-15 79 views
2

我遵循本指南創建自定義格式程序,因此我可以使用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()); 
+0

如果您希望支持REST,那麼我肯定會建議您在設計基於REST的SOA時採用新的WCF WebApi框架。在wcf.codeplex.com上找到的新位的擴展點非常好,易於使用。除了WCF團隊正在努力解決的一些問題之外,預覽版4還是非常出色的。對於JSON.NET支持查看我的帖子在這裏:http://wcf.codeplex.com/discussions/255870 – Daniel

回答

1

要使用的路線和到達服務端點引用的主要功能,需要您自定義的服務主機工廠。它可以像您當前使用的WebServiceHostFactory一樣(只是返回對WebServiceHost的引用),而是返回對定製服務主機的引用。

你可以在http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/14/wcf-extensibility-servicehostfactory.aspx找到更多關於服務主機工廠的信息。

public class MyServiceHostFactory : ServiceHostFactory 
{ 
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) 
    { 
     return base.CreateServiceHost(serviceType, baseAddresses); 
    } 

    class MyServiceHost : WebServiceHost 
    { 
     public MyServiceHost(Type serviceType, Uri[] baseAddresses) 
      : base(serviceType, baseAddresses) 
     { } 

     protected override void OnOpening() 
     { 
      base.OnOpening(); 

      foreach (ServiceEndpoint endpoint in this.Description.Endpoints) 
      { 
       CustomBinding custom = endpoint.Binding as CustomBinding; 
       if (custom != null) 
       { 
        custom = new CustomBinding(endpoint.Binding); 
       } 

       custom.Elements.Find<WebMessageEncodingBindingElement>().ContentTypeMapper = new MyRawMapper(); 
       endpoint.Binding = custom; 

       endpoint.Behaviors.Add(new NewtonsoftJsonBehavior()); 
      } 
     } 
    } 
} 
+0

真棒我會試試今天晚些時候!感謝Carlos的反饋! – Altonymous

+0

是否有可能在配置文件中定義屬性而不是編寫代碼.....我注意到了wsBasicHttpEndpoint的可配置屬性,但不是用於webHttpEndpoint –