Creating a WCF Service and WCFASMX Clients


Creating a WCF Service and WCF/ASMX Clients

In this next example, we'll look at interoperability from the other direction. Here we'll create a WCF service and then create ASMX and WCF clients that utilize it.

Creating the WCF Service

To create the new service project, follow these steps:

  1. Open Visual Studio and add a new project. Create a new Windows Console application in C:\WCFHandsOn\Chapter7\Before\PartII\InteropHelloWorld2.

  2. Add a Reference to System.ServiceModel.

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

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

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

    This service will expose one operation, InteropHelloWorld. First, add the interface IInteropHelloWorld to the Program.cs file.

  5. Enter the following interface code:

    namespace Samples.Microsoft.Com {    [ServiceContract]    public interface IInteropHelloWorld    {      [OperationContract]      string InteropHelloWorld(string Name);    }

    With the interface defined, we can now add the class for our service that implements it.

  6. Enter the following code for the class:

    public class InteropHelloWorld2 : IInteropHelloWorld {     public string InteropHelloWorld(string Name)     {        return "Hello " + Name + ", you've just interop'd.";     } }

    For this exercise, the service will not be be hosted within IIS, but is instead hosted inside of a Windows Console application.

    This requires the creation of a ServiceHost object to instantiate the service. When a new ServiceHost is being created, information regarding the service is provided in the constructor. In this case, we specify the serviceType that maps to the name of the class:

        static void Main(string[] args)     {         ServiceHost service =           new ServiceHost(typeof(InteropHelloWorld2));         service.Open();         Console.WriteLine("Service is available.");         Console.ReadLine();      }

    Now we can create the App.config file and configure the service.

  7. Add a new application configuration file to the project and accept the default name of App.config.

    Open the configuration file and enter the information for the service's abc's (address, binding, contract) as seen here:

    [View full width]

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <!-- use appSetting to configure base address provided by host --> <add key="baseAddress" value="http://localhost:8080/WCFHandsOn/InteropHelloWorld2" /> </appSettings> <system.serviceModel> <services> <service type="Samples.Microsoft.Com.InteropHelloWorld2"> <endpoint address="" binding="basicHttpBinding" contract="Samples .Microsoft.Com.IInteropHelloWorld"/> </service> </services> </system.serviceModel> </configuration>

    Our base address is specified in the appSettings setting, because we'll be setting this value inside of the code. Also note that the endpoint for this service is being made available on port 8000 of the local server. Because this is running in a console application and is not hosted within Internet Information Server, we've specified a separate port. Note that we are using the basicHttpBinding, and the service will be compliant with BP 1.0. Finally, note that the contract consists of the concatenation of the namespace and the interface for the service.

    With the application configuration file populated, the project is now ready to be compiled.

  8. Build the project.

  9. Using Windows Explorer, navigate to the InteropHelloWorld2.exe just created and run the InteropHelloWorld2 service. The full path to the file is C:\WCFHandsOn\Chapter7\PartII\Before\InteropHelloWorld2\InteropHelloWorld2\bin\Debug\InteropHelloWorld2.exe.

With the service now running, it is available for clients to query it for metadata and interact with it.

Creating a WCF Client

Creating the WCF client for our WCF service will be virtually identical to how we created the WCF client for the ASMX service in the initial example, using SvcUtil.exe:

  1. Create a new Windows Console project in C:\WCFHandsOn\Chapter7\PartII\Before\WCFClient\.

  2. Add a Reference to System.ServiceModel.

  3. Open Visual Studio.NET Command Window.

  4. Execute the SvcUtil.exe utility, pointing it at the WCF Service. This creates both a proxy and a configuration file to be used by the client:

    [View full width]

    "c:\Program Files\Microsoft SDKs\Windows\v1.0\Bin\SvcUtil.exe" http://localhost:8000 /WCFHandsOn/InteropHelloWorld2 /out:proxy.cs

  5. Using Solution Explorer, add proxy.cs to the WCFClient project.

  6. Using Solution Explorer, add output.config to the WCFClient project.

  7. Rename output.config to App.config.

  8. Place the following code in the Main method in the Program.cs file:

    static void Main(string[] args) {    Console.WriteLine("Please enter your name");    string Name = Console.ReadLine();    InteropHelloWorldProxy hello = new InteropHelloWorldProxy();    Console.WriteLine("This is the message from the WCF Web Service:");    Console.WriteLine(hello.InteropHelloWorld(Name));    Console.ReadLine(); }

  9. In Solution Explorer, right-click the project and select Debug, Start New Instance.

The client will prompt you for your name. It will then call the InteropHelloWorld service and write the response to the console.

Creating an ASMX Client

There are very straightforward ways to enable ASMX clients to access your Windows Communication Foundation service. For projects in which clients will have both the .NET Framework 2.0 and WinFX installed, SvcUtil.exe can be used, as it was in the preceding example. In that environment, the proxy.cs and output.config files generated by SvcUtil.exe could be added to an ASMX Web service project without modification.

If your service consumers do not have WinFX installed and/or are unfamiliar with SvcUtil.exe, they can consume your BP 1.1 Web service as they would any other Web service, using the Add Web Reference functionality built into Visual Studio. The WCF service exposes both WSDL and MEX metadata, which can be used by programmers and development tools to consume the service on any platform.

This example looks at creating an ASMX client for the WCF Service created earlier.

Rather than using SvcUtil.exe, which requires the .NET Framework 2.0, as mentioned earlier, we'll use the Add Web Reference facility that's been available since the initial release of VisualStudio.NET. It is important to note that while WCF requires the .NET Framework 2.0, clients for BP 1.1 services can be created using earlier versions of the framework and Visual Studio.NET:

  1. Within VisualStudio.NET, create a new website at C:\WCFHandsOn\Chapter7\PartII\Before\.

  2. Name the website ASMXClient.

  3. In Solution Explorer, add a web reference to the project.

    When prompted in the Add Web Reference dialog, enter the following URL: http://localhost:8000/WCFHandsOn/InteropHelloWorld2?wsdl

    With the web reference set, the ASMX client will now utilize the service within its own service. When the project was created, a file Service.cs was added to the project in the project's App_Code directory.

  4. Open this file now and add the code for the WebMethod:

    using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://Samples.Microsoft.Com/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService {     public Service () {     } [WebMethod] public string HelloWorldAsmx(string Name) {      localhost.InteropHelloWorld2 hello = new localhost.InteropHelloWorld2();      return "This is the message from the WCF Web Service" + hello.InteropHelloWorld(Name); ;     } }

  5. Using the Internet Information Services Manager, create a new virtual directory. The new directory should use the alias WCFHandsOn_ASMXClient and the local path for the virtual directory should point to the location of the project, C:\ WCFHandsOn\Chapter7\PartII\Before\ASMXClient.

  6. Using VisualStudio.NET, build the project.

  7. Finally, test the "client" ASMX service by opening Internet Explorer and entering the URL for the service, http://localhost/WCFHandsOn/ASMXClient/Service.asmx.




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