Edwin
Edwin

Reputation: 961

proper way to use a WCF service?

I followed this guide from MSDN to host a simple WCF service.

But what is the proper way on the client side to use it?

Upvotes: 0

Views: 1080

Answers (6)

L-Four
L-Four

Reputation: 13531

The most used options are:

  • Generate proxy with Visual Studio or use svcutil. This means that it is autogenerated for you, so it's very easy to use and mostly used if the client to your service is external to the system. But as it's generated code, you loose some control.
  • Use ChannelFactory if the client is tightly bound to the service. I like this method the most because you are working with interfaces directly. In most cases I provide a service agent that abstracts the use of the service, so that the client doesn't have to worry about it. In this service agent you can also put additional concerns like caching and logging.

Upvotes: 4

FloChanz
FloChanz

Reputation: 3429

Add a service reference to your client project and select Discover/Services in solution. Then select your service and you'll be able to access all your services methods by writing this kind of code :

 using(var myClient = new YourServiceReference.YourService())
 {
   myClient.MyMethod()...
 }

Upvotes: 1

rpeshkov
rpeshkov

Reputation: 5047

Personally I prefer to work with WCF service by creating ChannelFactory<T> and then creating a channel for communication with it.

Example:

ChannelFactory<IProcessor> factory = null;

try
{
    var netTcpBinding = new NetTcpBinding("netTcpBinding_BigPackets");

    factory = new ChannelFactory<IProcessor>(netTcpBinding);

    var processor = factory.CreateChannel(processorAddress);
    var result = processor.Process(request);

    return result;
}
catch (CommunicationException)
{
    if (factory != null)
    {
        factory.Abort();
        factory = null;
    }

    throw;
}
finally
{
    if (factory != null)
    {
        factory.Close();
    }
}

Good example can be found in MSDN: http://msdn.microsoft.com/library/ms576132.aspx

Also I would advise you to refer WCF samples from IDesign page: http://idesign.net/Downloads

Upvotes: 1

iefpw
iefpw

Reputation: 7042

You use it using C# code by extracting the WCF XML from it or use WCF Test Client. Go to Visual Studio and add "Service Reference" using the WCF URL to add the service and it will generate the service code for you.

Upvotes: 0

mipe34
mipe34

Reputation: 5666

You can continue your learning path on MSDN.

There are several options:

  1. Use svcutil to generate the client
  2. Use add service reference in VS solution context menu
  3. Create client on your own (not recommended for beginners)

Note: The first 2 options requires already running service.

Upvotes: 3

Dejan Dakić
Dejan Dakić

Reputation: 2448

Here is nice example about usage of WCF services http://wcfbyexample.codeplex.com/

Upvotes: 0

Related Questions