Using the WsContract and the ServiceCatalog - HodStudio/XitSoap GitHub Wiki

In real life, we address on a web service change basically depending on the model that you want to use. Because of this, will be basically impossible to create something totally generic.

But to avoid this kind of problem, the XitSoap has a inteligent implementation that can save you a lot of time (and headache!).

The WsContract is a class atribute that has to overloads:

public WsContractAttribute(string contractName)
public WsContractAttribute(string contractName, string @namespace)

Using the WsContract only to access the ServiceCatalog

The XitSoap has a Catalog, called ServiceCatalog, for your services. The good approach is to register your Contracts' Names with the related web service address. We recommend you to do it at the start of your application.

ServiceCatalog.Catalog.Add("ProductContract", "http://localhost/XitSoap/ProductService.asmx");

After do this, you can decorate you class with the related WsContract.

[WsContract("ProductContract")]
public class ProductOutput

Doing this, you can make you code more simpler!

var wsCon = new WebService();
var result = wsCon.Invoke<Product>("GetProduct");

As the class Product has the attribute WsContract set to ProductContract and we have this contract registered on the ServiceCatalog, we can invoke direct, without have to provide any information.

Providing the namespace

In real world, most of the web services are using some namespace different from "http://tempuri.org/". And sometimes, even classes that are on the same base address have different namespaces.

REMEMBER: the namespace are normally case sensitive, what make this a constant situation on development.

The second overload of the WsContract provides a way to work with this.

ServiceCatalog.Catalog.Add("UsZipContract", "http://www.webservicex.net/uszip.asmx");

The class now will you the second overload of the method.

[WsContract("UsZipContract", "http://www.webserviceX.NET")]
public class CityZipSearch

//Same Contract, Different namespaces (case sensitive)
[WsContract("UsZipContract", "http://www.webservicex.net")]
public class CityZipAdd

When call the method, you don't need to provide any information. And because of this, you can re-use the WebService object.

var wsCon = new WebService();
var citySearch = wsCon.Invoke<CityZipSearch>("GetInfoByZIP");
var cityAdd = wsCon.Invoke<CityZipAdd>("AddCity");
⚠️ **GitHub.com Fallback** ⚠️