最近在做一个项目,需要在windows系统服务上动态创建WCF,研究了下,找到方法,记录如下:
核心部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ServiceHost host = new ServiceHost(typeof(DataSerivce.XXInterface), new Uri(publishUrl)); ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); if (smb == null) { smb = new ServiceMetadataBehavior(); host.Description.Behaviors.Add(smb); } smb.HttpGetEnabled = true; smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; ServiceDebugBehavior bhv = host.Description.Behaviors.Find<ServiceDebugBehavior>(); if (null != bhv) { bhv.IncludeExceptionDetailInFaults = true; } BasicHttpBinding binding = new BasicHttpBinding(); binding.Security.Mode = BasicHttpSecurityMode.None; host.AddServiceEndpoint(typeof(DataSerivce.IXXInterface), binding, ""); host.Open(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.Data; namespace DataSerivce { [ServiceContract] public interface IXXInterface { [OperationContract] DataTable GetCaseInfo(string systemName, string systemCode, string strsql); [OperationContract] string GetCaseInfoXml(string systemName, string systemCode, string strsql); } } |