using SOA.WCF.Service; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace SOA.WCF { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Entrance”。 // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Entrance.svc 或 Entrance.svc.cs,然后开始调试。 public class Entrance : MathService { } }
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5"/> </system.web> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <!-- 若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。 在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。 --> <directoryBrowse enabled="true"/> </system.webServer> </configuration>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SOA.WCF.Hosting { class Program { static void Main(string[] args) { try { Console.WriteLine("I'm Console"); ServiceInit.StartService(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.Read(); } } } }
using SOA.WCF.Service; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace SOA.WCF.Hosting { public class ServiceInit { /// <summary> /// 启动服务 /// </summary> public static void StartService() { List<ServiceHost> hostList = new List<ServiceHost>() { new ServiceHost(typeof(MathService)), //new ServiceHost(typeof(Searcher)) }; foreach (ServiceHost host in hostList) { host.Open(); Console.WriteLine("{0}已经启动!", host.Description); } Console.WriteLine("输入任何字符串停止服务"); Console.ReadKey(); foreach (ServiceHost host in hostList) { host.Abort(); } Console.WriteLine("服务已关闭。。。。"); Console.Read(); //using (ServiceHost host = new ServiceHost()) //{ // #region 程序配置 // //host.AddServiceEndpoint(typeof(IMathService), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice"); // //if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null) // //{ // // ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); // // behavior.HttpGetEnabled = true; // // behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata"); // // host.Description.Behaviors.Add(behavior); // //} // #endregion 程序配置 // host.Opened += ()=> // { // Console.WriteLine("MathService已经启动,按任意键终止服务!"); // }; // host.Open(); //} } //cd C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\ //svcUtil.exe /out:c:\clientCode.cs /config:c:\app.config http://127.0.0.1:9999/calculatorservice/metadata } }
using SOA.WCF.Model; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace SOA.WCF.Interface { [ServiceContract] public interface IMathService { [OperationContract] int PlusInt(int x, int y); int Minus(int x, int y); [OperationContract] WCFUser GetUser(int x, int y); [OperationContract] List<WCFUser> UserList(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace SOA.WCF.Model { [DataContract] public class WCFUser { //[DataMember] public int Id { get; set; } [DataMember] public int Age { get; set; } [DataMember] public int Sex { get; set; } [DataMember] public string Name { get; set; } [DataMember] public string Description { get; set; } } public enum WCFUserSex { Famale, Male, Other } }
using SOA.WCF.Model; using SOA.WCF.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SOA.WCF.Service { public class MathService : IMathService { public int PlusInt(int x, int y) { return x + y; } public WCFUser GetUser(int x, int y) { return new WCFUser() { Id = 13, Name = "慕冬", Age = 22, Description = "这里是WCFServie", Sex = (int)WCFUserSex.Famale }; } public List<WCFUser> UserList() { return new List<WCFUser>(){ new WCFUser() { Id = 1, Name = "只想简简单单的活着", Sex = (int)WCFUserSex.Male, Age = 12, Description = "123456678990" }, new WCFUser() { Id = 2, Name = "謹記妳容顏", Sex = (int)WCFUserSex.Male, Age = 12, Description = "123456678990" }, new WCFUser() { Id = 3, Name = "雅牛", Sex = (int)WCFUserSex.Famale, Age = 112, Description = "123456678990" } }; } public int Minus(int x, int y) { return x - y; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; namespace SOA.WCF8 { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。 [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); // TODO: 在此添加您的服务操作 } // 使用下面示例中说明的数据约定将复合类型添加到服务操作。 [DataContract] public class CompositeType { bool boolValue = true; string stringValue = "Hello "; [DataMember] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] public string StringValue { get { return stringValue; } set { stringValue = value; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; namespace SOA.WCF8 { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。 // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs,然后开始调试。 public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}", value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite == null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } } }
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5"/> </system.web> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <!-- 若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。 在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。 --> <directoryBrowse enabled="true"/> </system.webServer> </configuration>