【问题标题】:WCF WebHttp, Service, Behaviors, Endpoints, Custom FormatterWCF WebHttp、服务、行为、端点、自定义格式化程序
【发布时间】: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


【解决方案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());
            }
        }
    }
}

【讨论】:

  • 太棒了,我今天晚些时候试一试!感谢卡洛斯的反馈!
  • 是否可以在配置文件中定义属性而不是编写代码.....我注意到 wsBasicHttpEndpoint 的可配置属性但 webHttpEndpoint 没有
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
  • 2010-10-22
相关资源
最近更新 更多