How to Create a Client by Generating a Proxy
In this demonstration you will see how to create a client
application to access a Windows Communication Foundation Service by generating
a proxy.
In our client application, we have a label, text box, and a
button. When a user enters a name into the text box, and clicks the button, a
message box will be displayed showing a greeting with a name entered into the
text box.
We need to add a reference to our client application, and in
order to do so, we need to have our Endpoint available. This requires us to go
in and edit the service behavior that we created in our previous demonstration.
Another serviceMetadata element, we notice that our
HttpGetEnabled tag is set to False. We need to enable that, so we will set it
to True, save our changes, and come back to Visual Studio.
We will start the service application, and once our service
application is up and running, we switch back to Visual Studio. We can now add
a service reference to our client application.
We simply type in the URL that was specified in our service
application, and provide a service namespace reference, and call this
GreetingServices. And this will cause our client application to go and search
to the endpoint that we have specified, locate the necessary metadata, and
generate the appropriate proxy for us.
So you can see that our client application now contains a
System.ServiceModel reference, as well as our new service reference
GreetingServices.app.
The proxy has been generated for us automatically,
GreetingServices.cs. And as we scroll through our code we can see that it has
actually generated a greeting client partial class for us, and we can utilise
that in our application.
So let’s go ahead and start debugging, and we will switch
back to the code for our client application.
Let’s go ahead and add a using statement here,
WCFClient.GreetingServices, so we will add that namespace.
Now we want to add a new proxy client variable, so we will
call this GreetingClient proxy. We need to create the proxy to the service, so
proxy equals new GreetingClient.
And you will notice that we have five options available to us
for the constructor for our GreetingClient. For this demonstration, we are only
going to use the default constructor.
So now that we have our proxy created, we are going to call
the necessary service, and store the value into the response variable, so the
response equals proxy.GetGreeting, and of course the value that we are
concerned with, will be displayed in the text box txtName.
And we have a little catch block in the event that we have in
the issue we are trying to get the data from our service. The message box will
show the response, and of course, when our form is closing, we want to go
through the process of closing out our proxy service, so we will close that
here.
Now in order to make both of these function, we need to go
and make some changes to our solution. We will set our start-up project, so we
will have our client and our demo both starting simultaneously, save our
changes, and let’s go ahead and run our application and see how it works.
So we see our services running in the background. Here is our
client application.
So let’s say hello to Fred Flinstone this morning. We click
on Display Greeting, an error message box pops up with the greeting displayed.
So that it is it for our client application that we have
generated within Visual Studio. For finer control, Visual Studio also makes
available the svcutil client. The svcutil is a tool that you can execute from
the command line, and it contains multiple options based on the specific type
of configuration you will want to create.
Notice that we have some common options available, some code
generation, metadata export options, service validation options, metadata
download options, XmlSerializer type generation options, and a few examples in
Help file that will show you how to make use of the svcutil command.
This allows a much finer control over the generation of the
proxy. For our purposes, in this demonstration, we are simply going to generate
the same files that Visual Studio did. So we will use the svcutil connecting to
our local host. Note that our service is actually still running.
We will connect to that endpoint, and we will have the
svcutil go ahead and download the metadata from our service, and then it will
generate the two files that we have available, the app.config and our
Greeting.cs.
Notice that it is called an output config.
So if we use notepad to look at our Greeting.cs file, you can
see that it has created the same code that was generated in Visual Studio.
We can also use notepad to view the output.config file, and
see the values that were generated in the config that we would apply to our
client application. This was generated using the svcutil class.
So in this demonstration you have seen how to create a client
application for accessing your Windows Communication Foundation Service by
generating proxy information.
No comments:
Post a Comment