调用:启动wcf服务程序,然后建立一个控制台应用程序
建立wcf程序
添加
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfService { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service2”。 // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service2.svc 或 Service2.svc.cs,然后开始调试。 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class Service2 : IService2 { public string Check() { return "哈哈哈"; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfService { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService2”。 [ServiceContract] public interface IService2 { [OperationContract] [FaultContract(typeof(ApplicationServiceError))] string Check(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Web; namespace WcfService { [DataContract(Name = "ServiceError", Namespace = "Microsoft.Samples.DistributedServices.Core")] public class ApplicationServiceError { /// <summary> /// 客户端服务的错误消息 /// </summary> [DataMember(Name = "ErrorMessage")] public string ErrorMessage { get; set; } } }