WCF WebHttp,服务,行为,端点,自定义格式化程序

时间:2011-07-15 00:21:25

标签: wcf service endpoint behavior webhttp

我按照本指南创建自定义格式化程序,因此我可以使用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

据我所知,我只需找出一种方法将适当的行为添加到我的服务端点。任何指导我指向正确方向的帮助都将不胜感激。

下面的一些代码段以便于参考......


在我的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());

1 个答案:

答案 0 :(得分: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());
            }
        }
    }
}