10.4 Web Services from an ASP.NET Server

Accessing a web service from Flash using Flash Remoting on an ASP.NET server is almost as easy as it is from a ColdFusion MX Server. Like ColdFusion MX, you do not have to supply any extra code on the server to use a remote web service. The only difference is that you need to set up the proper permissions on the ASP.NET server. ASP.NET contains a utility called wsdl.exe, which Flash Remoting uses to automatically generate proxies for web services. The ASP.NET server creates a .dll file in your local assembly cache that acts as a proxy for the web service when you first call the service. This proxy remains in place for future calls to the service. For that reason, the first call to the service takes a little longer than subsequent calls. It's a good idea to delete these files manually during development, to prevent the use of a cached web service.

The Windows user ASPNET (found in Administrative Tools Computer Management Users and Groups) needs to be set up with permissions to write to the bin directory in your Flash Remoting application. This allows the ASP.NET server to create the .dll files necessary to consume the web service. ASP.NET also creates a C# source file for the DLL that can be modified and recompiled. The ASPNET user also needs Script execute permission from the IIS management console.

If you can't access a web service, even after applying the proper permissions, check the permission level on the wsdl.exe file, which resides in C:\Program Files\Microsoft.NET\FrameworkSDK\Bin in a default installation of the .NET Framework SDK. This file also needs to allow the ASPNET user to access it.

You can use Example 10-1 to call the web service using an ASP.NET server, by changing only one line, the reference to the gateway:

var myURL = "http://localhost/flashremoting/gateway.aspx";

You should substitute your own Flash Remoting URL here.

Upon running the example, the webroot\flashremoting\bin directory contains two new files: SQLDataSoap.cs and SQLDataSoap.dll. If you recall, the web service .wsdl file was named SQLDataSoap.wsdl, and the .dll proxy is always named after the web service. If you create .dll files manually, you should follow this naming convention as well. The .cs file is the C# source code for the proxy to the web service. The code is listed in Example 10-2. Nothing else has to be done to the code, but it is listed here for your perusal or if you need to modify the file in any way.

Example 10-2. C# code for the web service proxy
//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//     Runtime Version: 1.0.3705.0
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

//
// This source code was auto-generated by wsdl, Version=1.0.3705.0.
//
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;


/// <remarks/>
[System.Diagnostics.DebuggerStepThroughAttribute( )]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="SQLDataSoapBinding",
 Namespace="http://www.SoapClient.com/xml/SQLDataSoap.wsdl")]
public class SQLDataSoap : System.Web.Services.Protocols.SoapHttpClientProtocol {

    /// <remarks/>
    public SQLDataSoap( ) {
        this.Url = "http://soapclient.com/xml/SQLDataSoap.wsdl";
    }

    /// <remarks/>
    [System.Web.Services.Protocols.SoapRpcMethodAttribute(
"http://soapclient.com/SQLDataSRL",
RequestNamespace="http://www.SoapClient.com/xml/SQLDataSoap.xsd", 
ResponseNamespace="http://www.SoapClient.com/xml/SQLDataSoap.xsd")]
    [return: System.Xml.Serialization.SoapElementAttribute("return")]
    public string ProcessSRL(string SRLFile, string RequestName, string key) {
        object[] results = this.Invoke("ProcessSRL", new object[] {
                    SRLFile,
                    RequestName,
                    key});
        return ((string)(results[0]));
    }

    /// <remarks/>
    public System.IAsyncResult BeginProcessSRL(string SRLFile, string
RequestName, string key, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("ProcessSRL", new object[] {
                    SRLFile,
                    RequestName,
                    key}, callback, asyncState);
    }

    /// <remarks/>
    public string EndProcessSRL(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((string)(results[0]));
    }

    /// <remarks/>
    [System.Web.Services.Protocols.SoapRpcMethodAttribute(
"http://soapclient.com/SQLDataSRL", 
RequestNamespace="http://www.SoapClient.com/xml/SQLDataSoap.xsd",
ResponseNamespace="http://www.SoapClient.com/xml/SQLDataSoap.xsd")]
    [return: System.Xml.Serialization.SoapElementAttribute("return")]
    public string ProcessSRL2(string SRLFile, string RequestName, string key1,
string key2) {
        object[] results = this.Invoke("ProcessSRL2", new object[] {
                    SRLFile,
                    RequestName,
                    key1,
                    key2});
        return ((string)(results[0]));
    }

    /// <remarks/>
    public System.IAsyncResult BeginProcessSRL2(string SRLFile, string
RequestName, string key1, string key2, System.AsyncCallback callback, object
asyncState) {
        return this.BeginInvoke("ProcessSRL2", new object[] {
                    SRLFile,
                    RequestName,
                    key1,
                    key2}, callback, asyncState);
    }

    /// <remarks/>
    public string EndProcessSRL2(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((string)(results[0]));
    }

    /// <remarks/>
    [System.Web.Services.Protocols.SoapRpcMethodAttribute(
"http://www.SoapClient.com/SQLDataSQL",
RequestNamespace="http://www.SoapClient.com/xml/SQLDataSoap.xsd",
ResponseNamespace="http://www.SoapClient.com/xml/SQLDataSoap.xsd")]
    [return: System.Xml.Serialization.SoapElementAttribute("return")]
    public string ProcessSQL(string DataSource, string SQLStatement, string
UserName, string Password) {
        object[] results = this.Invoke("ProcessSQL", new object[] {
                    DataSource,
                    SQLStatement,
                    UserName,
                    Password});
        return ((string)(results[0]));
    }

    /// <remarks/>
    public System.IAsyncResult BeginProcessSQL(string DataSource, string
SQLStatement, string UserName, string Password, System.AsyncCallback callback,
object asyncState) {
        return this.BeginInvoke("ProcessSQL", new object[] {
                    DataSource,
                    SQLStatement,
                    UserName,
                    Password}, callback, asyncState);
    }

    /// <remarks/>
    public string EndProcessSQL(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((string)(results[0]));
    }
}


    Part III: Advanced Flash Remoting