一、创建一个服务契约(即填写一些接口,不过要记得【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