本文主要是介绍WCF基本概念以winfrom为宿主发布WCF,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.WCF的基本概念 (ABC)A 地址 Address 服务对外的地址 如: http://192.168.0.73:8888/WCFServer/UserService/ B 绑定 Binding 决定服务的细节 常用的有BasicHttpBinding 用http进行传输,数据格式为text/xmlWSHttpBinding 比BasicHttpBinding更加安全NetTcpBinding 最适合做跨机器的安全绑定C 契约 Contract 服务的定义(抽象) ,对应服务的接口 如: IUserServiceeg: WCF的一个配置(配置文件一般在新建contract时生成)<system.serviceModel><bindings /><client /><services><service name="WCFServer.Service1"><host><baseAddresses><add baseAddress="http://192.168.0.73:8888/WCFServer/Service1/" /></baseAddresses></host><endpoint address="" binding="wsHttpBinding" contract="WCFServer.IService1"></endpoint></service><service name="WCFServer.UserService"><host><baseAddresses><add baseAddress="http://192.168.0.73:8888/WCFServer/UserService/" /></baseAddresses></host><endpoint address="" binding="wsHttpBinding" contract="WCFServer.IUserService"></endpoint></service></services><behaviors><serviceBehaviors><behavior><!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 --><serviceMetadata httpGetEnabled="True"/><!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息--><serviceDebug includeExceptionDetailInFaults="False" /></behavior></serviceBehaviors></behaviors></system.serviceModel>2. 以winform程序作为WCF的契约宿主1. 新建一个WCF服务库项目,在里面可以新建契约(IUserService)和实现(UserService),新建完成后会在app.config中生成 WCF的配置文件。2. winfrom程序启动WCF(1)新建winfom项目namespace WCFServerHost
{public partial class Form1 : Form{private ServiceHost serviceHost = null;private ServiceHost userHost = null;public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){string btnText = button1.Text.Trim();if (btnText.Equals("启动服务")){if (null == serviceHost && null == userHost){serviceHost = new ServiceHost(typeof(WCFServer.Service1));userHost = new ServiceHost(typeof(WCFServer.UserService)); //定义了两个契约,都需要启动//绑定 BindingSystem.ServiceModel.Channels.Binding httpBinding = new BasicHttpBinding();//终结点 EndPoint serviceHost.AddServiceEndpoint(typeof(WCFServer.IService1), httpBinding, "http://192.168.0.73:8888/WCFServer/MyService1/");userHost.AddServiceEndpoint(typeof(WCFServer.IUserService), httpBinding, "http://192.168.0.73:8888/WCFServer/MyUserService/");serviceHost.Open(); //启动服务userHost.Open();button1.Text = "停止服务";lblResult.Text = "服务已经启动";}}else {serviceHost.Close();userHost.Close();serviceHost = null;userHost = null;button1.Text = "启动服务";lblResult.Text = "服务已经停止";}}}
}(2) 将WCF服务库中的app.config 拷贝到winform项目下,并将WCF服务库中的app.config配置文件删除(3) 运行winFrom工程下的 bin/debug 下面的 exe(这一步是为了生成客户端代理,需要启动Host) 3 客户端调用, 新建一个控制台程序,添加服务引用 ,如:http://192.168.0.73:8888/WCFServer/Service1/调用如下:namespace WCFClient
{class Program{static void Main(string[] args){WCFServer1.IService1 wcfServerClient = new WCFServer1.Service1Client("BasicHttpBinding_IService1");string result = wcfServerClient.GetData(123);Console.WriteLine(result);UserService.IUserService userServiceClient = new UserService.UserServiceClient("BasicHttpBinding_IUserService");int userId = 0 ;int responseId = userServiceClient.CheckUser(out userId, "test", "123456");if(responseId==1){Console.WriteLine(userId);}Console.ReadLine();}}
}
这篇关于WCF基本概念以winfrom为宿主发布WCF的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!