WCF多种调用方式兼容

原文:WCF多种调用方式兼容

1、能被ajax get

2、能post

3、wcf正常调用

实现:

 1     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
 2     [JavascriptCallbackBehavior(UrlParameterName = "jsoncallback")]
 3     public class WCFJsonTest : IWCFJsonTest
 4     {
 5
 6         public List<TestModel> GetTest(string id)
 7         {
 8             List<TestModel> list = new List<TestModel>();
 9             TestModel t = new TestModel();
10             t.Ids = 1;
11             t.Names = id;
12             list.Add(t);
13
14             TestModel t1 = new TestModel();
15             t1.Ids = 1;
16             t1.Names = id+"_"+Guid.NewGuid().ToString();
17             list.Add(t1);
18
19             return list;
20         }
21
22
23         public TestModel PostTest(string id, string name)
24         {
25             TestModel t1 = new TestModel();
26             t1.Ids = int.Parse(id);
27             t1.Names = name + "_" + Guid.NewGuid().ToString();
28             return t1;
29         }
30
31
32         public TestModel PostTest1(TestModel model)
33         {
34             TestModel t1 = new TestModel();
35             t1.Ids = model.Ids;
36             t1.Names = model.Names + "_" + Guid.NewGuid().ToString();
37             return t1;
38         }
39     }

接口:

 1 [ServiceContract]
 2
 3     public interface IWCFJsonTest
 4     {
 5         [OperationContract]
 6         [WebGet(UriTemplate = "/GetTest/{id}", ResponseFormat = WebMessageFormat.Json)]
 7         List<TestModel> GetTest(string id);
 8
 9         [OperationContract]
10         [WebInvoke(Method="GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
11         TestModel PostTest(string id, string name);
12
13         [OperationContract]
14         [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
15         TestModel PostTest1(TestModel model);
16     }

配置:

endpoint配置两个一个web使用webHttpBinding,一个给wcf

 1 <system.serviceModel>
 2     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
 3     <services>
 4       <service name="BLL.WCFJsonTest" behaviorConfiguration="AllBehavior" >
 5         <endpoint contract="IBLL.IWCFJsonTest" binding="wsHttpBinding" bindingConfiguration="WsHttpBinding_TEST" address="wcf" />
 6         <endpoint kind="webHttpEndpoint" contract="IBLL.IWCFJsonTest" binding="webHttpBinding" bindingConfiguration="basicTransport" behaviorConfiguration="web" address="" />
 7         <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
 8       </service>
 9     </services>
10
11     <behaviors>
12       <serviceBehaviors>
13         <behavior name="AllBehavior">
14           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
15           <serviceDebug includeExceptionDetailInFaults="true" />
16         </behavior>
17         <behavior name="">
18           <serviceMetadata httpGetEnabled="true" />
19           <serviceDebug includeExceptionDetailInFaults="false" />
20         </behavior>
21       </serviceBehaviors>
22       <endpointBehaviors>
23         <behavior name="web">
24           <webHttp helpEnabled="true"/>
25         </behavior>
26       </endpointBehaviors>
27     </behaviors>
28     <bindings>
29       <webHttpBinding>
30         <binding name="basicTransport" crossDomainScriptAccessEnabled="true"/>
31       </webHttpBinding>
32       <wsHttpBinding>
33         <binding name="WsHttpBinding_TEST" closeTimeout="00:01:00"
34         openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
35         allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
36          maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
37         messageEncoding="Text" textEncoding="utf-8"
38         useDefaultWebProxy="true">
39           <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
40        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
41           <security mode="None">
42             <transport clientCredentialType="None" proxyCredentialType="None"
43         realm="" />
44             <message clientCredentialType="UserName" algorithmSuite="Default" />
45           </security>
46         </binding>
47       </wsHttpBinding>
48     </bindings>
49     <standardEndpoints>
50       <webHttpEndpoint>
51         <standardEndpoint crossDomainScriptAccessEnabled="true"/>
52       </webHttpEndpoint>
53     </standardEndpoints>
54   </system.serviceModel>

调用:

wcf正常调用地址:http://xxxxxx:xxxx/JsonTestService.svc

post:http://xxxxxx:xxxx/JsonTestService.svc/PostTest

get:http://xxxxxx:xxxx/JsonTestService.svc/GetTest/2

 

例如:

 1 string srcString=GetData("", "http://xxxxxx:xxxx/JsonTestService.svc/GetTest/2");
 2
 3                 srcString = PostHelper.GetPermissionRemoteData("http://xxxxxx:xxxx/JsonTestService.svc/PostTest","{\"id\":\"10\",\"name\":\"张三\"}","text/json");
 4
 5                 string jsonStr = "{\"Ids\":\"10\",\"Names\":\"张三1\"}";
 6                 srcString = PostHelper.GetPermissionRemoteData("http://xxxxxx:xxxx/JsonTestService.svc/PostTest1", "{\"model\": "+ jsonStr+" }", "text/json");
 7
 8                 WCFJsonTestClient client = new WCFJsonTestClient();
 9                 var r = client.GetTest("1");
10                 var r1 = client.PostTest("1", "a");
11                 var r2 = client.PostTest1(new TestModel() { Ids = 1, Names = "2" });
 1 $.ajax({
 2                 type: "GET",
 3                 dataType: "jsonp",
 4                 url: 'http://xxxxxx:xxxx/JsonTestService.svc/GetTest/2',
 5                 success: function (data) {
 6                     console.log(data);
 7                 },
 8                 error: function (XMLHttpRequest, textStatus, errorThrown) {
 9                     console.log('err1' + XMLHttpRequest);
10                     console.log('err2' + textStatus);
11                     console.log('err3' + errorThrown);
12                 }
13             });

 

时间: 2024-07-29 17:32:01

WCF多种调用方式兼容的相关文章

JavaScript中具名函数的多种调用方式总结_javascript技巧

前面有一篇提到了 匿名函数的多种调用方式.这篇看看具名函数的多种调用方式. 1.()  平时最常用的就是()运算符来调用/执行一个函数: 复制代码 代码如下: // 无参函数fun1 function fun1() {     alert('我被调用了'); } fun1();   // 有参函数fun2 function fun2(param) {     alert(param); } fun2('我被调用了'); ECMAScript3后加入给Function加入了call和apply后,

《JavaScript设计模式》——2.5 多种调用方式——多态

2.5 多种调用方式--多态 "小铭,在面向对象编程中不是还有一种特性叫作多态么?在JavaScript中可以实现么?" "多态,就是同一个方法多种调用方式吧.在JavaScript中也是可以实现的,只不过要对传入的参数做判断以实现多种调用方式,如我们定义一个add方法,如果不传参数则返回10,如果传一个参数则返回10+参数,如果传两个参数则返回两个参数相加的结果." //多态 function add(){ // 获取参数 var arg = arguments,

Javascript中匿名函数的多种调用方式总结_javascript技巧

Javascript中定义函数的方式有多种,函数直接量就是其中一种.如var fun = function(){},这里function如果不赋值给fun那么它就是一个匿名函数.好,看看匿名函数的如何被调用. 方式1,调用函数,得到返回值.强制运算符使函数调用执行 复制代码 代码如下: (function(x,y){     alert(x+y);     return x+y; }(3,4)); 方式2,调用函数,得到返回值.强制函数直接量执行再返回一个引用,引用再去调用执行 复制代码 代码如

Android开发中怎样调用系统Email发送邮件(多种调用方式)

我们都知道,在Android中调用其他程序进行相关处理,几乎都是使用的Intent,所以,Email也不例外. 在Android中,调用Email有三种类型的Intent: Intent.ACTION_SENDTO 无附件的发送 Intent.ACTION_SEND 带附件的发送 Intent.ACTION_SEND_MULTIPLE 带有多附件的发送 当然,所谓的调用Email,只是说Email可以接收Intent并做这些事情,可能也有其他的应用程序实现了相关功能,所以在执行的时候,会出现选择

Wcf:可配置的服务调用方式

添加wcf服务引用时,vs.net本来就会帮我们在app.config/web.config里生成各种配置,这没啥好研究的,但本文谈到的配置并不是这个.先看下面的图: 通常,如果采用.NET的WCF技术来架构SOA风格的应用,我们会把项目做一些基本的分层,如上图: 01. contract层:通常定义服务的接口(即服务契约ServiceContract,指明该服务提供了哪些方法可供外部调用).以及接口方法中传输的Model定义(即:数据契约DataContract,指明方法中的对象参数的Clas

wcf+silverlight 在ie10兼容模式下正常调用wcf服务,在标准模式下无法调用

问题描述 wcf+silverlight 在ie10兼容模式下正常调用wcf服务,在标准模式下无法调用 wcf+silverlight程序,在chrome,firfox,ie6-ie9下都能正常访问wcf服务,但是在ie10和ie11下访问wcf服务就报500错误了(我用fiddler监听看到的状态).还有在开发环境和部署在本地iis上是能正常访问的.部署到服务器后在ie10和ie11兼容模式下也可以正常访问. 我想问为什么部署到服务器后在ie10和ie11标准模式下无法调用wcf服务? 网站:

web service-gSoap可以访问WCF绑定wshttpbinding方式的WebService吗?

问题描述 gSoap可以访问WCF绑定wshttpbinding方式的WebService吗? WebService 服务端是用C#用WCF写的,想用C++访问WebService的服务器,就用了gSoap2.8.试了几次发现gSoap似乎只能访问basicHttpbinding方式的WCF,而不能访问WsHttpBinding方式的WCF.不知道是我哪里用的不对,还是gSoap就是不能调用wsHttpbinding的WCF? 解决方案 查了一些例子,也都是访问BasicHttpbinding的

C#开发微信门户及应用(11)--微信菜单的多种表现方式介绍

原文:C#开发微信门户及应用(11)--微信菜单的多种表现方式介绍 在前面一系列文章中,我们可以看到微信自定义菜单的重要性,可以说微信公众号账号中,菜单是用户的第一印象,我们要规划好这些菜单的内容,布局等信息.根据微信菜单的定义,我们可以看到,一般菜单主要分为两种,一种是普通的Url菜单(类型为View的菜单),一种是事件菜单(类型为Click的菜单),一般情况下,微信的Url菜单,是无法获得用户的任何信息的,但微信用户信息非常重要,因此也提供了另外一种方式(类似重定向的方式)来给我们使用,本篇

Spark源码分析:多种部署方式之间的区别与联系(1)

<http://www.aliyun.com/zixun/aggregation/13383.html">Spark源码分析:多种部署方式之间的区别与联系(1)> <Spark源码分析:多种部署方式之间的区别与联系(2)> 从官方的文档我们可以知道,Spark的部署方式有很多种:local.Standalone.Mesos.YARN-..不同部署方式的后台处理进程是不一样的,但是如果我们从代码的角度来看,其实流程都差不多. 从代码中,我们可以得知其实Spark的部署