标签:ace 接口 技术分享 lap static 配置 operation double find
原文:http://www.cnblogs.com/artech/archive/2007/02/26/656901.html
方式1:
需引用 System.ServiceModel
namespace ConsoleApplication1 { /* 契约(Contracts):主要定义了实现那些服务,如何访问服务 契约 通常是一个 接口,定义了我们会提供哪些服务。 */ [ServiceContract(Name = "CalculatorService", Namespace = "http://www.artech.com/")] public interface ICalculator { //在方法上 声明OperationContract 表示该方法是一个对外的服务 [OperationContract] double Add(double x, double y); [OperationContract] double Subtract(double x, double y); [OperationContract] double Multiply(double x, double y); [OperationContract] double Divide(double x, double y); } /* 服务 则是如何实现这个契约 */ public class CalculatorService : ICalculator { public double Add(double x, double y) { return x + y; } public double Subtract(double x, double y) { return x - y; } public double Multiply(double x, double y) { return x * y; } public double Divide(double x, double y) { return x / y; } } }
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //原文:http://www.cnblogs.com/artech/archive/2007/02/26/656901.html using (ServiceHost host = new ServiceHost(typeof(CalculatorService))) { host.AddServiceEndpoint(typeof(ICalculator), 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); } host.Opened += delegate { Console.WriteLine("CalculaorService已经启动,按任意键终止服务!"); }; host.Open(); Console.Read(); } } } }
//方式2(配置App.config):
namespace ConsoleApplication1 { /* 契约(Contracts):主要定义了实现那些服务,如何访问服务 契约 通常是一个 接口,定义了我们会提供哪些服务。 */ [ServiceContract(Name = "CalculatorService", Namespace = "http://www.artech.com/")] public interface ICalculator { //在方法上 声明OperationContract 表示该方法是一个对外的服务 [OperationContract] double Add(double x, double y); [OperationContract] double Subtract(double x, double y); [OperationContract] double Multiply(double x, double y); [OperationContract] double Divide(double x, double y); } /* 服务 则是如何实现这个契约 */ public class CalculatorService : ICalculator { public double Add(double x, double y) { return x + y; } public double Subtract(double x, double y) { return x - y; } public double Multiply(double x, double y) { return x * y; } public double Divide(double x, double y) { return x / y; } } }
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //原文:http://www.cnblogs.com/artech/archive/2007/02/26/656901.html //WCF服务寄宿通过一个特殊的对象完成:ServiceHost。 //在上面的例子中,基于WCF服务的类型(typeof(CalculatorService))创建了ServieHost对象,并添加了一个终结点。 //具体的地址为http://127.0.0.1:9999/calculatorservice,采用了WSHttpBinding,并指定了服务契约的类型ICalculator。 //接下来是要将这个服务承载于宿主中。和为这个服务分配一个访问的地址。这时,需要新建一个App.config配置文件 ServiceHost host = new ServiceHost(typeof(CalculatorService));//将服务寄宿在宿主中 host.Open();//运行 Console.WriteLine("服务已启动、、、"); Console.ReadKey(true); host.Close(); //关闭 } } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:9999/calculatorservice/metadata" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="metadataBehavior" name="ConsoleApplication1.CalculatorService"> <endpoint address="http://127.0.0.1:9999/calculatorservice" binding="wsHttpBinding" contract="ConsoleApplication1.ICalculator" /> </service> </services> </system.serviceModel> </configuration>
标签:ace 接口 技术分享 lap static 配置 operation double find
原文地址:https://www.cnblogs.com/guxingy/p/10329592.html