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