WCF实现http访问地址后面带参数访问方式
2016-06-19 17:37
579 查看
最近在做一个移动支付相关项目,需要实现支付宝通知回调服务端,而支付宝通知回调是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、先是接口定义:
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>在这里面也有几个重要的地方,这些我都在配置文件中标注红色了,不明白的可以去查找详细的资料就不在这里做具体的说明了。
相关文章推荐
- java-用HttpURLConnection发送Http请求.
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- VBScript 剪贴板抓取URL并在浏览器中打开
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法