首先 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
配置完毕之后就可以启动服务了