wcf简单使用小结

一、创建一个服务契约(即填写一些接口,不过要记得【ServiceContract】【OperationContract】)

二、创建一个服务来实现这个契约

三、通过自我寄宿的方式寄宿服务,在控制台应用程序的配置文件中填写

<?xml version="1.0" encoding="utf-8"?>

<configuration>

<system.serviceModel>

<services>

<service name="Service.CalculateService(要与服务中实现该服务契约的类相对应)" behaviorConfiguration="serviceBehavior">

<endpoint address="http://localhost:1000/calculateservice" binding="wsHttpBinding" contract="Service.ICalculateService(对应服务契约接口)"></endpoint>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior name="serviceBehavior(要与service中的behaviorConfiguration的值一样)">

<serviceDebug includeExceptionDetailInFaults="false"/>

<serciceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8888/calculateservice"/>

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

</configuration>

 

在宿主中还要打开服务

ServiceHost host1 = new ServiceHost(typeof(Service.PatientSer));

try

{

 host1.Opened += (sender, e) => { Console.WriteLine("服务启动"); };

}

catch(Exception e)

{

console.writeline(e.Message);

}

finally

{

host1.Close();

}

四、在客户端调用服务,添加服务引用指定源地址“http://localhost:8888/calculateservice”

Client.ServiceReference1.Patient patient=new Client.ServiceReference1.Patient();

通过patient就可以调用Patient的属性

Client.ServiceReference1.PatientClient pc = new Client.ServiceReference1.PatientClient();

通过pc调用方法

 

注意事项:

1、在客户端调用服务的时候,服务一定要启动

2、除客户端其他层用到都会引用Model层,Model层中的类一定要是可序列化的【DataContract】【DataMember】

3、各层之间的引用:DAL引用Model、Bll引用DAL和Model、Contract(契约类库)引用System.ServiceModel和Model、Service(实现契约类库)引用Bll和Contract和Model

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值