4 changed files with 191 additions and 52 deletions
@ -0,0 +1,90 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.ComponentModel; |
||||||
|
using System.ServiceModel.Description; |
||||||
|
|
||||||
|
namespace ICSharpCode.SharpDevelop.Gui |
||||||
|
{ |
||||||
|
public class ServiceReferenceDiscoveryClient |
||||||
|
{ |
||||||
|
const int DiscoveryClientsUsed = 2; |
||||||
|
|
||||||
|
Uri discoveryUrl; |
||||||
|
BackgroundWorker mexDiscoveryBackgroundWorker = new BackgroundWorker(); |
||||||
|
BackgroundWorker mexRelativePathDiscoveryBackgroundWorker = new BackgroundWorker(); |
||||||
|
List<Exception> errors = new List<Exception>(); |
||||||
|
|
||||||
|
public ServiceReferenceDiscoveryClient() |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
public event EventHandler<ServiceReferenceDiscoveryEventArgs> DiscoveryComplete; |
||||||
|
|
||||||
|
protected virtual void OnDiscoveryComplete(ServiceReferenceDiscoveryEventArgs e) |
||||||
|
{ |
||||||
|
if (DiscoveryComplete != null) { |
||||||
|
DiscoveryComplete(this, e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void Discover(Uri url) |
||||||
|
{ |
||||||
|
this.discoveryUrl = url; |
||||||
|
DiscoverMexMetadata(); |
||||||
|
} |
||||||
|
|
||||||
|
void DiscoverMexMetadata() |
||||||
|
{ |
||||||
|
DiscoverMexMetadata(mexDiscoveryBackgroundWorker, discoveryUrl); |
||||||
|
DiscoverMexMetadata(mexRelativePathDiscoveryBackgroundWorker, GetRelativeUrl("mex")); |
||||||
|
} |
||||||
|
|
||||||
|
Uri GetRelativeUrl(string relativeUrl) |
||||||
|
{ |
||||||
|
return new Uri(GetDiscoveryUrlWithTrailingSlash(), relativeUrl); |
||||||
|
} |
||||||
|
|
||||||
|
Uri GetDiscoveryUrlWithTrailingSlash() |
||||||
|
{ |
||||||
|
if (discoveryUrl.AbsoluteUri.EndsWith("/")) { |
||||||
|
return discoveryUrl; |
||||||
|
} |
||||||
|
return new Uri(discoveryUrl.AbsoluteUri + "/"); |
||||||
|
} |
||||||
|
|
||||||
|
void DiscoverMexMetadata(BackgroundWorker worker, Uri url) |
||||||
|
{ |
||||||
|
worker.DoWork += DiscoverMexMetadata; |
||||||
|
worker.RunWorkerCompleted += DiscoveryCompleted; |
||||||
|
worker.RunWorkerAsync(url); |
||||||
|
} |
||||||
|
|
||||||
|
void DiscoverMexMetadata(object sender, DoWorkEventArgs e) |
||||||
|
{ |
||||||
|
Uri url = (Uri)e.Argument; |
||||||
|
var client = new MetadataExchangeClient(url, MetadataExchangeClientMode.MetadataExchange); |
||||||
|
MetadataSet metadata = client.GetMetadata(); |
||||||
|
e.Result = new ServiceReferenceDiscoveryEventArgs(metadata); |
||||||
|
} |
||||||
|
|
||||||
|
void DiscoveryCompleted(object sender, RunWorkerCompletedEventArgs e) |
||||||
|
{ |
||||||
|
if (e.Error != null) { |
||||||
|
OnDiscoveryError(e.Error); |
||||||
|
} else { |
||||||
|
OnDiscoveryComplete((ServiceReferenceDiscoveryEventArgs)e.Result); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void OnDiscoveryError(Exception ex) |
||||||
|
{ |
||||||
|
errors.Add(ex); |
||||||
|
if (errors.Count == DiscoveryClientsUsed) { |
||||||
|
OnDiscoveryComplete(new ServiceReferenceDiscoveryEventArgs(errors)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,57 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.ServiceModel.Description; |
||||||
|
using System.Text; |
||||||
|
using System.Web.Services.Description; |
||||||
|
using WebServices = System.Web.Services.Description; |
||||||
|
|
||||||
|
namespace ICSharpCode.SharpDevelop.Gui |
||||||
|
{ |
||||||
|
public class ServiceReferenceDiscoveryEventArgs : EventArgs |
||||||
|
{ |
||||||
|
ServiceDescriptionCollection services = new ServiceDescriptionCollection(); |
||||||
|
|
||||||
|
public ServiceReferenceDiscoveryEventArgs(IEnumerable<Exception> errors) |
||||||
|
{ |
||||||
|
GenerateAggregateError(errors); |
||||||
|
} |
||||||
|
|
||||||
|
void GenerateAggregateError(IEnumerable<Exception> errors) |
||||||
|
{ |
||||||
|
var message = new StringBuilder(); |
||||||
|
foreach (Exception ex in errors) { |
||||||
|
message.AppendLine(ex.Message); |
||||||
|
message.AppendLine(); |
||||||
|
} |
||||||
|
Error = new AggregateException(message.ToString(), errors); |
||||||
|
} |
||||||
|
|
||||||
|
public ServiceReferenceDiscoveryEventArgs(MetadataSet metadata) |
||||||
|
{ |
||||||
|
GetServices(metadata); |
||||||
|
} |
||||||
|
|
||||||
|
void GetServices(MetadataSet metadata) |
||||||
|
{ |
||||||
|
foreach (MetadataSection section in metadata.MetadataSections) { |
||||||
|
var service = section.Metadata as WebServices.ServiceDescription; |
||||||
|
if (service != null) { |
||||||
|
Services.Add(service); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public ServiceDescriptionCollection Services { |
||||||
|
get { return services; } |
||||||
|
} |
||||||
|
|
||||||
|
public Exception Error { get; private set; } |
||||||
|
|
||||||
|
public bool HasError { |
||||||
|
get { return Error != null; } |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue