以程式設計方式將後設資料端點新增到服務
如果你還希望在沒有配置檔案的情況下公開後設資料,則可以通過程式設計方式建立 ServiceHost 來構建示例:
public ConsoleHost()
{
    mHost = new ServiceHost(typeof(Example), new Uri("http://localhost:8000/Example"), new Uri("net.tcp://9000/Example"));
    NetTcpBinding tcp = new NetTcpBinding();
    mHost.AddServiceEndpoint(typeof(IExample), tcp, "net.tcp://localhost:9000/Example");            
    ServiceMetadataBehavior metaBehavior = mHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
    if (metaBehavior == null)
    {
        metaBehavior = new ServiceMetadataBehavior();
        metaBehavior.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
        metaBehavior.HttpGetEnabled = true;
        mHost.Description.Behaviors.Add(metaBehavior);
        mHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
    }
    mHost.Open();
}
- 建立一個 ServiceHost 例項,傳遞具體的類型別和零個或多個 baseaddress Uri。
- 當你使用 mexHttpBinding 時,你必須新增 http:// localhost:8000 /示例 baseaddress
- 在這種情況下構造所需的繫結 NetTcpBinding。
- 呼叫 AddServiceEndpoint 傳遞 Address,Binding 和 Contract。 (ABC)。
- 構造 ServiceMetadataBehavior
- 將 HttpGetEnabled 設定為 true
- 將後設資料行為新增到 behavior 集合。
- 呼叫 AddServiceEndpoint 傳遞常量以進行後設資料交換
- 開啟主機。