WCF 自托管、无配置文件实现jsonp(跨域)的访问

前端之家收集整理的这篇文章主要介绍了WCF 自托管、无配置文件实现jsonp(跨域)的访问前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

以下内容基于WCF4.0,本文将对比讨论配置文件方案和无配置文件方案的实现方式。

@H_404_4@ WCF4.0加入了对RESTFU和标准终结点的支持,这为实现跨域提供了简单的方式。

@H_404_4@ 一、有配置文件的情况:

@H_404_4@ 首先我们先定义一个服务:

[ServiceContract]
@H_404_22@public @H_404_22@class MinitorServer
{
        [OperationContract]
        @H_404_22@bool Test()
        {
            @H_404_22@return @H_404_22@true;
        }
}
@H_404_4@ 在这里我故意没有声明接口,顺便废话几句,正常情况下我们应该定义接口去显示服务契约(servercontract)和操作契约(operationcontract),但是对于一些简单的服务,我们可以省略接口的定义,做事不应循规蹈矩。

@H_404_4@ 1、配置文件

@H_404_22@<?xml version="1.0" encoding="utf-8" @H_404_22@?>
 @H_404_22@<configuration@H_404_22@>
   system.serviceModel@H_404_22@>
     behaviors@H_404_22@>
      endpointBehaviors@H_404_22@>
         behavior name@H_404_22@="webHttp"@H_404_22@>
              webHttp automaticFormatSelectionEnabled@H_404_22@="true" defaultOutgoingResponseFormat@H_404_22@="Json" @H_404_22@/>
          @H_404_22@</behavior@H_404_22@>
          standardEndpoints@H_404_22@>
       webHttpEndpointstandardEndpoint crossDomainScriptAccessEnabled@H_404_22@="true" @H_404_22@/>
       bindingswebHttpBindingbinding ="true"  services@H_404_22@>      
       service ="HD.ExamMonitorClient.MinitorServer"endpoint kind@H_404_22@="webHttpEndpoint"
                   behaviorConfiguration@H_404_22@="webHttp"
                   address@H_404_22@="http://localhost:8088/MonitorServer/"
                   contractservice@H_404_22@>
 @H_404_22@>
@H_404_4@ 在这里比较重要的是:

@H_404_4@ kind="webHttpEndpoint" 表示该终结点采用标准终结点;

@H_404_4@   crossDomainScriptAccessEnabled="true" 设置该终结点可以响应跨域请求;

@H_404_4@   automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" 自动将响应类型设置为json,当然你可以根据自己的需求修改为xml。

@H_404_4@ 2、修改服务加入attribute用来响应get请求:

class MinitorServer { [OperationContract] [WebGet] 在这里如果你上一步没有配置automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json"那你应该将[WebGet] 改为[WebGet(ResponseFormat=WebMessageFormat.Json)],必须要注意的是:如果单纯响应非跨域请求,不需要设置defaultOutgoingResponseFormat="Json" ,因为在http请求的头部已经指定了数据类型。

@H_404_4@ 3、在控制台中托管服务:

using(host = @H_404_22@new ServiceHost(@H_404_22@typeof(MinitorServer))) { host.Open();
   Console.ReadKey(); }
@H_404_4@ 4、浏览器测试

$(@H_404_22@function () {
             $.ajax({
                 type: "get",url: "http://localhost:8088/MonitorServer/Test",dataType: "jsonp",success: @H_404_22@function (ret) {
                     console.log(ret);
                 }
             });
         });
@H_404_4@ 二、无配置文件方式:

@H_404_4@ 配置文件方式的难点在于不能直接设置标准终结点。在这里要指出,标准终结点=绑定+终结点行为,所以我们可以这样设置:

using(host = @H_404_22@typeof(MinitorServer)))
{

            //定义一个webHttp的绑定
            WebHttpBinding webBing = @H_404_22@new WebHttpBinding();
            webBing.CrossDomainScriptAccessEnabled = @H_404_22@true;
            
            定义一个终结点行为
            @H_404_22@var endpointBehavior =@H_404_22@new WebHttpBehavior();
            endpointBehavior.AutomaticFormatSelectionEnabled = @H_404_22@true;
            endpointBehavior.DefaultOutgoingResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json;

            
            加入服务
            @H_404_22@var end = host.AddServiceEndpoint(@H_404_22@typeof(MinitorServer),webBing,"http://localhost:8088/MonitorServer/");
            end.Behaviors.Add(endpointBehavior);
            
           

            host.Open();
            Console.ReadKey();  
}            
@H_404_4@ 现在可以将配置文件删除了。

@H_404_4@ 另外如果讨厌去为每个操作协定设置[WebGet],那么这里有个简单方式,在open之前,我们循环为每个操作协定加入行为即可。

var operationBehavio=@H_404_22@new WebGetAttribute(); foreach (@H_404_22@var item @H_404_22@in end.Contract.Operations) { @H_404_22@if (item.Behaviors.Find<WebGetAttribute>() == @H_404_22@null) { item.Behaviors.Add(operationBehavio); } }
转载自:http://www.cnblogs.com/fej121/p/4993514.html (我个人没有验证)

猜你在找的Json相关文章