1>客户端程序调用wcf,在程序中配置服务器的节点
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Web; using System.ServiceModel.Description; namespace ConsoleApplication1 { [ServiceContract] public interface IService { [OperationContract] [WebGet] string EchoWithGet(string s); [OperationContract] [WebInvoke] string EchoWithPost(string s); } public class Service : IService { public string EchoWithGet(string s) { return "You said " + s; } public string EchoWithPost(string s) { return "You said " + s; } } class Program { static void Main(string[] args) { WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:7900/")); try { ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), ""); host.Open(); using (ChannelFactory<IService> cf = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:7900")) { cf.Endpoint.Behaviors.Add(new WebHttpBehavior()); IService channel = cf.CreateChannel(); string s; Console.WriteLine("Calling EchoWithGet via HTTP GET: "); s = channel.EchoWithGet("Hello, world"); Console.WriteLine(" Output: {0}", s); Console.WriteLine(""); Console.WriteLine("This can also be accomplished by navigating to"); Console.WriteLine("http://localhost:7900/EchoWithGet?s=Hello, world!"); Console.WriteLine("in a web browser while this sample is running."); Console.WriteLine(""); Console.WriteLine("Calling EchoWithPost via HTTP POST: "); s = channel.EchoWithPost("Hello, world"); Console.WriteLine(" Output: {0}", s); Console.WriteLine(""); } Console.WriteLine("Press <ENTER> to terminate"); Console.ReadLine(); host.Close(); } catch (CommunicationException cex) { Console.WriteLine("An exception occurred: {0}", cex.Message); Console.Read(); host.Abort(); } } } }
2 客户端程序调用wcf,在配置文件中配置服务器的节点
1>新建“asp.net 空 web 应用程序”》在新建“wcf 服务”,直接将源码发布到iis中,
http://localhost/wcftest/MyFistService.svc?wsdl
2>在浏览器中打开以上地址,出现一下提示信息:
“若要测试此服务,需要创建一个客户端,并将其用于调用该服务。可以使用下列语法,从命令行中使用 svcutil.exe 工具来进行此操作“
svcutil.exe http://localhost/wcftest/MyFistService.svc?wsdl
3>安装2中的提示操作,在vs的命令窗口中执行以上操作,生成output.config和Service1.cs两个文件,将config中的配置节点服务到
自己的客户端程序中去,Service1.cs文件直接复制到客户端程序中。
程序员的基础教程:菜鸟程序员