WCF 服務可以通過兩種途徑發佈他們的元數據,這些元數據可以通過 HTTP-GET 傳送或者直接通過 Endpoint 發佈。如果通過 HTTP-GET 來發布元數據,你所要做的只是在配置文件中爲 service 節點添加 behaviorConfiguration 屬性並激活 HTTP-GET 而已。(完整的配置文件樣例可以參考 http://www.cnblogs.com/anders-x-hu/archive/2007/05/30/765449.html )
編程控制元數據
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
namespace Anrs.Service
{
class Program
{
static void Main( string [] args)
{
ServiceHost sh = new ServiceHost( typeof (AnrsService));
ServiceMetadataBehavior metadata = sh.Description.Behaviors.Find < ServiceMetadataBehavior > ();
Binding wsHttpBinding = new WSHttpBinding();
if (metadata == null )
{
metadata = new ServiceMetadataBehavior();
metadata.HttpGetEnabled = true ;
sh.Description.Behaviors.Add(metadata);
}
sh.AddServiceEndpoint( typeof (IAnrsServiceContract1), wsHttpBinding, new Uri( " http://localhost:8086/AnrsService/ " ));
sh.Open();
Console.Write( " Press any key to exit " );
Console.ReadLine();
sh.Close();
}
}
}
再次證明 WCF 並不是 Programming 的創新,更多的是概念方面的創新。