Implement mock service with wsdl tool - c#

I'm currently following this tutorial to learn how to implement a mock web service:
http://iandykes.blogspot.nl/2008/06/creating-mock-web-services-in-net.html
The web service that i'm using is a public web service: http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
So i used the wsdl.exe tool to generate a class for that service, like it is explained in the tutorial. I also added the generated .cs file to my Solution project.
I then did the following in Visual Studio to add the Web Service:
Right clicked on my project -> Add Service Reference -> Advanced -> Add Web Reference
In there i added the URL to the web service and then hit the Add Reference button. So now i have a Web References folder in my project with a CurrencyProxy in it (that is how i named it).
The next step that i have to do according to the tutorial is:
In the code behind for this Web Service, change the class definition
so that it implements the interface in the generated code.
That is where i'm actually stuck. Where can i find the code behind file of the Web Service? I'm not sure what to do here.
Could anyone help me out?

When you add Web Service to your project, there will be YourWebService.asmx and YourWebService.asmx.cs under it. In YourWebService.asmx.cs, just replace System.Web.Services.WebService with your interface, which was generated earlier. Implement this interface as you wish and that's it, mock service's done.

Related

How to create a mock WCF service in Visual Studio 2017

I have a decompiled client that calls a web service, the only configuration parameter is the endpoint url the rest is all in the service class. I wanted to simulate the service that calls starting from his wsdl I managed to do it with a visual studio 2017 web project and adding the .asmx file and implementing the methods from the interface generated by the wsdl. Basically I created a mock as a rule is made in soapui.
The question is: how can I do the same thing using a WCF project?
I Have followed this Post:
How to use a WSDL file to create a WCF service (not make a call)
But generated files (svcutil /sc) works with internal console test but decompiled client call (I can't modify config client - remember with .asmx works) show this message :
Failed to process the message with Action '' due to a ContractFilter mismatch in EndpointDispatcher
Thank you.

Debugging WebMethod()s - stepping into code from the calling project

I used a decompiler to retrieve lost source code for a set of web services written long ago. I want to debug the retrieved web methods using an existing web forms project. The two projects my solution contains are: 1) The recovered web services project and 2) The calling web forms project (created years ago) that references the live web services.
I created a project reference from the web forms project to the new web service project. My goal is make temporary changes in the calling code to allow me to step into each WebMethod using the project reference instead of the live web service reference. I'm hoping to debug the WebMethods as if I was debugging a standard class library.
The problem is that none of the WebMethods are visible using the project reference. Only the containing class is visible in the calling project but none of the Public Subs or Functions show via intellisense. How can I debug these WebMethods using the existing calling code from the web forms project?
Here is one of the simpler calling routines:
Private Sub LoadCountryCode()
Dim liveWebService As New EPriceBookWebService.Service1SoapClient
'Next line TEMPORARILY commented out for testing...
'liveWebService.GetCustomerCountry(lstbxCusCodes.SelectedValue, _countryCode)
'Project reference below shows only the web service class, not the contained public methods via intellisene. WHY?
EPriceBookService.EPriceBookService.
End Sub
Here is the stub of the WebMethod() being called:
<WebMethod()>
Public Sub GetCustomerCountry(ByVal CusCode As String, ByRef CountryCode As String)
I'm answering my own question here...
It was unwise for me to use a project reference to debug a web service project. Instead, I've learned how to debug the web service project from the calling web forms project (directly stepping into the code). To do this I made use of an obscure setting in the web service project called "Don't open a page. Wait for a request from an external application." You can turn on this setting from the project property pages under the Web category. I learned about the setting here and here.
My two projects are in the same solution. The web forms project now references two web services 1) the live web service and 2) the localhost web service in my solution. I've added compiler (preprocessor) directives to the web forms project telling it which web service reference to use.
#If DEBUG Then
Dim theWebService As New localhostEPricebookWebReference.EPriceBookService
#Else
Dim theWebService As New EPriceBookWebService.Service1SoapClient
#End If
Be sure you turn off debug mode before deploying your application.

Why my webservice method is not updating its parameters in the wsdl?

I modified my webservices and added one new parameter. I re-compile everything and when I ran in my localhost everything works fine. BUT when I deployed into my IIS server the wsdl is not being updated and it simply do not show my new parameter.
What do I have to do for my wsdl to be updated?
Right click on the web reference and click update. It will update the reference.cs file. Then compile and deploy. Make sure it reference the new reference.cs file.
When you say the wsdl is not being updated to show the new parameter how are you trying to access the wsdl? if you are trying to access http://myserver/myservice/service.asmx?wsdl and the updated wsdl is not showing up, try restarting the web application/website on IIS that hosts the service.asmx. If that doesn't solve it try redeploying the code. If on the other hand you are stating that a client application that is consuming the service you have hosted on IIS cannot see the updated wsdl you need to check the code of the client application not your service. If it has a web reference pointing to the URL where your service is hosted then you need to do what Azhar suggested and right click on the web reference and click update. If it has a proxy class generated by wsdl.exe then regenerate the proxy class by using wsdl.exe as follows
wsdl /namespace:MyCompany.MyApp.Services /out:C:\MyServiceProxy.cs http://myserver/myservice/service.asmx
and update the client project with the new MyServiceProxy.cs file which should contain the method with the updated parameters.

Update Service Reference is not working in WCF

I am using VS 2012.
I add services reference to my WPF project, but when I've changed my services and updated service reference my Reference.svcmap -> Reference.CS file will be blank and I can't use reference anymore. I can see it in project, but I can't use.
My settings of Services References are :-
Reference.cs file
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.17929
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
uncheck the Reuse types in referenced assemblies checkbox and put address of your new service in Address textbox.
While configuring service reference append /mex (address of your end point with mexHttpBinding ) at the end of your service URL.
https://service.domain.com/serivce1.svc/mex
This will enable metadata exchange. So Reference.cs will be modified / added (if you are adding service reference for the first time).
Edit : On analysis I found that this issue happens when a service (which implements one ServiceContract) is exposed via more than one end point.
This issue can be avoided by using different Service Contracts (Interface) for each end point and making the service class (service1) implement all these Interfaces.
If you cannot browse the URL, then you will not be able to add the service reference. Try hosting your web service locally in IIS. When you're able to browse the URL, then you can add the service reference.
Unchecking the reuse option the Update Service option works fine.
There are few things you can do first Clean the solution, delete the output directory and the service reference. Restart the Visual Studio and re configure the web service.
It can be caused by Team Foundation source control.
Try to right click on the service ref and choose "Check out for edit..."
Than update service reference again.

Calling WCF service from class library: App.Config vs Web.Config?

From an existing WebApplication I use to make calls to WCF services. Proxies for these services was created using Add Service Reference menu. Thus generating >> Web.config in this project.
I have added another class library project to the solution. This project also adds reference to the service. Thus generating >> App.Config file in this project.
I understand, in an N-Tier application, we should have common gateway to the service. Just out of curiosity I would like to know -
For WCF calls originated in WebApp, propagated to class library which config file (App/Web) would be referred
for locating client endpoint configurations ?
The web.config file will be used for locating the service.
The reason is because in this case, the app domain belongs to the web app, not the class library, and the default config file for this app domain is the web.config.
#musefan is correct. It is the web.config that is used.
If you want to, you can split some confuguration sections into seperate files and reference them from the main web.config. You might want to do this so you can maintain the WCF client and server config in a single place to ensure they are consistent.
foe example, if you want to seperate out the <client> section, you would do this:
<client configSource="client.xml" />
Where client.xml is a file containing the relevant client config information.
This blog post tells you how to do it in a bit more detail.
http://blog.andreloker.de/post/2008/06/keep-your-config-clean-with-external-config-files.aspx

Categories

Resources