سؤال

For the following class :

public class MyClass
{
   private WebServiceHost m_WebServiceHost;
}

I need to trace the uri it was initialized with. I have implemented that method:

public void MyTrace()
{
    Trace.TraceInformation("URI {0}",m_WebServiceHost.BaseAddresses);
}

But I get :

URI System.Collections.ObjectModel.ReadOnlyCollection`1[System.Uri]

What is wrong?

هل كانت مفيدة؟

المحلول

Well, WebServiceHost.BaseAddresses is a collection, not a single object. So using .ToString() will just return the classname not the value. You just need to enumerate the collection in some way first, eg foreach would do the trick. Each base address is a Uri, so we can use the AbsoluteUri property to get the string representation:

public void MyTrace()
{
    string addresses = string.Empty;
    foreach (var address in m_WebServiceHost.BaseAddresses)
        addresses += address.AbsoluteUri;
    Trace.TraceInformation("URI {0}", addresses);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top