Geeks With Blogs
Steve Loethen When I grow up I want to code in c#

Now that we have a contract defined (WCFContract) a binding selected (basicHttpBinding) and a address picked (http://localhost:8000/WCFConsoleHostedService/ConsoleService) we are ready to let someone use it.

To use a service, we basically perform many of the same operations. 

1) add a new console app to our solution.  It doesn't have to be part of our solution, that is just what this demo does.  To that console add the standard reference for our friend System.ServiceModel.

2) Open the program.cs (okay, I am a c# bigot, can't seem to program without curly braces and semi-colons) and add a using System.Service Model to the top.

Now we have the file prepped.  It should consist of an empty main.  Here we are going to create the pieces of our service request.  First, we must get a proxy to the service.  To create that proxy we are going to use class called ChannelFactory<T> that is part of System.ServiceModel.  But setting out there at the end of that class is this <T>, that needs to have something to implement. 

That something is the interface that our service exposes.  There are several ways we can get that, but for now we are going to take the easiest route, and just copy the interface into our client project.  So, add a .cs file to our client app, and copy the interface into it.  It will look like this.

 

using System.ServiceModel;


namespace WCFConsoleClient
{
    [ServiceContract(Namespace = "MyService")]
    public interface IMyContractInterface
    {
        [OperationContract]
        string SayHello(string name);

        [OperationContract]
        string AllCaps(string str);

        [OperationContract]
        string AllLower(string str);
    }
}

 

this is just our friend System.ServiceModel and the copy/paste of the IMyContractInterface from our contract class into this project.  Now our client knows what it is going to create a proxy to, and what operations it exposes.

The client now has the what.  Let's give it the where.  Create and endpointaddress to the service. 

Then from this endpointaddress, and the how (basicHttpBinding) use ChannelFactory to create a proxy. 

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

namespace WCFConsoleClient
{
    class Program
    {
        static void Main(string[] args)
        {
            EndpointAddress ep = new EndpointAddress("http://localhost:8000/WCFConsoleHostedService/ConsoleService");

            IMyContractInterface proxy = ChannelFactory<IMyContractInterface>.CreateChannel(new BasicHttpBinding(), ep);

            string str;
            str = "bob";

            str = proxy.SayHello(str);

            Console.WriteLine(str);

            str = proxy.AllCaps(str);

            Console.WriteLine(str);

            str = proxy.AllLower(str);

            Console.WriteLine(str);

            Console.WriteLine("Press <ENTER> to terminate Client");
            Console.ReadLine();
        }
    }
}

 

once we have our proxy, we can go ahead and call methods on it.  Pretty straight forward at this point. 

Our Server and our client both host the service and grab the proxy programmatically.  In our next chapter we will change the server to use settings in app.config to administratively set most of the features of our hosted service.

Until the next time.

Posted on Thursday, June 21, 2007 12:19 PM | Back to top


Comments on this post: My endpoint is all dressed up and no one to dance with..

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Steve Loethen | Powered by: GeeksWithBlogs.net