专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WCF

WCF多种调用形式兼容

发布时间:2011-06-23 13:51:21 文章来源:www.iduyao.cn 采编人员:星星草
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             });

 

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: