Creating a WCF Client for an ASMX Service with SvcUtil.exe


Creating a WCF Client for an ASMX Service with SvcUtil.exe

The following exercise creates a client for a very simple ASMX Web service with one web method, InteropHelloWorld. As you can see in the Listing 7.1, this method for that service receives a string parameter, Name, and returns a string value.

Listing 7.1. ASMX Service Code

using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService {     public Service () {     }     [WebMethod]     public string InteropHelloWorld(string Name) {         return "Hello " + Name + ", you've just interop'd.";     } }

This service was deployed using the setup script available at the publisher's website.

Validate that the service is online and create the proxy for the client:

  1. Test that the service is online and available.

  2. Open Internet Explorer, and navigate to the following URL:

    http://localhost/WCFHandsOn/InteropHelloWorld/Service.asmx

    This should display a page that lists the operations supported by this service. If this does not display, validate that the virtual directory exists and is pointing to C:\Apps\WCFHandsOn\Chapter7\PartI\After\InteropHelloWorld.

  3. In the open solution, add a new project.

  4. Create a new Windows Console application in C:\WCFHandsOn\Chapter7\Before\PartI\WCFClient.

  5. Add a reference to System.ServiceModel.

    Now you will create the proxy for the ASMX service.

  6. Open a Visual Studio command prompt and navigate to the directory C:\WCFHandsOn\Chapter7\Before\PartI\WCFClient.

    Enter the following at the command prompt:

    [View full width]

    "C:\Program Files\Microsoft SDKs\Windows\v1.0\Bin\SvcUtil.exe " http://localhost/ WCFHandsOn/InteropHelloWorld/Service.asmx?wsdl/out:proxy.cs

    SvcfUtil.exe generates two files, proxy.cs and output.config.

    Proxy.cs contains a generated proxy class for the ASMX Web service. Output.config contains the configuration information for the service that can be inserted into the application configuration file (App.config).

  7. Within Visual Studio, add both of these files to the WCFClient project.

  8. Rename output.config to App.config.

  9. Using Solution Explorer, open the App.config file and examine its contents:

    [View full width]

    <?xml version="1.0" encoding="utf-8"?> <configuration> <system.serviceModel> <client> <endpoint address="http://localhost/WCFHandsOn/InteropHelloWorld/Service .asmx" bindingConfiguration="ServiceSoap" binding="customBinding" name="ServiceSoap" contract="ServiceSoap" /> <endpoint address="http://localhost/WCFHandsOn/InteropHelloWorld/Service .asmx" bindingConfiguration="ServiceSoap12" binding="customBinding" name="ServiceSoap1" contract="ServiceSoap" /> </client> <bindings> <customBinding> <binding name="ServiceSoap"> <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" messageVersion="Soap11Addressing1" writeEncoding="utf-8 /> <httpTransport manualAddressing="false" maxBufferPoolSize="524288" maxMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous" bypassProxyOnLocal="false" ostNameComparisonMode="StrongWildcard" mapAddressingHeadersToHttpHeaders="true" proxyAuthenticationScheme="Anonymous" realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true" /> </binding> <binding name="ServiceSoap12"> <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" messageVersion="Default" writeEncoding="utf-8" /> <httpTransport manualAddressing="false" maxBufferPoolSize="524288"maxMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" mapAddressingHeadersToHttpHeaders="true" proxyAuthenticationScheme="Anonymous" realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true" /> </binding> </customBinding> </bindings> </system.serviceModel> </configuration>

    Notice that SvcUtil has created the two endpoints ServiceSoap and ServiceSoap1 for InteropHelloWorldService.

    If you look at the WSDL document exposed by our ASMX service, you'll notice that these endpoints were created from the port information in that document:

    <wsdl:service name="Service"> <wsdl:port name="ServiceSoap" binding="tns:ServiceSoap"> <soap:address location="http://localhost:8000/WCFHandsOn/InteropHelloWorld/Service.asmx" /> </wsdl:port> <wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12"> <soap12:address location="http://localhost:8000/WCFHandsOn/InteropHelloWorld/Service.asmx" /> </wsdl:port> </wsdl:service>

    Also note that SvcUtil.exe creates custom bindings for each of these. Each of these displays a number of the configurable attributes that can be set, highlighting the flexibility available for interoperability.

  10. From Solution Explorer, open proxy.cs.

    This is the proxy class created by SvcUtil.exe. Notice that the ServiceSoap interface was defined, containing a single operation that was in our service.

    In addition, a ServiceSoapProxy class was created. Note that in addition to several constructors for the class, the class also implements the ServiceSoap interface. When InteropHelloWorld is called on the proxy, it will call the operation on our ASMX service:

    [View full width]

    [System.ServiceModel.ServiceContractAttribute(Namespace="http://Samples.Microsoft.Com/")] public interface ServiceSoap { [System.ServiceModel.OperationContractAttribute(Action="http://Samples.Microsoft.Com /InteropHelloWorld", ReplyAction="http://Samples.Microsoft.Com/InteropHelloWorld")] string InteropHelloWorld(string Name); } public interface ServiceSoapChannel : ServiceSoap, System.ServiceModel.IClientChannel { } public partial class ServiceSoapProxy : System.ServiceModel.ClientBase<ServiceSoap>, ServiceSoap { public ServiceSoapProxy() { } public ServiceSoapProxy(string endpointConfigurationName) : base(endpointConfigurationName) { } public ServiceSoapProxy(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public ServiceSoapProxy(string endpointConfigurationName, System.ServiceModel .EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public ServiceSoapProxy(System.ServiceModel.Binding binding, System.ServiceModel .EndpointAddress remoteAddress) : base(binding, remoteAddress) { } public string InteropHelloWorld(string Name) { return base.InnerProxy.InteropHelloWorld(Name); } }

    Now that we have a proxy class and a configuration file, let's write the code to call the service.

  11. Using Solution Explorer, open the file Program.cs.

  12. Add using System.ServiceModel; to the top of the Program.cs file:

    using System; using System.Collections.Generic; using System.Text; using System.ServiceModel;

  13. Modify the Main method in Program.cs to include the lines listed in bold in the following code:

    static void Main(string[] args) {     ServiceSoapProxy proxy = new ServiceSoapProxy("ServiceSoap");     Console.WriteLine("What's your name?");     string name = Console.ReadLine().ToString();     Console.WriteLine(proxy.InteropHelloWorld(name));     Console.WriteLine("------------------------");     Console.WriteLine("Press Any Key To Exit");     Console.ReadLine(); }

    Note that we chose to reference the constructor that takes a configurationName as a parameter. At runtime, the name provided will be used to reference the appropriate endpoint, bindings, and contract in the App.config file.

  14. Using Solution Explorer, right-click the WCFClient project and select Set as Startup Project.

  15. From the Debug menu, select Start Debugging.

After you've entered your name at the prompt, the client will call out to the ASMX service and display the result.




Presenting Microsoft Communication Foundation. Hands-on.
Microsoft Windows Communication Foundation: Hands-on
ISBN: 0672328771
EAN: 2147483647
Year: 2006
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net