创建WCFHost应用程序
Iservice.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Drawing;
namespace WCFHost
{ [ServiceContract]
interface IService
{ [OperationContract]
float Verify(byte[] img1, string id); }
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WCFHost
{
class Program
{
static void Main(string[] args)
{
WcfHostor.StartService();
Console.WriteLine("Start Sever Success!");
Console.Read(); }
}
}
Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using FRS;
namespace WCFHost
{
public class Service:IService
{
FeatureData fa = new FeatureData();
// public static FeatureData FD{get;set;} static void InitFRS()
{
//System.Console.WriteLine(System.Environment.CurrentDirectory);
FRSParam param = new FRSParam(); param.nMinFaceSize = ;
param.nRollAngle = ;
param.bOnlyDetect = true; FaceImage.Create(, param);
Feature.Init();
} static Service()
{
InitFRS();
} public float Verify(byte[] img1, string id)
{
try
{
Image img = FRS.Util.ImageHelper.BytesToImage(img1);
List<HitAlert> res = fa.Search(img, id).ToList();
if (res.Count == )
return ;
else
return res[].Details[].Score; }
catch
{
return -;
} }
}
}
WcfHosts
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Xml;
using System.Runtime.Serialization;
namespace WCFHost
{
class WcfHostor
{
static Uri baseAddress = new Uri("http://localhost:8099/Service");
static ServiceHost host;
public static void StartService()
{ host = new ServiceHost(typeof(Service), baseAddress);
BasicHttpBinding binding = new BasicHttpBinding(); binding.MaxBufferSize = ;
binding.MaxReceivedMessageSize = ;
binding.TransferMode = TransferMode.Streamed;
binding.SendTimeout = new TimeSpan(, , ); // Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb); binding.ReaderQuotas.MaxDepth=;
binding.ReaderQuotas.MaxArrayLength=;
binding.ReaderQuotas.MaxBytesPerRead=;
binding.ReaderQuotas.MaxNameTableCharCount=;
host.AddServiceEndpoint(typeof(IService), binding, baseAddress); host.Open(); //// Create the ServiceHost.
//host = new ServiceHost(typeof(Service), baseAddress); //// Enable metadata publishing.
//ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); //smb.HttpsGetBinding
//smb.HttpGetEnabled = true;
//smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
//host.Description.Behaviors.Add(smb); //// Open the ServiceHost to start listening for messages. Since
//// no endpoints are explicitly configured, the runtime will create
//// one endpoint per base address for each service contract implemented
//// by the service.
//host.Open();
}
public static void EndService()
{
if (host != null)
{
host.Close();
}
}
}
}
至此,服务搭建完毕。
服务测试,建立C#应用程序
Reference->Add Service Reference
Address为上文 "http://localhost:8099/Service"
添加服务引用完毕。