首页 技术 正文
技术 2022年11月7日
0 收藏 818 点赞 1,029 浏览 1299 个字

问题描述:

现在有已有的WCF服务,但是ajax是不能请求到这个服务的;

需要把WCF转成WCF REST 的风格。

以下是从WCF转 WCF REST的步骤

1.首先在接口定义的地方加上 请求

WebInvoke http的请求动作 
如下:

[OperationContract]
[WebInvoke(Method = “POST”, UriTemplate = “Login”, ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
bool Login(string userNo, string pwd);

UriTemplate中的“login”  是接口的名称 其他不需要变;

下面来解释下 4种WebMessageBodyStyle的含义,按需配置

Bare:请求和响应都是裸露的
WrappedRequest:请求是包裹的(,响应是裸露的)
WrappedResponse:响应是包裹的(,请求是裸露的)
Wrapped:请求和响应都是包裹的
例子:GetOneParameter
BodyStyle = WebMessageBodyStyle.Bare / WrappedRequest
返回值:”You get: 1″
BodyStyle = WebMessageBodyStyle.WrappedResponse / Wrapped
返回值:{“GetOneParameterResult”:”You get: 1″}
WrappedRequest和Wrapped的请求是包裹的,即要指明key=value

2.webConfig 的配置

<service >
<endpoint address=”” behaviorConfiguration=”webBehavior” binding=”webHttpBinding” name=”test” contract=”test.model” >
</endpoint>
</service>

<endpointBehaviors>
<behavior name=”webBehavior”>
<webHttp />
</behavior>
</endpointBehaviors>

其中behaviorConfiguration=”webBehavior” binding=”webHttpBinding”  这个是必须的

注意事项

1)只要有OperationContract这个属性的,都需要添加 WebInvoke 请求动作。

2)如果出现  由于 AddressFilter 在 EndpointDispatcher 不匹配,To 为“http://localhost:8006/rest/DoWork”的消息无法在接收方处理。请检查发送方和接收方的 EndpointAddresses 是否一致。

就是第二点的配置有问题,改成以上的配置即可。

2)如果服务报500 的 System.ServiceModel.ServiceActivtyException 这个问题,则在本地调试服务,在视图里面会有相应的报错,解决即可。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,964
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781