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

推荐订阅源

月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
S
SegmentFault 最新的问题
量子位
有赞技术团队
有赞技术团队
V
V2EX
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
B
Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Jina AI
Jina AI
C
Check Point Blog
G
Google Developers Blog
博客园 - 叶小钗
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园_首页
T
Tailwind CSS Blog
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
U
Unit 42

博客园 - 留云

dotnet core 自定义配置文件 Dotnet Core Windows Service golang 用tar打包文件或文件夹 golang 最和谐的子序列 golang 轮训加密算法 golang map golang 队列 golang 栈操作 golang 多维数组 golang 数组反转 nginx 在windows平台上对asp.net做反向代理 - 留云 树形结构应用技巧 用批命令更新数据库 c# 中Exception 的重试机制 HTTP协议之Session和Cookie C#多线程同步 SQL优化-索引 UML在关系型数据库设计中的应用 Web 应用的 UML 建模与 .NET 框架开发
jquery 调用wcf project
留云 · 2010-09-17 · via 博客园 - 留云

最近项目中涉及到比较多的jquery调用后台的方法,开始的做法大都是新建ashx 文件,项目后期导致项目中新建了很多的ashx文件,当然也可以用webservice,或者是在web 项目中建wcf 文件,为了以后统一管理方便,打算新建wcf project ,把所有的文件放在一个统一项目中,部署一台服务器来提供统一的服务。

   后来在使用过程中发现了几个问题,具体的可以参考 dudu 的文章 

  把我项目中碰到的问题记录下来。 

  就用Wcf 项目 自带的例子,我就修改了文件的名称

  接口如下:

    [ServiceContract(Name = "CmarketService", SessionMode = SessionMode.NotAllowed, ProtectionLevel = ProtectionLevel.None)]

    public interface ICmarketService

    {

        [OperationContract]

        [WebGet]

        //[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]

        //[WebInvoke(ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]

        string GetData(int value); 

    } 

Service 端:RequirementsMode 必须为AspNetCompatibilityRequirementsMode.Allowed 或者是AspNetCompatibilityRequirementsMode.Required,不然运行的时候会提示The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode setting as 'Allowed' or 'Required'.

   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

    //[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

    public class CmarketService : ICmarketService

    { 

     public string GetData(int value)

        {

            return string.Format("You entered:{0}", value);

        }

}

wcf 项目的配置文件

<configuration>

  <system.web>

    <compilation debug="true" targetFramework="4.0" />

  </system.web>

 <system.serviceModel>

    <diagnostics wmiProviderEnabled="true">

      <messageLogging logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" />

    </diagnostics>

    <services>

       <service name="CMSService.CmarketService">

          <endpoint address="" behaviorConfiguration="ServiceBehavior"

             binding="webHttpBinding" name="CmarketService" contract="CMSService.ICmarketService" />

          <endpoint address="mex" binding="mexHttpBinding" name="Mex" contract="IMetadataExchange" />

          <host>

             <baseAddresses>

                <add baseAddress="http://localhost/CmarketService/" />

             </baseAddresses>

          </host>

       </service>

    </services>

    <behaviors>

      <endpointBehaviors>

        <behavior name="ServiceBehavior">

          <enableWebScript/>

        </behavior>

      </endpointBehaviors>

      <serviceBehaviors>

        <behavior>

          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->

          <serviceMetadata httpGetEnabled="true" />

          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->

          <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <serviceHostingEnvironment  aspNetCompatibilityEnabled="true">

    </serviceHostingEnvironment>

  </system.serviceModel> 

 <system.webServer>

    <modules runAllManagedModulesForAllRequests="true"/>

  </system.webServer>

</configuration>

下来就可以在项目中使用了,调用方法如下

$(document).ready(function () {

            $.getJSON('http://localhost/CmarketService/CmarketService.svc/GetData',

                function (data) {

                    debugger;

                    alert(data.name)

                });

            $.ajax({

                url: 'http://localhost/CmarketService/CmarketService.svc/GetData',

                data: {"value":"5"},

                type: 'get',

                //dataType: 'json',

                contentType: 'text/json',

                success: function (data) {

                    debugger;

                    if (data.d) {

                    }

                },

                error: function (xhr) {

                    //debugger;

                    //alert(xhr.responseText);

                }

            });  

        }); 

 我开始的时候参考了dudu 的做法,在service 的方法加上WebInvoke 标签,后来发现调用的时候提示400 bad request,后来修改成 Webget ,调用成功。

Download Code