How to create a mock WCF service in Visual Studio 2017 - c#

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.

Related

C# - How to add a SOAP web service reference to my project?

I am attempting to add a web service reference from an example project into my current VS 2015 C# project. This is what I have in the example project:
To move it over to my new project, I have attempted to copy and paste the "Address" field into my project by right clicking my project and selecting "Add Service Reference". When I do this I get the following error:
There was an error downloading 'http://myserver.com/MyServiceExample.svc?wsdl/$metadata'.
The remote name could not be resolved: 'myserver.com'
Metadata contains a reference that cannot be resolved: 'http://myserver.com/MyServiceExample.svc?wsdl/$metadata'.
There was no endpoint listening at 'http://myserver.com/MyServiceExample.svc?wsdl/$metadata that could accept the message.
This is often caused by an incorrect address or SOAP action.
See InnerException, if present, for more details.
The remote name could not be resolved: 'myserver.com'
If the service is defined in the current solution, try building the solution and adding the service reference again.
How can I go about adding this service reference into my project? I have tried adding it in by using the VS 2015 CMD Prompt with the WSDL /verbose command, but this does not seem to be working. I have the project files and wsdl files for the example, but I cannot get them to work on my solution.
Start your web-service project in iis express by launch debug session then when you get temporary url of your service (like http://localhost:8000/MyServiceExample.svc) you can retrive wsdl from it.
Just replace myserver.com part of url on new one when adding a service reference.
Here you can read how adding service refence is working but in general you need to retrive contract from deployed web service (wsdl) from which vs generates new classes.
Later you should change service url in web.config on deployed one.

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.

web service call fails

I have created a windows service application with C# with .Net 2.0, and added a web service as
http://localhost/postIncidents.ws.php?wsdl
It has the method "ReportIncident". I checked the windows service in local environment.
It worked.
Before I deploy it I added below code to get the particular url from a setting file.
shids_client.shidsWebServer.ReportIncident r = new shids_client.shidsWebServer.ReportIncident();
r.Url = Constant.readUrl();
r.CallReportIncident(time,LocalIPAddress(),type,line);
After I deployed, I added the URL to my setting file as
http://192.168.1.1/postIncidents.ws.php?wsdl
After I check it, it didn't work. the following error was in my log:
"Operation 'CallReportIncident' is not defined in the WSDL for this service"
if i remove it, then it will not show the wsdl xml. it will show only a rendered page.
You use the ?wsdl flag so that Visual Studio can download the WSDL and generate the service proxy. When the proxy actually runs, it runs against http:///postIncidents.ws.php, not ?wsdl needed. So try removing ?wsdl from the URL.

Clear Username Binding (Unable to automaticalli debug wcf service)

Recently I've read an article about own WCF binding with Username and Password authentication.
Here is an article: http://webservices20.blogspot.ru/2008/11/introducing-wcf-clearusernamebinding.html.
This project "Clear Username Binding" works well. But the problem for me is that I want the WCF server to be host in WCF Service Host. (Like if you create in VS any service library from the template it will be run in WCF Service Host). I've done the next thing. I've created a new project (service library) and simply copy your service and contract to this new project. Then I copied the app.config system.serviceModel section and added referenced to ClearUserNameBinding project and MyUserNameValidator. That's all
Next, I run the client application. Everything was ok. WCF Service Host had been started and client application ended as I expected. But the problem is that now if I put a breakpoint somewhere in the service implementation (let it be somewhere inside of the EchoString function) and run again I always get a message in VS IDE "Unable to automatically debug WCFServiceLibrary". I really don't know what to do with this error and how to get rid of it, because I need the debugging to be available for me.
P.S. This error is appearing only when I use ClearUserNameBinding. If I use for example wsHttpBinding it works perfect. And of course I have <system.web> <compilation debug="true" /> turned on in my config file
Thanks in advance!
In visual studio put a breakpoint in the first line of the client and attach to the process "WCF service host". Then you could debug into its breakpoints. Not sure how to do it w/o attaching to another process.
Also you could try to use CUB not as a separate binding but as a custom binding. You will need to create the custom binding from code. Pseudo code:
var b = new CustomBinding();
b.elements.add(new TextEncodingElement())
b.elements.add(new SecurityElement() {SecurityMode="UsernameOverTransport"})
b.elements.add(new AutoSecuredHttpTransportElement())

Implement mock service with wsdl tool

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.

Categories

Resources