WCF服務(wù)啟動(dòng)特殊方法分享
作者:佚名
我們在這篇文章中為大家介紹的WCF服務(wù)啟動(dòng)的方法,主要就是不通過配置文件來進(jìn)行啟動(dòng)。那么具體的操作方法將會(huì)在這里做一個(gè)詳細(xì)介紹。
對于WCF服務(wù)啟動(dòng)的方法,我們可以通過多種方式來實(shí)現(xiàn)。在這里我們將會(huì)通過一種特殊的方法來實(shí)現(xiàn)WCF服務(wù)啟動(dòng)。大家首先可以通過一段代碼示例來詳細(xì)分析一下這一服務(wù)的啟動(dòng)方法,以此加深這方面的印象。
WCF服務(wù)啟動(dòng)代碼示例:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- using System.ServiceModel.Description;
- namespace WCF1
- {
- [ServiceContract(Name = "MyService", Namespace =
"http://www.huisoftware.com")]- public class MyService
- {
- [OperationContract]
- public string MyMethod(string str)
- {
- return str + "Server Hello World";
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- using (ServiceHost host = new ServiceHost(typeof(MyService)))
- {
- host.AddServiceEndpoint(typeof(MyService), new WSHttpBinding(),
"http://127.0.0.1:8080/MyService");- if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
- {
- ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
- behavior.HttpGetEnabled = true;
- behavior.HttpGetUrl = new Uri("http://127.0.0.1:8080
/MyService/metadata");- host.Description.Behaviors.Add(behavior);
- }
- host.Opened += delegate
- {
- Console.WriteLine("WCF服務(wù)已經(jīng)啟動(dòng)");
- };
- host.Open();
- Console.Read();
- }
- }
- }
- }
WCF服務(wù)啟動(dòng)的相關(guān)代碼就為大家介紹到這里。
【編輯推薦】
責(zé)任編輯:曹凱
來源:
博客園