Master Data Service調用API創建Model

概述

這裏我們首先需要啓用MDS當中的Web Service服務,啓動方法見《啓用Master Data Services的Web Service》

添加服務引用

  • 解決方案資源管理器窗口中,右鍵單擊引用,單擊添加服務引用
  • 在地址,輸入的 URL 的 MDS 服務將"http:// <ServerName> / <MdsSiteName> / service/service.svc"。如果 MDS 位於的計算機上執行此操作,可以使用"localhost"作爲服務器名稱;
  • 單擊繼續。Visual Studio 會嘗試獲取服務並檢索 WSDL;
  • 如果成功,將看到服務IService在服務框中;
  • 該服務的Namespace框中指定命名空間。在此示例中,我將其命名爲MDService
  • 單擊高級按鈕,可以配置高級的設置;
  • 勾選Always generate message contracts(不確定中文版翻譯成什麼);
  • 設置集合類型下拉到System.Collections.ObjectModel.Collection
  • 單擊確定返回到添加服務引用對話框;

通過API創建模型

private static ServiceClient mdsProxy;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                mdsProxy = CreateMdsProxy("http://devserver:8080/Service/Service.svc");
            }
            catch (Exception ex)
            {
                Response.Redirect("Error connecting:" + ex.Message);
            }
        }

        private static ServiceClient CreateMdsProxy(string mdsURL)
        {
            System.ServiceModel.EndpointAddress endptAddress = new System.ServiceModel.EndpointAddress(mdsURL);
            System.ServiceModel.WSHttpBinding wsBinding = new System.ServiceModel.WSHttpBinding();
            return new ServiceClient(wsBinding, endptAddress);
        }

        private void CreateModel(string newModelName)
        {
            MetadataCreateRequest request = new MetadataCreateRequest();
            MetadataCreateResponse response = new MetadataCreateResponse();

            request.Metadata = new Metadata();
            request.Metadata.Models = new System.Collections.ObjectModel.Collection<Model>() { new Model() };
            request.Metadata.Models[0].Identifier = new Identifier();
            request.Metadata.Models[0].Identifier.Name = newModelName;

            response = mdsProxy.MetadataCreate(request);
        }

        protected void btnCreateModel_Click(object sender, EventArgs e)
        {
            CreateModel("TestModel");
        }
    }

注意:MDS的站點有緩存機制,如果你之前已經打開了該站點,則刷新之後不會看到新添加的那個Model,需要關閉瀏覽器或清理緩存。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章