Visual web part needs to fetch data from aspx web service. WSDL url is provided.
Service reference vs Code generation:
There are two ways to add reference to asmx services. You can directly right click the web part project and click on add service reference, click on advance and then choose asmx service reference.
Adding service reference will hard code the service url. So it is suggested that we use WSDL.exe available in .net framework tools to generate a client proxy.
How to use WSDL.exe to generate service proxy?
- Go to .net command prompt
- type wsdl
- Url of the service should not be hard coded, it should be coming from web part property.
- To imlpement this, add a custom property in web part to store service url
- Add another paramterized constructor to the first class. See example below:
this.Url = "web service url";
public Spotlight(string _url)
this.Url = _url;
- Generally the first class in wsdl generated proxy cs will be the one where you need to add the new constructor.
- You can confirm this by looking at the first class and it will be
- When web part property for service url is blank, use default constructor
- When web part property for service url is not blank use parameterized constructor.
- When your service will change, you have to repeat the process to generate proxy and add additional parameterized constructor
- Always use proper error handling to make sure exceptions are not visible to end users
- Always show appropriate message in case of no data to users
- Always show appropriate message in case of error getting data from service
- Always use best practice to parse xml data.
- If possible use xml deserializers to convert xml data into List
We find it very useful to use WSDL.exe to generate asmx proxy and using constructor injection to initialize the service url from web part property.