2

I am consuming a web service which has various methods one of which requires xml as input.

There is a structure that is required to be passed to get the correct response.

I have added the service reference within Visual studio and can see the method within the contract class which it has generated.

How can i create the correct class without phyisically getting the .xsd file and running xsd.exe to generate the class? As there could a change in the future to the xml format then I would need to run the xsd.exe again which is not good.

Any ideas would be great

Thanks

tjhack
  • 1,022
  • 3
  • 20
  • 43
  • Do you have access to the web service assemblies? Specifically the assemblies which contain the service definition? – tom redfern Apr 25 '16 at 14:07
  • Do the webservice want a class or just text? If it is just text then all you need to do is to read the xml file and send as text. A serialized class will produce the same xml as the text xml file so does it make sense to deserialize the xml file (create class) just to serialize (create text) the same text again? – jdweng Apr 25 '16 at 14:16

1 Answers1

1

As there could a change in the future to the xml format then I would need to run the xsd.exe again which is not good.

You are completely correct in that generating a client-side representation of the service contract definition is not good. Far better to reference the actual service definitions in the service assembly, and then construct your channel at run time using ChannelFactory<T>. This is a far superior approach than using a generated service reference for many reasons.

There are only two things which may stand in your way with this approach:

  1. You don't have access to the service binaries. Obvioulsy you must be able to consume the actual types used to define the service.
  2. The service binaries are available, but are compiled into an "uber-assembly" containing all sorts of stuff you don't care about and don't necessarily want to consume from your client app.

If the first of the above two conditions are true then sorry but you must resort to a generated client proxy using xsd.exe and accept all the penalties that this incurrs. If the second condition is true, you may be able to ask the service owner to extract the service definition code into a separate assembly for you and you can proceed as normal using the method described.

i have the class at hand so can use xml serializer...do i just create a soapEnvelope and then append the generated xml to the body of the request?

I am assuming the service is also using WCF?

The point of using a framework like WCF is that you don't need to care about the serialization, encoding, and transmission of the data from client to service. WCF takes care of all this for you. All you need to do is create the WCF channel using the service definition (usually this is an interface which defines the service operations as methods on the interface).

Then you can call these methods against the channel instance and the request will be serialized to XML, wrapped in a SOAP wrapper, and transmitted over HTTP to the service, where the request is recieved, unwrapped, and deserialised, again without any effort.

There is an example here: https://stackoverflow.com/a/8869809/569662

Community
  • 1
  • 1
tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • Hi thanks for the reply. It seems they will be flexible as we will need to provide the representation to them so i have the class at hand so can use xml serializer. But calling the service using the xml request do i just create a soapEnvelope and then append the generated xml to the body of the request? – tjhack Apr 25 '16 at 14:23