REST WCF 简单架设
来源:互联网 发布:怎样在手机上淘宝购物 编辑:程序博客网 时间:2024/06/05 18:19
首先 WCF 服务项目需要引用
System.ServiceModel.Activation
System.ServiceModel.Web
开始
接口
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.ServiceModel.Activation;using System.ServiceModel.Web;using System.Text;namespace StaticWCFService{ // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。 [ServiceContract] public interface IBetRadarHttpService { [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "GetMatchInformation?date={date}")] string GetMatchInformation(string date); // TODO: 在此添加您的服务操作 }}
这里要注意如果使用UriTemplate定义的参数必须为string类型 否则启动服务就报错
实现
using StaticWCFService.Base;using StaticWCFService.Cache;using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;using StaticWCFService.Entity;using StaticWCFService.Common;using System.ServiceModel.Activation;namespace StaticWCFService{ // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class BetRadarHttpService : BaseLogs, IBetRadarHttpService { private const string STR_MSG_TYPE = "BetRadarHttpService"; public string GetMatchInformation(string date) { string value = string.Empty; try { string ip = Utils.GetRemoteIPAddress(OperationContext.Current); bool checkIntervel = true; if (!string.IsNullOrEmpty(ip)) { checkIntervel = Cache.BetRadarServiceCallMgr.CheckIntervel(MethodEmun.GetMatchInformation, ip); } if (checkIntervel) { value = RemoteServiceComm.CashDBAgentServiceComm.GetMatchInformation(date); } } catch (Exception ex) { Error(STR_MSG_TYPE, MethodEmun.GetMatchInformation, ex, date); } DEBUG(STR_MSG_TYPE, OperationContext.Current, MethodEmun.GetMatchInformation, value, date); return value; } }}
最后是配置文件
<?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="DBAgentWCFService.appSet" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> <section name="DBAgentWCFService.app" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> </sectionGroup> </configSections> <system.web> <compilation debug="true" /> </system.web> <!-- 部署服务库项目时,必须将配置文件的内容添加到 主机的 app.config 文件中。System.Configuration 不支持库的配置文件。 --> <system.serviceModel> <services> <service name="StaticWCFService.BetRadarHttpService"> <endpoint address="" behaviorConfiguration="restBehavior" binding="webHttpBinding" bindingConfiguration="BetRadarService_WebHttpBinding" contract="StaticWCFService.IBetRadarHttpService" /> <host> <baseAddresses> <add baseAddress="http://192.168.30.194:8899/API/BetRadar/HTTP/" /> </baseAddresses> </host> </service> <service name="StaticWCFService.BetRadarService"> <endpoint address="" binding="basicHttpBinding" contract="StaticWCFService.IBetRadarService" bindingConfiguration="BetRadarService_BasicHttpBinding" /> <host> <baseAddresses> <add baseAddress="http://192.168.30.194:8899/API/BetRadar/" /> </baseAddresses> </host> </service> </services> <behaviors> <endpointBehaviors> <behavior name="restBehavior"> <webHttp/> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior> <!-- 为避免泄漏元数据信息, 请在部署前将以下值设置为 false --> <serviceMetadata httpGetEnabled="True"/> <!-- 要接收故障异常详细信息以进行调试, 请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <basicHttpBinding> <binding name="BetRadarService_BasicHttpBinding" openTimeout="00:00:05" closeTimeout="00:00:05" receiveTimeout="00:00:05" sendTimeout="00:00:05" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" /> </basicHttpBinding> <webHttpBinding> <binding name="BetRadarService_WebHttpBinding" openTimeout="00:00:05" closeTimeout="00:00:05" receiveTimeout="00:00:05" sendTimeout="00:00:05" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" /> </webHttpBinding> </bindings> </system.serviceModel></configuration>
配置文件需要注意的是作为REST 发布的服务必须绑定behaviorConfiguration 设置 <webHttp/>,绑定类型必须是webHttpBinding
配置完毕之后就可以启动服务了
0 0
- REST WCF 简单架设
- [WCF REST] 一个简单的REST服务实例
- 【REST WCF】
- REST WCF
- 简单五步创建一个WCF Rest Service
- WCF REST简单应用 编程初始化 help页面实现
- WCF and REST
- WCF REST Starter Kit
- WCF实现REST服务
- WCF实现REST
- 说说WCF Rest
- WCF REST 上传图片下载
- REST VS SOAP , WCF
- WCF Rest用法
- 简单介绍使用WCF的Web编程模型开发REST风格的Web Service
- REST WCF + jQuery EasyUI 应用
- REST WCF Service中的WebMessageBodyStyle
- 基于WCF的REST服务
- ATL 添加事件 error C2065: 'IID__IXXXEvents' : undeclared identifier
- Variational Bayes(变分贝叶斯)
- SSL/TLS协议详解
- 重命名Web项目时,需要更改的文件
- Java中native关键字
- REST WCF 简单架设
- vs2010 库函数问题:objidl.h(11266) : error C2061: syntax error : identifier '__RPC__out_xcount_part'
- 线段树基础入门学习(适合在notnoly success以前看)
- select函数详解及实例分析
- TCP/IP基础(二)
- hdu 1299 Diophantus of Alexandria (素数筛选)
- NYOJ 39 水仙花数
- OSG (OpenSceneGraph-3.2.1)编译程序下载
- Cocoapods安装和使用