Skip to content

Commit

Permalink
Fix for WCFCSTools issue - invalid namedPipe URL throws uncatchable e…
Browse files Browse the repository at this point in the history
…xception in Visual Studio. (#5604)
  • Loading branch information
imcarolwang authored Sep 11, 2024
1 parent f9f07b7 commit 3e24c11
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class NamedPipeMetadataImporter
const string UriSchemeNetPipe = "net.pipe";
const string NamedPipeBindingName = "MetadataExchangeNamedPipeBinding";
const string BindingNamespace = "http://schemas.microsoft.com/ws/2005/02/mex/bindings";
XmlReader _xmlReader;

#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
Expand All @@ -38,41 +37,65 @@ public async Task<XmlReader> GetMetadatadataAsync(Uri uri)
ChannelFactory<IMetadataExchange> factory = new ChannelFactory<IMetadataExchange>(CreateNamedPipeBinding(), new EndpointAddress(uri.AbsoluteUri));
IMetadataExchange proxy = factory.CreateChannel();
MessageVersion messageVersion = factory.Endpoint.Binding.MessageVersion;

var tcs = new TaskCompletionSource<XmlReader>();
try
{
var _message = Message.CreateMessage(messageVersion, WSTransfer.GetAction);
IAsyncResult result = proxy.BeginGet(_message, new AsyncCallback(RequestCallback), proxy);
IAsyncResult result = proxy.BeginGet(_message, new AsyncCallback(ar =>
{
try
{
RequestCallback(ar, tcs);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
}), proxy);

while (!result.IsCompleted)
{
await Task.Delay(100);
}
}
catch
catch (Exception ex)
{
((IClientChannel)proxy).Close();
tcs.SetException(ex);
}

finally
{
((IClientChannel)proxy).Abort();
}

return _xmlReader;
return await tcs.Task;
}

public void RequestCallback(IAsyncResult result)
public void RequestCallback(IAsyncResult result, TaskCompletionSource<XmlReader> tcs)
{
if (result.CompletedSynchronously)
return;

if (result.AsyncState is IMetadataExchange metadataClient)
{
Message response = metadataClient.EndGet(result);
if (!response.IsFault)
try
{
Message response = metadataClient.EndGet(result);
if (!response.IsFault)
{
XmlReader xmlReader = response.GetReaderAtBodyContents();
tcs.SetResult(xmlReader);
}
else
{
// Handle fault response
tcs.SetException(new Exception("Fault response received."));
}
}
catch (Exception ex)
{
_xmlReader = response.GetReaderAtBodyContents();
tcs.SetException(ex);
}
}
}
Expand Down
50 changes: 29 additions & 21 deletions src/dotnet-svcutil/lib/src/Metadata/ServiceDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public async Task ImportMetadataAsync(Action<WsdlImporter> onWsdlImporterCreated
//if it's net.pipe url
if (MetadataUrl != null && MetadataUrl.Scheme.Equals("net.pipe"))
{
string tfn;
if(OperationalCtx == OperationalContext.Infrastructure)
string tfn;
if (OperationalCtx == OperationalContext.Infrastructure)
{
tfn = "net462";
}
Expand All @@ -116,27 +116,35 @@ public async Task ImportMetadataAsync(Action<WsdlImporter> onWsdlImporterCreated
Type type = assembly.GetType("Microsoft.Tools.ServiceModel.Svcutil.NamedPipeMetadataImporter");
if (type != null)
{
object typeInstance = Activator.CreateInstance(type, null);
MethodInfo methodInfo = type.GetMethod("GetMetadatadataAsync", BindingFlags.Public | BindingFlags.Instance);
var xmlReader = await (Task<System.Xml.XmlReader>)methodInfo.Invoke(typeInstance, new object[] { MetadataUrl });
try
{
object typeInstance = Activator.CreateInstance(type, null);
MethodInfo methodInfo = type.GetMethod("GetMetadatadataAsync", BindingFlags.Public | BindingFlags.Instance);
var xmlReader = await (Task<System.Xml.XmlReader>)methodInfo.Invoke(typeInstance, new object[] { MetadataUrl });

if (xmlReader != null)
if (xmlReader != null)
{
Encoding encoding = Encoding.UTF8;
MemoryStream stream = new MemoryStream(encoding.GetBytes(xmlReader.ReadOuterXml()));
stream.Position = 0;
XmlReader reader = XmlDictionaryReader.CreateTextReader(
new MaxMessageSizeStream(stream, int.MaxValue),
Encoding.UTF8,
EncoderDefaults.ReaderQuotas,
null);

reader.Read();
reader.MoveToContent();

MetadataSet newSet = MetadataSet.ReadFrom(reader);
(this.metadataDocumentLoader.MetadataSections as List<MetadataSection>).AddRange(newSet.MetadataSections);
this.metadataDocumentLoader.State = MetadataDocumentLoader.LoadState.Successful;
}
}
catch
{
Encoding encoding = Encoding.UTF8;
MemoryStream stream = new MemoryStream(encoding.GetBytes(xmlReader.ReadOuterXml()));
stream.Position = 0;
XmlReader reader = XmlDictionaryReader.CreateTextReader(
new MaxMessageSizeStream(stream, int.MaxValue),
Encoding.UTF8,
EncoderDefaults.ReaderQuotas,
null);

reader.Read();
reader.MoveToContent();

MetadataSet newSet = MetadataSet.ReadFrom(reader);
(this.metadataDocumentLoader.MetadataSections as List<MetadataSection>).AddRange(newSet.MetadataSections);
this.metadataDocumentLoader.State = MetadataDocumentLoader.LoadState.Successful;
this.metadataDocumentLoader.State = MetadataDocumentLoader.LoadState.Failed;
throw;
}
}
}
Expand Down

0 comments on commit 3e24c11

Please sign in to comment.