• WCF Part 5 : Consuming the service


    Last time we generated the client and configuration file. Whereas in the asmx world we had a proxy class, the WCF team renamed this in the June CTP to client. For us to use the generated files, we need a new console application and add the files. Don't forget to add the System.ServiceModel reference to the project. Once this is done, it should compile without errors.

    We'll have to create an instance of the client. At that point, the application configuration on the client will be read. However, there won't be a connection until the first call has been made. After creating the instance, we can call the HelloWorld operation. Notice that the HelloComputer operation isn't exposed, as we didn't apply the OperationContractAttribute in the interface. Besides the generated files, not much code is needed to execute the call.

        1 static void Main(string[] args)
        2 {
        3   Console.WriteLine("Press any key when the service is available...");
        4   Console.ReadKey();
        5 
        6   HelloClient client = new HelloClient();
        7 
        8   string msg = client.HelloWorld();
        9 
       10   Console.WriteLine("The message is : {0}", msg);
       11   Console.WriteLine("Press any key to quit...");
       12   Console.ReadKey();
       13 }

    On line 6 the instance is created, on line 8 we call the service. Line 10 shows the message in the console window. Notice line 3 and 4. When we start (or execute) both assemblies at the same time, we need to be able to wait and allow the service to get up and running.

    There are two methods to execute the assemblies. You can right-click a project in Visual Studio 2005, choose Debug and Start new instance. Or you can right-click the solution, choose Set StartUp Projects... and select the radiobutton Multiple startup projects. Then specify 'Start' for both projects. After hitting the F5 key, both projects will be started at the same time.

    Consumer configuration
    You can examine the configuration of the consumer, in our case our console application that's calling the service. You'll see a lot of info on the basicHttpBinding, but that's not really important right now. Just stuff on timeouts, message encoding, etc. that's all default. What is (kind of) important however, is everything in the client element.

    <client>
        <endpoint address="http://localhost:8080/HelloService/" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IHello" contract="IHello"
            name="BasicHttpBinding_IHello" />
    </client>

    Again you'll notice the WCF ABC, address, binding and contract. All this has been imported by the Service Utility (svcutil.exe) and is now inside the client class as well.

    We've setup our first (have we? ;-) running service and client application and we're talking in text/xml over http with each other. We're running this on the same computer, but you could place either one of these applications on the other side of the world and wouldn't notice a difference, but perhaps some delay because of the distance.

    You can download the complete solution here, to see how I've set everything up. Before we take further steps into the world of Windows Communication Foundation and get beyond the WCF ABC, we'll examine what we've done exactly.

    Update : Be sure to have the WCF/WF extensions installed! You don't need the >1GB SDK installed.

  • 相关阅读:
    C#
    C#
    css
    css
    css
    css
    css
    Css
    Javascript
    ASP.NET MVC
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2552065.html
Copyright © 2020-2023  润新知