using System; using System.Collections.Generic; using System.Linq; using System.Text; //new using using System.Runtime.Serialization; using System.ServiceModel; namespace Service { [ServiceContract(Name="http://oec2003.cnblogs.com")] public interface IHelloWorldService {
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace Service { public class HelloWorldService:IHelloWorldService { public string SayHello() { return "Hello oec2003"; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using Service; namespace Host { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(HelloWorldService))) { host.AddServiceEndpoint(typeof(IHelloWorldService), new BasicHttpBinding(), new Uri("http://localhost:10000/Service/HelloWorldService")); if (host.State != CommunicationState.Opening) host.Open(); Console.WriteLine("服务已经启动!"); Console.ReadLine(); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace Client { class Program { static void Main(string[] args) { EndpointAddress ea = new EndpointAddress("http://localhost:10000/Service/HelloWorldService"); IHelloWorldService proxy = ChannelFactory<IHelloWorldService>.CreateChannel(new BasicHttpBinding(),ea); Console.WriteLine(proxy.SayHello()); Console.ReadLine(); } } }