2017-01-02 100 views
0

我有一個WCF服務,其類實現了IContractBehaviorIWsdlExportExtension,該類使用用戶允許的操作生成WSDL,並排除他們無權訪問的操作和類型。將參數傳遞到WCF元數據

但是這個限制是,對於每個用戶,我必須手動更改我爲其生成WSDL的用戶。

我想通過作爲元數據請求的一部分傳入用戶來解決此限制。

  • localhost/service.svc?user=me
  • localhost:9766/service.svc?singleWsdl&user=me

我也可以用svcutil也只要所產生的WSDL是扁平的工作。

回答

0

我能得到這個做的工作如下

代碼

const string BASE_ADDRESS = 
"http://localhost:8731/Design_Time_Addresses/CalcService"; 
var uri = new Uri(BASE_ADDRESS); 
var user = "userName"; 

using (var serviceHost = new ServiceHost(typeof(Calc), uri)) 
{ 
    var exporter = new WsdlExporter(); 
    var endpoint = serviceHost.AddServiceEndpoint(typeof(ICalc), 
    new BasicHttpBinding(), ""); 
    endpoint.Contract.Behaviors.Add(new 
    RestrictedOperationsWsdlExportExtensionAttribute(user)); 
    serviceHost.Open(); 
    Console.WriteLine("The service is ready: " + user); 
    exporter.ExportEndpoint(endpoint); 
    if (exporter.Errors.Count == 0) 
    { 
    var metadataSet = exporter.GetGeneratedMetadata(); 
    var asy= Assembly.GetAssembly(typeof(WsdlExporter)); 
    var t = asy.GetType("System.ServiceModel.Description.WsdlHelper", 
     true); 
    var method = t.GetMethod("GetSingleWsdl", 
     System.Reflection.BindingFlags.Public 
     | System.Reflection.BindingFlags.Static); 
    var serviceDescription = 
     method.Invoke(null, new object[] {metadataSet}) 
     as System.Web.Services.Description.ServiceDescription; 
    if (serviceDescription != null) 
    { 
     serviceDescription.Name = "Calc"; 
     serviceDescription.Write(user + ".wsdl"); 
    } 
    } 
}