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

推荐订阅源

aimingoo的专栏
aimingoo的专栏
S
Securelist
博客园 - Franky
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
AI
AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
Attack and Defense Labs
Attack and Defense Labs
Vercel News
Vercel News
腾讯CDC
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
量子位
S
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
Cyberwarzone
Cyberwarzone
S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
News and Events Feed by Topic
T
Tenable Blog
PCI Perspectives
PCI Perspectives
MyScale Blog
MyScale Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
W
WeLiveSecurity
N
News | PayPal Newsroom
P
Proofpoint News Feed
O
OpenAI News
C
CERT Recently Published Vulnerability Notes
B
Blog
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Spread Privacy
Spread Privacy

博客园 - 曾伟

不干胶、热敏打印 wpf 滚屏数据显示 net core quartz调度 warp打包 nssm部署到windowsservice c# webapi 过滤器token、sign认证、访问日志 c# NPOI文件操作 c# ini文件操作 网页打印javascript:window.print() window.showModalDialog弹出对话框刷新问题 ASP.NET] 选择文件夹的对话框 常用SQL语句书写技巧 控制同一exe程序打开多次 - 曾伟 - 博客园 IIS6 MMC检测到此管理单元发生一个错误,建议您关闭并重新启动mmc 在上传文件时限制上传文件的大小,并捕捉超过文件大小限制的 - 曾伟 - 博客园 Lucene的例子 javascript 获取标签具体位置 - 曾伟 - 博客园 JavaScript实现类,有多种方法。 Javascript实现截图功能(代码) 终端服务器超出了最大允许连接数 DBCC CHECKDB 数据库或表修复
WCF多种调用方式兼容
曾伟 · 2015-06-03 · via 博客园 - 曾伟

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             });