Restful WCF开发实践 互动版

WCF例程的编译运行

我们使用mcs来编译程序,-target:library指定输出dll文件,-pkg:wcf指定编译wcf文件,-r引入编译时需要的库文件

  1. WcfService.Contract 的编译
~/demo/WcfService.Contract$ sudo mcs IEmployeeService.cs -target:library -pkg:wcf -r:System.ServiceModel -r:System.ServiceModel.Web  -r:System.Runtime.Serialization
~/demo/WcfService.Contract$ ls                                                                                  
IEmployeeService.cs  IEmployeeService.dll
  1. WcfService.Implementation 的编译
~/demo/WcfService.Implementation$ sudo mcs EmployeeService.cs  -target:library -pkg:wcf -r:System.ServiceModel -r:System.ServiceModel.Web -r:System.Runtime.Serialization -r:/home/user/demo/WcfService.Contract/IEmployeeService.dll     
~/demo/WcfService.Implementation$ ls                                                                            
EmployeeService.cs  EmployeeService.dll
  1. ServerConsole 的编译
~/demo/ServerConsole$ sudo mcs Program.cs -target:library -pkg:wcf -r:System.ServiceModel -r:System.ServiceModel.Web -r:System.Runtime.Serialization -r:/home/user/demo/WcfService.Contract/IEmployeeService.dll -r:/home/user/demo/WcfService.Implementation/EmployeeService.dll
~/demo/ServerConsole$ ls                                                                                        
App.config  Program.cs  Program.exe
  1. 配置、运行服务

拷贝dll到运行目录

~/demo/ServerConsole$ sudo cp -r /home/user/demo/WcfService.Contract/IEmployeeService.dll /home/user/demo/ServerConsole
~/demo/ServerConsole$ sudo cp -r /home/user/demo/WcfService.Implementation/EmployeeService.dll /home/user/demo/ServerConsole

修改App.config文件名为Program.exe.configmono中应用的默认配置名称); 由于系统仅对外开放80端口,这里需要确认先关闭apache,结束其对端口80的占用, 然后后台运行服务;

~/demo/ServerConsole$ sudo service apache2 stop
~/demo/ServerConsole$sudo mono Program.exe &

打开浏览器,输入http: //me.hubwiz.com/RestWcfService/employee/002,如果可以看到返回的json数据,说明服务运行成功