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

发布于 2024-11-24 03:38:33 字数 2098 浏览 1 评论 0原文

我按照本指南创建自定义格式化程序,这样我就可以使用 Newtonsoft Json.NET 进行对象序列化,因为内置的 Microsoft 格式化程序不支持父/子关系的循环。

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>

在他的程序的 Main 函数中

        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());

I followed this guide for creating my custom formatter so I could use the Newtonsoft Json.NET for object serialization since the built in Microsoft one doesn't support cycles from parent/child relationships.

http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx

In his example he's manually creating his ServiceHost. I am leveraging Routes and the WebServiceFactory taught to me by this guide.

http://blogs.msdn.com/b/endpoint/archive/2010/01/06/introducing-wcf-webhttp-services-in-net-4.aspx

From what I can tell I just need to figure out a way to add the appropriate Behaviour to my Service endpoints. Any help in pointing me in the right direction would be appreciated.

Some code snippets below for ease of reference...


In my Global.asax

        WebServiceHostFactory webServiceHostFactory = new WebServiceHostFactory();
        RouteTable.Routes.Add(new ServiceRoute(Accounts.Route, webServiceHostFactory, typeof(Accounts)));

If my 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>

In the Main function of his program

        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());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何其悲哀 2024-12-01 03:38:33

要使用路由并获取对服务端点的引用,您将需要自定义服务主机工厂。它可以执行与您当前正在使用的 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());
            }
        }
    }
}

To use the routes and get a reference to the service endpoint, you'll need your custom service host factory. It can do the same as the WebServiceHostFactory which you're currently using (simply return a reference to the WebServiceHost), but instead return a reference to your custom service host.

You can find more information about service host factories at 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());
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文