0

I am currently developing a WCF client to interaction with a set of WCF service references. I am developing in c#.

I don't want to following the approach of having to generate a service reference using svc util or manually adding a service reference to my class library.

Have I an other alternatives open to me? I am considering using the ServiceClient class within the ServiceModel library.

I am little confused though, for example the request and response objects related to an endpoint, where are these created or how are they created? In a previous project I used T4 mappings and DTO's, but I feel these are over kill. I did like though that I could share the same object between different service endpoints. My goal here is to create a custom client object communicating through a custom written proxy. I would like some direction on this.

amateur
  • 43,371
  • 65
  • 192
  • 320

1 Answers1

0

To talk to a WCF service (endpoint) you need to know three things (ABC): the address of the endpoint, the binding it uses, and the contract used in the communication. If you have all those three things, you don't need to use any tool to interact with the service.

The address is just the URI of the endpoint. The binding is represented by one instance of the abstract System.ServiceModel.Channels.Binding class (such as System.ServiceModel.BasicHttpBinding, System.ServiceModel.WSHttpBinding and so on). And the contract is usually represented by an interface decorated with the [ServiceContract] attribute. If you have all those three, you can create a custom proxy by using the ChannelFactory<T> class, as shown below.

public static void TalkToService(Binding binding, Uri endpointAddress) {
    // Assuming that the service contract interface is represented by ICalculator
    var factory = new ChannelFactory<ICalculator>(binding, new EndpointAddress(endpointAddress));
    ICalculator proxy = factory.CreateChannel();
    Console.WriteLine(proxy.Multiply(45, 56));
}
carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • I have the endpoint and binding but not so sure regarding the contract, is this an object I create? And the request and response objects, how do I know the type for these etc? – amateur Oct 30 '12 at 21:30
  • If you own the service, you can just copy the [ServiceContract] interface from the service to the client. That's how you get the type. What tools such as svcutil or "Add Service Reference" do is to generate an interface decorated with [ServiceContract] which is compatible with the one used in the service, but if you already have that, you can use the same type. – carlosfigueira Oct 30 '12 at 23:21