WCF实现http访问地址后面带参数访问方式
来源:互联网 发布:呼死你在淘宝上叫什么 编辑:程序博客网 时间:2024/06/05 01:14
最近在做一个移动支付相关项目,需要实现支付宝通知回调服务端,而支付宝通知回调是URL带参数形式(http://xxxx.com?x=y&a=b)访问服务端,这个时候就需要实现一套匹配的服务端接收通知。由于已经有了一个WCF服务端(寄宿在WINFORM),所以想把支付宝通知回调服务也集成到现有的WCF服务中,之前写的都是URL后面不带参数的访问,试着直接带参数访问的时候能进入服务却找不到匹配的接口,网上找了很多资料也很少并且不是很详细,大多数只是提供了大致的思路。通过查找大量的资料和动手测试终于完成了,下面就把我所实现的贴出来希望对有需要的人有所帮助减少时间去查找资料。
1、先是接口定义:
using System;using System.Collections.Generic;using System.Linq;using System.ServiceModel;using System.ServiceModel.Web;using System.Text;using System.Threading.Tasks;namespace Contract{ [ServiceContract] public interface IServer { [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "<span style="color:#ff6666;">/notify?notify_id={notify_id}¬ify_name={notify_name}¬ify_content={notify_content}</span>", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] string Notify(string notify_id, string notify_name,string notify_content=null); }}这里主要是通过配置UriTemplate 来实现URL带参数访问(上面标红色的代码部分),还可以根据具体需要修改为其他形式。好啦,现在我们已经实现了URL带参数访问WCF服务了,但是还有一个问题,那就是正常情况下我们传递的参数可能不会有这么多或者说有的参数默认为空这个实现就不需要传递,这个时候怎么办呢,这个在正常的开发过程中我们就已经做过类似的了,只要把可以为空的参数赋值为null就可以了。
2、接口实现好了 那么就该实现服务了下面是服务实现:
using Contract;using System;using System.Collections.Generic;using System.Collections.Specialized;using System.Linq;using System.ServiceModel;using System.ServiceModel.Activation;using System.ServiceModel.Channels;using System.ServiceModel.Web;using System.Text;using System.Threading.Tasks;using System.Web;namespace Service{ // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service”。 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession)] public class Server : IServer { public string Notify(string notify_id, string notify_name, string notify_content = null) { return "success"; } }}在实现服务的时候记住一定要加上[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)],否则不能被脚本调用。
3、服务和接口都已经实现了,下面就是配置服务了,下面直接把服务的配置贴出来:
<?xml version="1.0" encoding="utf-8" ?><configuration> <system.serviceModel> <standardEndpoints> <webHttpEndpoint> <span style="color:#ff6666;"> <standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint></span> </webHttpEndpoint> </standardEndpoints> <behaviors> <endpointBehaviors> <span style="color:#ff6666;"> </span><behavior name="webHttpHavior"> <span style="color:#ff0000;"> <webHttp/></span> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="serviceHavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8081/service"/> <serviceDebug includeExceptionDetailInFaults="true"/> <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/> </behavior> </serviceBehaviors> </behaviors> <bindings> <webHttpBinding> <span style="color:#ff6666;"><binding crossDomainScriptAccessEnabled="true"></binding></span> </webHttpBinding> <!--<netTcpBinding> <binding name="netTcp" transferMode="Buffered"> <security mode="None"></security> </binding> </netTcpBinding>--> </bindings> <span style="color:#ff6666;"> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"></span> </serviceHostingEnvironment> <services> <service name="Service.Server" behaviorConfiguration="serviceHavior"> <host> <baseAddresses> <add baseAddress="http://localhost:8080"/> </baseAddresses> </host> <endpoint address="/app/service" contract="Contract.IServer" binding="webHttpBinding" behaviorConfiguration="webHttpHavior" ></endpoint> </service> </services> </system.serviceModel></configuration>在这里面也有几个重要的地方,这些我都在配置文件中标注红色了,不明白的可以去查找详细的资料就不在这里做具体的说明了。
1 0
- WCF实现http访问地址后面带参数访问方式
- WCF 的 Web 访问方式
- Android访问WCF服务(使用json实现参数传递)
- Android访问WCF服务(使用json实现参数传递)
- HttpHelps类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理
- httpClient访问webservice服务--带http头参数
- 无配置实现WCF访问
- WCF Asp.Net Http 通道访问配置
- C# http 访问Ip地址
- 以POST方式访问wcf方法
- 以Get方式访问wcf方法
- 文件上传,带其他非文件参数,返回文件可以访问的本地地址
- href后面带参数
- 实现私有地址访问互联网
- NSOperation 实现 HTTP多线程访问
- Android通过GET和POST方式访问WCF服务(Json)
- asp 实现访问memcached方式
- Silverlight3动态访问wcf
- 检查字符串是否存在字符变位
- PHP打印输出数组内容及结构函数print_r与var_dump
- 32岁,重回研发,不为别的,给自己过去一个交待!
- Access denied for user 'root '@'连接的ip'(using password:YES)
- 安装完Hadoop之后,命令行输入hadoop却找不到命令的解决方法
- WCF实现http访问地址后面带参数访问方式
- ACdream1415--最短路,桥
- 程序执行流程
- Java中Vector与ArrayList的区别详解
- C语言之预编译指令
- Spring-----Spring整合Struts2实例
- 设置随机颜色
- PowerCmd
- Javascript模块化编程(二):AMD规范