wcf 学习程序

本文详细介绍了如何使用Visual Studio创建WCF服务类库、创建WCF主机、部署到IIS,以及通过Windows Form调用WCF服务的过程。

(一)创建WCF Service

(1)创建WCF Service类库

 

创建一个Class Library的项目:

image

 

删除掉默认的Class1.cs文件,然后添加一个WCF Service项目:

image

 

Visual Studio会自动帮助你生成两个文件:HelloService.cs 和 IHelloService.cs,另外还自动添加了System.ServiceModel引用,它是WCF的核心。

image

 

 

修改IHelloService.cs和HelloService.cs文件。

IHelloService.cs:

 

复制代码
namespace HelloService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IHelloService" in both code and config file together.
    [ServiceContract]
    public interface IHelloService { [OperationContract] string GetMessage(string name); } }
复制代码

 

HelloService.cs:

 

复制代码
namespace HelloService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "HelloService" in both code and config file together.
    public class HelloService : IHelloService { public string GetMessage(string name) { return "Hello " + name; } } }
复制代码

 

(2)创建WCF的Host

添加一个新的ASP.NET Empty Web Application:

image

 

添加一个新Item WCF Service

image

image

 

删除HelloService.svc.cs和IHelloService.cs文件。

添加HelloService Class Library的项目引用:

 

image

 

修改HelloService.svc为:

<%@ ServiceHost Language="C#" Debug="true" Service="HelloService.HelloService" %>

  

Web.config:

 

复制代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration> <system.serviceModel> <services> <service name="HelloService.HelloService" behaviorConfiguration="metaBehavior"> <endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService"></endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint> <host> <baseAddresses> <add baseAddress="http://localhost:8080"/> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name="metaBehavior"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
复制代码

 

其中,service name=”命名空间.类名”,behaviorConfiguration是用来关联下面behavior的定义的。

endpoint address中定义的是相对地址,与baseAddress结合起来为完整地址

endpoint contract=”命名空间.接口名”

两个endpoint,第一个binding是basicHttpBinding,用于HTTP协议;第二个endpoint用于交换metadata,binding为mexHttpBinding。

其中behavior的定义是用来允许交换metadata的。

 

Build解决方案,如果没有错误就进行到下一步,部署WCF Service到IIS

 

(二)部署WCF Service到IIS

(1)Publish HelloServiceIISHost项目

image

image

image

image

image

image

 

(2)部署到IIS

image

image

image

 

浏览HelloService.svc

image

image

 

 

(三)创建一个Windows Form来调用WCF Service

image

 

添加一个服务引用:

image

image

image

image

 

private void button1_Click(object sender, EventArgs e) 
{ 
    HelloService.HelloServiceClient client = new HelloService.HelloServiceClient(); label1.Text = client.GetMessage(textBox1.Text); } 

 

运行代码,效果如下:

image

 

(四)总结

svc文件中,包含着服务指令,Service属性指明文件指向的是哪个服务

<%@ ServiceHost Language="C#" Debug="true" Service="HelloService.HelloService" %>

 

service的代码可以在

(1) XXX.svc.cs的文件中

(2) 一个独立的Assembly(如同本文)

(3) App_Code文件夹下

转载于:https://www.cnblogs.com/highyunxiao/p/4059390.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值