2011-07-15 66 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 –