开发者

How to handle webservice without WSDL with C#?

My application(.NET 4.0) needs to integrate with a webservice. If the webservice did have a WSDL it would be easy to generate proxy class with WCF but this is not possible in this case (No WSDL).

To communicate with this server we will have to send a message with settings and then receiver something like this :

<Desc>
<Make cfe_code="98" cfe_value="Volkswagen" label="Märke" value="Volkswagen"/>
<ModelName cfe_code="99" cfe_value="Touareg" label="Modell" value="Touareg"/>
<BodyType cfe_code="212" cfe_value="22" label="Kaross" value="SUV"/>
<ModelYear开发者_如何学JAVA cfe_code="8" cfe_value="2005" label="Årsmodell" value="2005"/>
...
</Desc>

So how do I solve this? Do I have to dig in to XMLDocuments and do it all manual?

Edit1: The URL to the webservice might look like this : http://MyServer.com/ag/get?UID=9999999999.eu_vddsall_xml&VINREG=STU123&LANG=en

UID is static
VINREG is the registration ID of the object that we need information about
LANG is the language setting

There will also be a basic authentication (Username/Password)


This appears to be a non-SOAP endpoint, in which case there is not a standard way to expose service metadata via a WSDL.

Essentially what you'd need to do is create classes to represent the Desc and its child elements:

[XmlRoot("Desc")]
public class Description
{

    [XmlElement("Make")]
    public Make make { get; set; }

    [XmlElement("ModelName")]
    public ModelName modelName { get; set; }

    // etc...
}

public class Make
{

    [XmlAttribute("cfe_code")]
    public int cfeCode { get; set; }

    // etc...
}

These classes represent the data you will receive from the service call. Next, create a ServiceContract that represents the service method:

[ServiceContract]
[XmlSerializerFormat]
public interface IService
{
    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare,
      ResponseFormat = WebMessageFormat.Xml,
      UriTemplate = "get?UID=9999.eu_vddsall_xml&VINREG={vinreg}&LANG=en")]
    Description MyMethod(string vinreg);
}

Lastly, create an instance of a client proxy using IChannelFactory and consume the service.

See this blog post on how to create a WCF client to consume a RESTful service using a WCF client.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜