WCF WebService Replication
I would like to replicate a given web-service for simulation purposes. The main idea is that the new service implements the same interface as an existing one (which I do not control in any way) but with different actual method implementations. I would, then, like to be able to redirect request to the real address or to 开发者_StackOverflow社区the simulated one under certain testing conditions without having to change configurations. Is this possible to accomplish in WCF?
In terms of actually replicating the service, if the existing service exposes metadata, i.e. WSDL, then you can recreate the service by essentially doing the following:
- Browse to the wsdl and all the xsd's that the wsdl references and save them all to disk.
- With those files, run the wsdl.exe utility passing it the wsdl and the xsd's and use the /si option to generate a service interface.
- The output of that will be an interface file (c# file)
- Go into Visual Studio and create a new project of "WCF Service Application"
- Rename the initial Service1 (and interface) files and classes to the name of your new service and interface.
- Copy and paste the interface code generated above into the new service's interface class file
- In the service class, implement the service methods with whatever implementation you want. (TIP: if you right click on the interface name in the file, you can use the "Implement Interface" option to have VS stub it out for you - nice feature.)
- Build it and you should be ready to go.
In terms of toggling between the original and your new service, I suppose one option would be from the client side reference the different service endponts in code (versus config) and that way you could just toggle a switch to hit one service or the other...
without having to change configurations
That'll be cumbersome.
You could create a service with an identical interface, and then implement a client for calling the 'real' service in your service.
Then in every call you do what you want to do, and after that you forward the request with the same (or altered) parameters to the real service.
But then you'll have to point your clients to your service, instead of the real service. Perhaps that part could be worked around by using a proxy server or a DNS record.
精彩评论