2

組み込みのMicrosoftは親子関係からのサイクルをサポートしていないため、このガイドに従ってカスタムフォーマッターを作成し、オブジェクトのシリアル化にNewtonsoftJson.NETを使用できるようにしました。

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

1 に答える 1

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());
            }
        }
    }
}
于 2011-07-15T02:39:52.517 に答える