惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
WordPress大学
WordPress大学
爱范儿
爱范儿
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客
博客园_首页
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
MyScale Blog
MyScale Blog
IT之家
IT之家
H
Help Net Security
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
人人都是产品经理
人人都是产品经理

博客园 - 空紫竹

Crystal Report Error: Either the Crystal Reports registy key permission are insufficient or the Crystal Reports runtime is not installed correctly SSRS 例子的网站 SSRS配置 获取数据库中有默认值的所以字段 sql server xml nodes 的使用 Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, Version=3.0.0.0 IIS 时间问题 sql server 创建临时表 系统程序监控软件 Window 7sp1 安装vs2010 sp1 打开xaml文件崩溃 windows 2008 安装 sql server 2008 详解Silverlight中PivotViewer控件编程实例 CSS资源网址 SQL server 2008 安装和远程访问的问题 vss File for <file> (<physical file>) Was Not Found silverlight 报错超时 sql server isnumeric 函数 asp 中的getChunk(img_size) Windows 7 恢复删除的数据
JQuery, Silverlight 公用WCF
空紫竹 · 2013-07-26 · via 博客园 - 空紫竹

WCF web.config配置:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="" closeTimeout="00:30:00" openTimeout="00:30:00"
          receiveTimeout="00:30:00" sendTimeout="00:30:00" maxBufferSize="2147483647"
          maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <!--below config is for https-->
          <!--<security mode="Transport">
            <transport clientCredentialType="None" />
          </security>-->
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="WcfHost.Service2">
        <endpoint address="Ajax" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" contract="WcfHost.Service2" />
        <endpoint address="Siverlight"
          binding="basicHttpBinding" contract="WcfHost.Service2" />        
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

在JQuery使用:

<script type="text/javascript">
  var Type;
  var Url;
  var Data;
  var ContentType;
  var DataType;
  var ProcessData;

  function WCFJSON() {
    Type = "POST";
    Url = "/WcfHost/Service2.svc/Ajax/GetTest";
    ContentType = "application/json; charset=utf-8";
    DataType = "json";
    ProcessData = true;
    CallService();
  }

  function CallService() {
    $.ajax({
      type: Type, //GET or POST or PUT or DELETE verb
      url: Url, 
      data: Data, 
      contentType: ContentType,
      dataType: DataType, 
      processdata: ProcessData,
      success: function (msg) {
        ServiceSucceeded(msg);
      },
      error: ServiceFailed
    });
  }

  function ServiceFailed(xhr) {
    alert('Service call failed: ' + xhr.status + '' + xhr.statusText);
    Type = null;
    varUrl = null;
    Data = null;
    ContentType = null;
    DataType = null;
    ProcessData = null;
  }

  function ServiceSucceeded(result) {
    if (DataType == "json") {
      alert(result.d);
    }
  }

  $(document).ready(
    function () {
      WCFJSON();
    }
);
</script>

WCF文件

namespace WcfHost
{
  [ServiceContract(Namespace = "")]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  public class Service2
  {
    [OperationContract]
    public void DoWork()
    {
      // Add your operation implementation here
      return;
    }
    // Add more operations here and mark them with [OperationContract]
    [OperationContract]
    public string GetTest()
    {
      return "Test";
    }
  }
}