Requirement: Client uses IBM ESB and expects the WSDL and XSD files that follow ESB standards
Issue: The auto generated WSDL and XSD from ASMX (http://url.asmx?WSDL) don’t support the ESB standards because of .Net limitation in providing XMLElement Attributes
Solution: Build XSD and WSDL manually and generate Service implementation and Proxy classes using WSDL.EXE tool provided by .Net framework. The possible Implementation classes can be Abstract class and Interface. Once implementation file is generated implement the real business logic by extending the implementation class or interface in to service file (.asmx).
Steps to create:
1. The quick way to build XSD and WSDL is to create an ASP.Net Webservice and add the web methods we are planning to implement.
3. Capture the WSDL and make a copy of its content
4. You will find XSD definition under tag.
a. If you want to separate XSD from WSDL you can copy that and make an XMLSchema file (.xsd) and start building it in visual studio
Referencing XSD into WSDL like below:
<wsdl:types>
<xsd:schema targetNamespace="http://sampledomain/Service" elementFormDefault="qualified">
<xsd:import namespace="http://sampledomain/service" schemaLocation="Service.xsd" />
</xsd:schema>
</wsdl:types>
|
b. If you don’t want to separate XSD from WSDL you can edit in WSDL itself (of course using Visual Studio)
Note: There are some tools out which can help you to build WSDL and XSD but I felt comfortable using Visual Studio.
5. Define all the elements required in XSD and define messages and other elements in WSDL
6. Now, it is the time to generate Service files from WSDL using WSDL.EXE. I was using .Net 3.5 and my system has only VS IDE 2010 installed. VS 2008 Framework was there but no IDE installed. So used the below command and register VS 2008 assemblies using RUN.
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm.exe
|
7. Run WSDL.EXE and generate the class files like below:
a.) Generate Service Interface using the command given below:
C:\Windows\Microsoft.NET\Framework\v2.0.50727>wsdl /si /out:IUserManagementServi
ce.cs " D:\Users\POCs\WebApplication1\WebApplication1\SampleService.ws
dl" "D:\Users\POCs\WebApplication1\WebApplication1\SampleService.xsd"
|
b.) Generate Service Abstract class using the command given below:
C:\Windows\Microsoft.NET\Framework\v2.0.50727>wsdl /server /out:SampleServi
ceBase.cs "D:\Users\POCs\WebApplication1\WebApplication1\SampleService.ws
dl" "D:\Users\POCs\WebApplication1\WebApplication1\SampleService.xsd"
|
/si: Generates the Interface
/server Generates the Abstract class
Note: You should use only one file either it is abstract class or interface in your Webservice project.
c.) Generate the Service Proxy for Client for testing.
C:\Windows\Microsoft.NET\Framework\v2.0.50727>wsdl /out:SampleServi
ceProxy.cs "D:\Users\POCs\WebApplication1\WebApplication1\SampleService.ws
dl" "D:\Users\POCs\WebApplication1\WebApplication1\SampleService.xsd"
|
These files need to be regenerated every time you make a change in XSD and WSDL files.
8. Let’s take Interface implementation over abstract class (I did both, but liked Interface). Go to Web Service Project in Visual Studio and add the Interface file, WSDL and XSD files. The interface will have all the signatures of WebMethods. Now go to your service file (.asmx.cs) and implement the interface (you can remove the previously added webmethods, including helloworld webmethod). It will look like below:
public class SampleService : System.Web.Services.WebService, ISampleServiceSOAP
|
To implement interface methods in Service class, right click on interface (ISampleServiceSOAP) right click on it and select “Implement Interface” and you will see two options and between them select “Implement Interface”. This will add all the interface methods. We can add our business logic in these methods.
Now the service is ready with contract approach!!
9. Time for testing. Create a client application (Console or Web App or Window) and take the proxy class file that was created at step 7c and add it to Client Project.
10. Create an object on Proxy Service class and call a web method with proper input values and test it.
Note:
1. It is always good idea to host your service in your local IIS server with proper port number for testing it locally. This way you can ensure that the service can be accessed from client using URL
2. Also make sure that the Proxy Service class constructor has the url rightly pointing to the service url (.asmx) which provides service end points
Ex:
public SampleService()
{
this.Url = "http://localhost:9999/SampleService.asmx";
}
|
Online references:
No comments:
Post a Comment