« Battery Conscious: Using your GPS with Android | Main | Debugging JavaScript in IE8 with Developer Tools »
Thursday
Feb042010

Creating a static WSDL through reflection

If you are deploying custom services to a SharePoint site you are accustom to the following steps:

1. Deploy the signed library to the GAC.

2. Deploy the asmx file to \12\TEMPLATE\LAYOUTS\

3. Run disco.exe to generate a disco wsdl file.

4. Copy the asmx file to 12\ISAPI.

5. Modify the disco and wsdl file, rename them, and copy them to the 12\ISAPI

For more info on that read Creating a Custom SharePoint Web Service

To speed up your deployment the following code will open up the service dll, reflect it, and modify the attributes for use in SharePoint. This can be run as a post-build event so the output from your builds are the actual deployable files.

 

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);
}
}

 

Download:

Reader Comments (7)

Thanks for taking the time to talk about this, I feel fervently about this and I take pleasure in learning about this topic. Please, as you gain information, please update this blog with more information. I have found it very useful. Hermes mens belts

November 24, 2010 | Unregistered Commenterhappyttyy

Your article is nice, I read your article to learn a lot and hope to see your next article, look forward to your masterpiece, you can also see our mens jewelry crosses
information, I hope it can give you You some convenient.

June 9, 2011 | Unregistered CommenterKaya Craig

(Dale) No one indebted for others, while many people don't know how to cherish others.-fendi replica handbags

October 26, 2011 | Unregistered Commenterrossvince21202

You've wonderfully written the post. I've liked your way of penning this. Thanks for sharing this.

Houton Home Security

October 26, 2011 | Unregistered CommenterHouston Home Security

I truly love rookie blue season 2 dvd follows the lives of five rookie cops from 15 Division who have just graduated from the academy. all of the tough effort you have devoted to keeping sons of anarchy season 3 dvd for every one and good wife season 2 dvd give us a surprise!

October 27, 2011 | Unregistered CommenterQUALITY DVD

So now New Nike Shoes On Salethat Nike Jordan 18we

have talked about what chronic pain and chronic Hot Nike Shoes On Saleillness are, how do these things effect a person’s mental

health? The Retro Jordan 2011simple truth is that chronic pain and chronic illness can be extremely

detrimental to an individuals mental Buy Discount Vibrams Five Fingers Shoeshealth. The experience of feeling constant pain

and/or experiencing fatigue, exhaustion, and lifestyle Vibram FiveFingers KSO Treklimitations

can be demoralizing and isolating to Discount Nike Shoes Onlinethe individual suffering from the pain or illness. It is common

for patients to develop depressiveNike Jordan AJF4 symptoms and begin to view themselves as less

fully Cheap Nike Retro Jordanhuman new nike shoesand whole than they did

before the pain or illness Jordan CMFT Max 12 leatheroccurred. This occurs because they

areAir Jordan 1 often times UGG Bootsless or unable

to complete meaningful work or Adironadack UGG Boots home-related tasks and engage in social

interactions as they once did.

November 17, 2011 | Unregistered Commenteralong

Your website have very interesting article. I got knowledge from here. Besides that, your blog is so popular among the searchers from search engines. It means yours website is very goodAlarm Monitoring Houston [url=http://www.smithmonitoring.com/houston-security/]Alarm Monitoring Houston[/url]

November 27, 2011 | Unregistered CommenterAlarm Monitoring Houston

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>