1 SOA : service oriented architecture 面向服务的架构
2 web service标准
3 概念理解图
4 WCF类库 项目的 wcf简单实现
首先创建一个简单的wcf类库 serviceLib:
其次创建一个控制台 作为宿主 寄宿类库
最后创建一个控制台作为客户端 通过引用服务的方式 引用远程的宿主
完成
5 详细代码:
lib:删除了复杂的东西 只留下了一个接口和对应的方法
接口:
using System.ServiceModel; namespace ServiceLib { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。 [ServiceContract] public interface IService1 { [OperationContract] string GetData(string value); } }
实现:
namespace ServiceLib { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。 public class Service1 : IService1 { public string GetData(string value) { return string.Format("You entered: {0}", value); } } }
控制台主方法中的代码
using ServiceLib; using System; using System.ServiceModel; //本来想给这个项目取名字叫做ServiceHost 结果跟我们的ServiceHost类冲突了 //所以在这里给项目取名ServiceConsole //1 引用System.ServiceModel //2 引用ServiceLib //3 将ServiceLib中的App.Config复制粘贴到这个项目中 修改其中的部分内容 //4 写代码 //5 以管理员身份启动 namespace ServiceConsole { class Program { static void Main(string[] args) { //此处的Service1来自ServiceLib ServiceHost host = new ServiceHost(typeof(Service1)); host.Open(); Console.WriteLine("服务已经启动"); Console.ReadKey(); } } }
App.Config修改一下的内容如下:
<services> <service name="ServiceLib.Service1"> <host> <baseAddresses> <!--<add baseAddress = "http://localhost:8733/Design_Time_Addresses/ServiceLib/Service1/" />被注释的原代码--> <add baseAddress = "http://localhost:8733/Service" /><!--这段代码是对上面一行的修改--> </baseAddresses> </host> <!-- Service Endpoints --> <!-- 除非完全限定,否则地址相对于上面提供的基址--> <endpoint address="" binding="basicHttpBinding" contract="ServiceLib.IService1"> <!-- 部署时,应删除或替换下列标识元素,以反映 用来运行所部署服务的标识。删除之后,WCF 将 自动推断相应标识。 --> <identity> <dns value="localhost"/> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 --> <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除--> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services>
以上就创建好了服务宿主 用管理员权限启动
客户端控制台重点:
a 按照服务宿主中的配置文件中的地址 引用服务:
b 主方法代码如下:
using System; namespace ServiceConsole_Client { class Program { static void Main(string[] args) { ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(); var str=client.GetData("露似真珠月似弓"); Console.WriteLine(str); Console.ReadKey(); } } }
OKAY 至此启动就可以了