2014-11-26 16 views
5

Gereksinim: Bir dosya paylaşımı konumdan WSDL dosyasını almak çalışma zamanı yani en SABUN Web hizmetinin WSDL vermektüketin ve Runtime de SABUN WebServices Çağır - Dinamik Web Servis istemcisi WSDL dosyasından

  1. Müşteri.
  2. WSDL'yi tüketin ve Müşteri tarafından UI'de seçilen Yöntemi çağırın ve yanıtı işleyin.

WSDL barındırılmadığı için MetadataExchangeClient kullanamıyorum.

Uygulama:

var serviceDescription = ServiceDescription.Read(@"C:\Contacts.WSDL"); 
var metadataSection = new MetadataSection 
{ 
Dialect = MetadataSection.ServiceDescriptionDialect, 
Identifier = serviceDescription.TargetNamespace, 
Metadata = serviceDescription 
}; 

var metadataSections = new List<MetadataSection> {metadataSection}; 
var metadatSet = new MetadataSet(metadataSections); 
var wsdlImporter = new WsdlImporter(metadatSet); 
var services = wsdlImporter.ImportAllEndpoints(); 

Yol Blokları:

  1. Yukarıdaki kod hiç hizmet uç noktalarını çıkaramamıştır. Yani, bir servis bitiş noktasını el ile oluşturmam gerekiyordu.
  2. Bütün yukarıda WSDL içerdiği yöntem ve ilgili girişler üzerinden liste olamazdı/çıkışa (değişken operationName ve aşağıdaki operationParameters kullanılmak üzere)
object retVal = instance.GetType().GetMethod(operationName) 
         .Invoke(instance, operationParameters); // Invoke 

WSDL'den elle ayrıştırılan işlem adını kodlayarak zorlamayı denedim, ancak sonra parametrelerde başarısız oldu. Bu aşağıdaki gibi hiyerarşi içeren karmaşık bir tür bekliyor: -> ListOfContacts -> İletişim -

ContactInput> ad, soyadı

Sonraki Adımlar:

birisi Yapabilseydim Yol engellerini düzeltmeme yardım et, sonra yukarıdaki yaklaşıma devam edebilirim. Else

, ben zamanında

Teşekkür, Dev svcutil.exe kullanımıyla ilgili araştırma başlamak zorunda

bu bu makalede açıklanan yapmak için bir çözüm var

cevap

4

:

Generate proxy code for a web service dynamically

Bu bağlantıyı açıp okuyabilseniz de, kodu her zaman girdiğimde buraya ekleyebilirim:

Bu yöntem, web hizmeti tarafından gösterilen işlemlerin listesini döndürür. Ben elde edilen assmebly sadece web yöntemi isimlerini yansıtmak mümkün değildi olarak bunu elde etmek için ServiceDescription kullanmış. Mevcut işlem adlarıyla, geriye kalan her şey, her bir yöntem için girdi ve dönüş parametrelerini bulmaktır.

public string[] GenerateProxyAssembly() 
{ 
    //create a WebRequest object and fetch the WSDL file for the web service 
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(this.uri); 
    request.Credentials = CredentialCache.DefaultCredentials; 
    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
    System.IO.Stream stream = response.GetResponseStream(); 

    //read the downloaded WSDL file 
    ServiceDescription desc = ServiceDescription.Read(stream); 

    //find out the number of operations exposed by the web service 
    //store the name of the operations inside the string array 
    //iterating only through the first binding exposed as 
    //the rest of the bindings will have the same number 
    int i = 0; 
    Binding binding = desc.Bindings[0]; 
    OperationBindingCollection opColl = binding.Operations; 
    foreach (OperationBinding operation in opColl) 
    { 
    listOfOperations[i++] = operation.Name; 
    } 

    //initializing a ServiceDescriptionImporter object 
    ServiceDescriptionImporter importer = new ServiceDescriptionImporter(); 

    //set the protocol to SOAP 1.1 
    importer.ProtocolName = "Soap12"; 

    //setting the Style to Client in order to generate client proxy code 
    importer.Style = ServiceDescriptionImportStyle.Client; 

    //adding the ServiceDescription to the Importer object 
    importer.AddServiceDescription(desc, null, null); 
    importer.CodeGenerationOptions = CodeGenerationOptions.GenerateNewAsync; 

    //Initialize the CODE DOM tree in which we will import the 
    //ServiceDescriptionImporter 
    CodeNamespace nm = new CodeNamespace(); 
    CodeCompileUnit unit = new CodeCompileUnit(); 
    unit.Namespaces.Add(nm); 

    //generating the client proxy code 
    ServiceDescriptionImportWarnings warnings = importer.Import(nm, unit); 

    if (warnings == 0) 
    { 
    //set the CodeDOMProvider to C# to generate the code in C# 
    System.IO.StringWriter sw = new System.IO.StringWriter(); 
    CodeDomProvider provider = CodeDomProvider.CreateProvider("C#"); 
    provider.GenerateCodeFromCompileUnit(unit, sw, new CodeGeneratorOptions()); 

    //creating TempFileCollection 
    //the path of the temp folder is hardcoded 
    TempFileCollection coll = new TempFileCollection(@"C:\wmpub\tempFiles"); 
    coll.KeepFiles = false; 

    //setting the CompilerParameters for the temporary assembly 
    string[] refAssembly = { "System.dll", "System.Data.dll", 
     "System.Web.Services.dll", "System.Xml.dll" }; 
    CompilerParameters param = new CompilerParameters(refAssembly); 
    param.GenerateInMemory = true; 
    param.TreatWarningsAsErrors = false; 
    param.OutputAssembly = "WebServiceReflector.dll"; 
    param.TempFiles = coll; 

    //compile the generated code into an assembly 
    //CompilerResults results = provider.CompileAssemblyFromDom(param, unitArr); 
    CompilerResults results 
     = provider.CompileAssemblyFromSource(param, sw.ToString()); 
    this.assem = results.CompiledAssembly; 
    } 

    //return the list of operations exposed by the web service 
    return listOfOperations; 
} 

Bu yöntem Parameterınfo [] listesinde giriş parametrelerinin döndürür. Çıkış parametresini almak için, aramayı GetParamters() metodunda GetParameter özelliği ile değiştirin ve bunu yeni bir yönteme yerleştirin. Bu 3 yöntemi bir dll içine koyun ve herhangi bir istemci uygulamasından ona bir referans ekleyin. Bu kadar. Sadece URL'yi sağlayın ve web servisini, herhangi bir web servisini tüketin. Yeni bir web servisi kullanmak istediğiniz her seferinde bir proxy dosyası oluşturma prosedürüne girmeniz gerekmez. işaretçileri için

public ParameterInfo[] ReturnInputParameters(string methodName) 
{ 
    //create an instance of the web service type 
    //////////////to do///////////////////////// 
    //get the name of the web service dynamically from the wsdl 
    Object o = this.assem.CreateInstance("Service"); 
    Type service = o.GetType(); 
    ParameterInfo[] paramArr = null; 

    //get the list of all public methods available in the generated //assembly 
    MethodInfo[] infoArr = service.GetMethods(); 

    foreach (MethodInfo info in infoArr) 
    { 
    //get the input parameter information for the 
    //required web method 
    if (methodName.Equals(info.Name)) 
    { 
     paramArr = info.GetParameters(); 
    } 
    } 

    return paramArr; 
} 
+0

teşekkürler. Bunu dener ve benim için çalışırsa günceller. – Dev