wcf 有两种部署方法,一种是代码,一种是配置文件部署,今天主要记录一下配置文件部署,首先在需要部署的项目上创建配置文件,我用的winform,所以创建的是appconfig
里面最重要的添加的节点 services, 里面的service name属性需要指向执行契约的类,behaviorconfiguartion 无所谓,名字需要跟后面的behavior名字一样就行,baseaddress地址是自己填写的,可以http也可以tcp,对应的endpoint节点里的binding就要更改
如果是http开头的话,就是basichttpbinding,contract就是契约,指向的当然是契约接口。
原文地址:http://www.cnblogs.com/sixiangqimeng/p/3568651.html
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="winServer.myClass" behaviorConfiguration="testBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:8002/test"/> </baseAddresses> </host> <endpoint address="" binding="basicHttpBinding" contract="ClassLibrary1.myInterface"></endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name="testBehavior"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
这个是契约接口代码
[ServiceContract] public interface myInterface { [OperationContract] string getServerTime(); [OperationContract] DataTable getData(); }
这个是实现契约接口代码
public class myClass:ClassLibrary1.myInterface { public string getServerTime() { return DateTime.Now.ToString(); } public DataTable getData() { DataTable dt = new DataTable("books"); DataColumn dc1 = new DataColumn("书号"); DataColumn dc2 = new DataColumn("书名"); DataColumn dc3 = new DataColumn("价格"); dt.Columns.Add(dc1); dt.Columns.Add(dc2); dt.Columns.Add(dc3); DataRow dr = dt.NewRow(); dr["书号"] = "BF1001"; dr["书名"] = "WCF高级进阶"; dr["价格"] = "30"; dt.Rows.Add(dr); return dt; } }
用winform启用服务
ServiceHost host = null; private void button1_Click(object sender, EventArgs e) { host = new ServiceHost(typeof(winServer.myClass)); host.Open(); label1.Text = "服务已启动!"; }
启动服务后,到之前输入的地址,用ie打开http://localhost:8002/test,就会有wcf地址了,看到后你就明白了,就直接跟wcf用法一样,用的地直接添加服务引用就可以了