首页 > 代码库 > WCF IIS部署

WCF IIS部署

创建WCFHost应用程序

 Iservice.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.ServiceModel;
 6 using System.ServiceModel.Description;
 7 using System.Drawing;
 8 namespace WCFHost
 9 {
10     
11          
12     
13      [ServiceContract]
14     interface IService
15     {
16         
17          [OperationContract]
18          float Verify(byte[] img1, string id);
19         
20     }
21 }

Program.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace WCFHost
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             WcfHostor.StartService();
14             Console.WriteLine("Start Sever Success!");
15             Console.Read();
16 
17         }
18     }
19 }

Service.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.IO;
 7 using FRS;
 8 namespace WCFHost
 9 {
10     public  class Service:IService
11     {
12         FeatureData fa = new FeatureData();
13        // public static FeatureData FD{get;set;}
14 
15         static void InitFRS()
16         {
17             //System.Console.WriteLine(System.Environment.CurrentDirectory);
18             FRSParam param = new FRSParam();
19 
20             param.nMinFaceSize = 50;
21             param.nRollAngle = 10;
22             param.bOnlyDetect = true;
23 
24             FaceImage.Create(1, param);
25             Feature.Init(1);
26         }
27 
28         static Service()
29         {
30             InitFRS();
31         }
32       
33         public float Verify(byte[] img1, string id)
34         {
35             try
36             {
37                 Image img = FRS.Util.ImageHelper.BytesToImage(img1);
38                 List<HitAlert> res = fa.Search(img, id).ToList();
39                 if (res.Count == 0)
40                     return 0;
41                 else
42                     return res[0].Details[0].Score;
43           
44             }
45             catch
46             {
47                 return -1;
48             }
49 
50         }
51     }
52 }

WcfHosts

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.ServiceModel;
 6 using System.ServiceModel.Description;
 7 using System.Xml;
 8 using System.Runtime.Serialization;
 9 namespace WCFHost
10 {
11     class WcfHostor
12     {
13         static Uri baseAddress = new Uri("http://localhost:8099/Service");
14         static ServiceHost host;
15         public static void StartService()
16         {
17             
18             host = new ServiceHost(typeof(Service), baseAddress);
19             BasicHttpBinding binding = new BasicHttpBinding();
20 
21             binding.MaxBufferSize = 2147483647;
22             binding.MaxReceivedMessageSize = 2147483647;
23             binding.TransferMode = TransferMode.Streamed;
24             binding.SendTimeout = new TimeSpan(0, 0, 10);
25 
26 
27             // Enable metadata publishing.
28             ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
29 
30             
31             smb.HttpGetEnabled = true;
32             smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
33             host.Description.Behaviors.Add(smb);
34 
35             binding.ReaderQuotas.MaxDepth=32;
36             binding.ReaderQuotas.MaxArrayLength=2147483647;
37             binding.ReaderQuotas.MaxBytesPerRead=2147483647;
38              binding.ReaderQuotas.MaxNameTableCharCount=2147483647;
39             host.AddServiceEndpoint(typeof(IService), binding, baseAddress);
40            
41             host.Open();
42 
43             //// Create the ServiceHost.
44             //host = new ServiceHost(typeof(Service), baseAddress);
45 
46             //// Enable metadata publishing.
47             //ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
48 
49             //smb.HttpsGetBinding
50             //smb.HttpGetEnabled = true;
51             //smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
52             //host.Description.Behaviors.Add(smb);
53 
54             //// Open the ServiceHost to start listening for messages. Since
55             //// no endpoints are explicitly configured, the runtime will create
56             //// one endpoint per base address for each service contract implemented
57             //// by the service.
58             //host.Open();
59         }
60         public static void EndService()
61         {
62             if (host != null)
63             {
64                 host.Close();
65             }
66         }
67     }
68 }

至此,服务搭建完毕。

服务测试,建立C#应用程序

Reference->Add Service Reference

Address为上文 "http://localhost:8099/Service"

添加服务引用完毕。

WCF IIS部署