REST WCF 简单架设

首先 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值