string assemblyFile = "C:\\temp\\WebServiceLibrary.dll";
string serviceClassName = "WebServiceLibrary.ClientPortalSvc";
string outFile = "c:\\temp\\test1.txt";
// Load the assembly from file
// Note: Assembly will be locked
Assembly assembly = null;
try
{
assembly = Assembly.LoadFile(assemblyFile);
}
catch (Exception ex)
{
Console.WriteLine("Error loading file " + ex.Message);
return;
}
Type[] types = assembly.GetTypes();
Type referencedType = null;
foreach (Type typ in types)
{
if (typ.FullName.Equals(serviceClassName))
{
// Found the requested type
referencedType = typ;
}
}
if (referencedType != null)
{
var reflector = new ServiceDescriptionReflector();
reflector.Reflect(referencedType, "http://localhost/temp.asmx");
using (var ms = new MemoryStream())
{
var wtr = new XmlTextWriter(ms, Encoding.ASCII);
wtr.Formatting = Formatting.Indented;
// Add the SP imports and start element
wtr.WriteStartElement("%@ Assembly Name=\"Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c\" %>\r\n" +
"<%@ Import Namespace=\"Microsoft.SharePoint.Utilities\" %>\r\n" +
"<%@ Import Namespace=\"Microsoft.SharePoint\" %>\r\n" +
"<% Response.ContentType = \"text/xml\"; %");
reflector.ServiceDescriptions[0].Write(wtr);
// Read the XML into a string
ms.Position = 0;
var reader = new StreamReader(ms);
var xmlString = reader.ReadToEnd();
ms.Position = 0;
wtr.Close();
// Rewrite the soap address for SP. This will allow the soap:address to be
// correct regardless of service name or request location
string spLocation =
" location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request))" +
",Response.Output); %> />";
xmlString = Regex.Replace(xmlString, "\\<soap\\:address.*", "<soap:address" + spLocation);
xmlString = Regex.Replace(xmlString, "\\<soap12\\:address.*", "<soap12:address" + spLocation);
// Out to a file
using (StreamWriter fileStream = new StreamWriter(outFile))
{
fileStream.Write(xmlString);
}
}
}
else
{
throw new Exception("The " + serviceClassName + " was not found in " + assemblyFile);
}
}