【问题标题】:WCF with JSONP but not special properties具有 JSONP 但没有特殊属性的 WCF
【发布时间】:2012-12-26 04:35:59
【问题描述】:

我有一个需要返回 JSON/JSONP 的 WCF 4.5 服务。我可以通过使用WebScriptServiceHostFactory 来做到这一点,但这会将__type 特殊属性添加到所有对象并将响应包装在{d:{...}} 对象中。我不想那样。

我可以使用WebServiceHostFactory 摆脱它,但它不支持 JSONP。

如何从 WCF 获得干净的 JSONP 输出?

【问题讨论】:

    标签: .net wcf jsonp compatibility


    【解决方案1】:

    你的函数/方法有这些属性吗?

    <OperationContract()> _
    <WebGet(UriTemplate:="GetJobDetails?inpt={inpt}", BodyStyle:=WebMessageBodyStyle.Wrapped,
            RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json)>
    

    【讨论】:

    • 这些属性不会影响是否支持 JSONP。
    【解决方案2】:

    您有几个选项可以通过 webHttpBinding 启用 JSONP:

    如果您想使用WebServiceHostFactory,您可以在该工厂创建的default 端点中更改crossDomainScriptAccessEnabled 属性的默认值。这可以通过将以下部分添加到您的配置来完成:

    <system.serviceModel>
        <standardEndpoints>
            <webHttpEndpoint>
                <standardEndpoint crossDomainScriptAccessEnabled="true"
                                  defaultOutgoingResponseFormat="Json"/>
            </webHttpEndpoint>
        </standardEndpoints>
    </system.serviceModel>
    

    如果您不想更改应用程序中所有 Web 服务主机的默认设置,那么您可以仅对您的应用程序进行更改。一种替代方法是使用自定义服务主机工厂,它将根据需要设置端点:

        public class MyFactory : ServiceHostFactory
        {
            protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
            {
                return new MyServiceHost(serviceType, baseAddresses);
            }
    
            class MyServiceHost : ServiceHost
            {
                public MyServiceHost(Type serviceType, Uri[] baseAddresses)
                    : base(serviceType, baseAddresses)
                {
                }
    
                protected override void OnOpening()
                {
                    // this assumes the service contract is the same as the service type
                    // (i.e., the [ServiceContract] attribute is applied to the class itself;
                    // if this is not the case, change the type below.
                    var contractType = this.Description.ServiceType;
                    WebHttpBinding binding = new WebHttpBinding();
                    binding.CrossDomainScriptAccessEnabled = true;
                    var endpoint = this.AddServiceEndpoint(contractType, binding, "");
                    var behavior = new WebHttpBehavior();
                    behavior.DefaultOutgoingResponseFormat = WebMessageFormat.Json;
                    endpoint.Behaviors.Add(behavior);
                }
            }
        }
    

    另一种选择是通过配置定义端点:

    <system.serviceModel>
        <services>
            <service name="StackOverflow_14280814.MyService">
                <endpoint address=""
                          binding="webHttpBinding"
                          bindingConfiguration="withJsonp"
                          behaviorConfiguration="web"
                          contract="StackOverflow_14280814.MyService" />
            </service>
        </services>
        <bindings>
            <webHttpBinding>
                <binding name="withJsonp" crossDomainScriptAccessEnabled="true" />
            </webHttpBinding>
        </bindings>
        <behaviors>
            <endpointBehaviors>
                <behavior name="web">
                    <webHttp/>
                </behavior>
            </endpointBehaviors>
        </behaviors>
    </system.serviceModel>
    

    【讨论】:

    • 对我们来说是关键。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多