WCF服务寄宿应用程序
1.先创建一个WCF服务库
2.创建一个Console控制台,服务将寄宿在该应用程序上,该程序一旦关闭,服务将停止。
控制台代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using WcfServiceLibrary; 6 using System.ServiceModel; 7 using System.ServiceModel.Description; 8 9 namespace wcfConsole 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //这是将来要引用的服务地址,由你自己定义 16 Uri address = new Uri("http://localhost:8123/ServiceDemo/Service"); 17 18 //MyService是你WCF服务库里面服务的名称 19 ServiceHost selfHost = new ServiceHost(typeof(MyService), address); 20 21 try 22 { 23 //IService是你服务库里面接口的名称 24 selfHost.AddServiceEndpoint(typeof(IService), new WSHttpBinding(), "MyService"); 25 26 //设置服务行为 27 ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 28 smb.HttpGetEnabled = true; 29 selfHost.Description.Behaviors.Add(smb); 30 31 //打开 ServiceHost 32 selfHost.Open(); 33 Console.WriteLine("success"); 34 Console.WriteLine(); 35 Console.ReadLine(); 36 37 // 关闭 ServiceHost 38 selfHost.Close(); 39 } 40 catch (CommunicationException ce) 41 { 42 Console.WriteLine("Error : {0}", ce.Message); 43 selfHost.Abort(); 44 } 45 } 46 } 47 }
也可以用 app.config 配置服务信息,代码要略作修改。
app.config内容:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <system.serviceModel> 4 <services> 5 <service name="WcfServiceLibrary.MyService" behaviorConfiguration="MyBehavior"> 6 <host> 7 <baseAddresses> 8 <add baseAddress="http://localhost:8123/ServiceDemo/Service" /> 9 </baseAddresses> 10 </host> 11 <endpoint address="MyService" binding="basicHttpBinding" 12 contract="WcfServiceLibrary.IService" /> 13 </service> 14 </services> 15 <behaviors> 16 <serviceBehaviors> 17 <behavior name="MyBehavior"> 18 <serviceMetadata httpGetEnabled="true" /> 19 </behavior> 20 </serviceBehaviors> 21 </behaviors> 22 </system.serviceModel> 23 </configuration>
service.name 是你WCF服务库里服务类的完全限定名
service.behaviorConfiguration 要与 behavior.name 相同
endpoint.contract 是你WCF服务库里接口的完全限定名
改动后的控制台代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using WcfServiceLibrary; 6 using System.ServiceModel; 7 using System.ServiceModel.Description; 8 9 namespace wcfConsole 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //这是将来要引用的服务地址,由你自己定义 16 //Uri address = new Uri("http://localhost:8123/ServiceDemo/Service"); 17 18 //MyService是你WCF服务库里面服务的名称 19 ServiceHost selfHost = new ServiceHost(typeof(MyService)); 20 21 try 22 { 23 ////IService是你服务库里面接口的名称 24 //selfHost.AddServiceEndpoint(typeof(IService), new WSHttpBinding(), "MyService"); 25 26 ////设置服务行为 27 //ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 28 //smb.HttpGetEnabled = true; 29 //selfHost.Description.Behaviors.Add(smb); 30 31 //打开 ServiceHost 32 selfHost.Open(); 33 Console.WriteLine("success"); 34 Console.WriteLine(); 35 Console.ReadLine(); 36 37 // 关闭 ServiceHost 38 selfHost.Close(); 39 } 40 catch (CommunicationException ce) 41 { 42 Console.WriteLine("Error : {0}", ce.Message); 43 selfHost.Abort(); 44 } 45 } 46 } 47 }
测试:
先运行控制台,然后新建项目添加服务引用:http://localhost:8123/ServiceDemo/Service
直接调用WCF服务库默认提供的方法:GetData()
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 9 namespace InvokeWcfServiceTest 10 { 11 public partial class _default : System.Web.UI.Page 12 { 13 protected void Page_Load(object sender, EventArgs e) 14 { 15 ServiceReference.ServiceClient client = new ServiceReference.ServiceClient(); 16 Response.Write(client.GetData(123789)); 17 Response.End(); 18 } 19 } 20 }
结果是可喜的。
!!!转发请注明出处和作者名称。
!!!转发请注明出处和作者名称。
!!!转发请注明出处和作者名称。
重要的事,说三遍。