Scheming for Method Schemas

Eric did a great post about how to retrieve the thing-type schemas programmatically. This code shows how to retrieve the methods reponse an request programmatically.

The gist is in the method call GetServiceDefinition, this call returns the list of method and only can work on the methods to retrieve the urls for the schema’s for that version’s request and response.

    protected void Page_Load(object sender, EventArgs e)
    {
        ApplicationConnection connection = base.DictionaryConnection;
        ServiceInfo serviceInfo = connection.GetServiceDefinition();
        List methodVersions =
            new List(
                serviceInfo.Methods.Count);
        foreach (HealthServiceMethodInfo method in serviceInfo.Methods)
        {
            SaveMethodXSD(method);
        }
    }

    void SaveMethodXSD(HealthServiceMethodInfo methods)
    {
        string directoryName = MapPath("platform");
        Directory.CreateDirectory(directoryName).CreateSubdirectory("web").CreateSubdirectory("methods");

        foreach (HealthServiceMethodVersionInfo method in methods.Versions)
        {
            if (!String.IsNullOrEmpty(method.RequestFileName))
            {
                SaveFile(method.RequestSchemaUrl.ToString(), method.RequestFileName);
            }
            if (!String.IsNullOrEmpty(method.ResponseFileName))
            {
                SaveFile(method.ResponseSchemaUrl.ToString(), method.ResponseFileName);
            }
        }
    }

    void SaveFile(string schemaUri, string name)
    {
        string filename = MapPath(@"platformwebmethods" + name + ".xsd");
        using (StreamWriter writer = System.IO.File.CreateText(filename))
        {
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(schemaUri);
            using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
            {
                using (StreamReader httpResponseStream = new StreamReader(httpResponse.GetResponseStream()))
                {
                    writer.Write(httpResponseStream.ReadToEnd());
                }
            }
        }
    }

Note: the above snipped tries to save the schemas to a directory named platformwebmethods.
PLEASE NOTE: By blogging software is capitalizing the code in a weird way. I dont know what’s up with dpSyntaxHighligher.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s