打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
一个WCF的HelloWorld程序
要解决的问题:
WCF入门,创建一个HelloWorld程序。
解决方法:
打开VS,创建一个空白的solution。然后依次创建下面三个项目:
Contracts:类库项目,添加System.ServiceModel的引用。 Services:类库项目,添加System.ServiceModel和对Contracts项目的引用。 Host:控制台项目,田间System.ServiceModel和对Contracts、Services项目的引用。
在项目上右键,Add Reference->选择.NET标签页,再找到System.ServiceModel 添加。
效果如下图所示,并将Host项目设为启动项目。
2.在Contracts项目中定义服务契约接口。
IHello
namespace Contracts{[ServiceContract]public interface IHello{[OperationContract]void Hello();}}
3.在Services项目中实现Contracts项目中的接口。
HelloWorld实现IHello
namespace Services{public class HelloWorld : IHello{public void Hello(){Console.WriteLine(" Hello world");}}
4.在Host项目为WCF的服务提供运行环境。
HostApp
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using Services;using Contracts;using System.ServiceModel.Description;namespace Host{/// <summary>/// This is a Host Console application/// </summary>public class HostApp{static void Main(string[] args){/*** Create a host to provide service.* ServiceType is HelloWorld* BaseAddress is in localhost*/ServiceHost host = new ServiceHost(typeof(HelloWorld), new Uri("http://localhost:8080/HelloService"));/*** Add an serviceEndpoint to this host* Implemented Contract is IHello* Binding pattern is BasicHttpBinding* Address 'SVC' is a relative address*/host.AddServiceEndpoint(typeof(IHello), new BasicHttpBinding(),"SVC");if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null){ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();behavior.HttpGetEnabled = true;behavior.HttpGetUrl = new Uri("http://localhost:8080/HelloService");host.Description.Behaviors.Add(behavior);}/*** Once you add an endpoint to ServiceHost,it can create a EndpointListener for each ServiceEndpoint* ServiceDescription is involved in the creating process*/host.Open();Console.WriteLine("Start Your Service.");Console.ReadKey();host.Close();}}}
5.启动Host中的服务,也就是运行Host项目。出现下图效果,暂时先别关闭这个窗口。
6.在你的浏览器地址栏中,输入http://localhost:8080/HelloService出现效果如下:
7.点击页面中的链接,出现WSDL格式的XML文件:
8.经过上面这一系列,说明你的服务端已经OK了,现在进行客户端的配置。
另开一个solution,新建一个名为Client的控制台程序(什么应用程序都行)。添加对System.ServiceModel的引用。
接着,在Client项目上右键,Add Service Reference. 接着如下图:(你前面的Host项目请保持运行状态)
9,在Client端添加调用的Host服务的代码。代码里面IHello接口命名空间你可以在Client项目的Service Reference中找到,也就是你前一步输入的Namespace。
Client调用Hello服务
namespace Client{public class ClientApp{static void Main(String[] args){ServiceEndpoint httpEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IHello)),new BasicHttpBinding(), new EndpointAddress("http://localhost:8080/HelloService/SVC"));using (ChannelFactory<IHello> factory = new ChannelFactory<IHello>(httpEndpoint)){IHello service = factory.CreateChannel();service.Hello();}}}}
10. OK,现在你再运行你的Client项目。控制台下面会多出一行HelloWorld,至此一个最简单的WCF程序完成了。
工作原理:
这个程序中的配置,都是用代码写的,实际中应该在config文件里写。虽然效果是一样的,但代码需要再编译,所以XML好点。
具体我还是下一篇再写吧,不然太多了。
分类: WCF入门笔记
标签: WCF
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Windows Communication Foundation入门(Part Two) ...
WCF之旅(2):Endpoint Overview
使用一个窗体应用程序作为WCF服务的宿主
.Net下RabbitMQ的使用(9) -- 在WCF下使用RabbitMQ
WCF Tutorial
MyService WCF服务应用程序
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服