I'm going to use C# to read data from a few webservices. I've done that many times before, but those times I've had direct access to the webservices from my development machine.
In this project I've just been sent a .wsdl file, and a couple of .xsd files for the webservice they have in their local intranet.
I've seen that I can use "Add Web Reference", and point directly to the .wsdl file, so that a C# class is created.
But how can I really test it? I'd like to return some dummy data that I can visualize while I develop. Any tips for this situation?
Mock Webservice
From the above source, a .cs file can be generated by opening a Visual Studio Command Prompt and running something like the following:
wsdl /language:CS /namespace:Your.Namespace.Here
/out:Directory\To\Save\To\ /protocol:SOAP /serverinterface
finally-your-wsdl-file-here.wsdl
You can also try SOAP UI for mocking up service with ease. (Free web service testing tool)
Check:
http://www.soapui.org/gettingstarted/mocking.html
The svcutil.exe tool bundled with the Windows SDK (found at C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin) is a nice command line tool that generates WCF client proxies. I've found this to be a good way to interrogate a WSDL. It'll create an interface for the service and then a proxy class that implements that interface. You can then mock up something else that implements that interface to facilitate testing.
It is easy to create your own server side stub. Assuming you want to do it with WCF, then go here. You can then add some basic logic to get your client working.
You can also use the legacy Web Service functionality via the WSDL tool's /Server option, though I recommend you use WCF.
You may also want to try a product like "Fiddler" (http://www.fiddler2.com)
It allows you to capture HTTP (or HTTPS) packets and send a fake automated reply file back as if the server had sent the response. I use it for my project and it works wonders when the test server goes offline (which is often). I take an old response packet, save it as a text file, then send it back again and the application I'm building has no idea it didn't come from the actual host.
I've also found this article very useful: https://ivangrigoryev.com/en/how-to-mock-a-web-service-in-dot-net/
It describes the steps needed, from downloading the WSDL file and to the creation of the test project:
Get WSDL somehow
In the VS Command Prompt:
wsdl /language:CS /out:C:\Downloads\ /protocol:SOAP /serverinterface C:\Downloads\webservice.wsdl
Create a new ASP .NET Web Application with an empty template
Add the file, generated on step 2, to the project
Add a new service to the project (Add -> New item -> Web Service (ASMX))
Change the class definition in the code for this added web service, so it implements the right interface, e.g replace System.Web.Services.WebService with yours.
Hit Alt+Enter and implement the interface automatically with stubs.
Hit F5, the service will run and your browser will open, copy the URL and use it instead of the real URL.
This works only in .Net Framework since .Net Core and .Net 5 dropped the WCF server support. But I found it acceptable to have the main project on .Net 5, but this mock service on .Net Fw 4.8
Related
I have a lot of SOAP/XML and REST/JSON experience in Java and C++, but am pretty much a newbie in .NET. I have to create a SOAP client from a WSDL in C# in VS 2012. The app is not a web-based app, but a console app that will be run as a cron job every 24 hours. It has to query a Web Service for a token, do a client database lookup, and then use the token to update a list of client id's on the Web Service with any new ones - two calls only.
The company has a tester where I can type in either SOAP message (envelope and contents) by hand, click the run button, and a window shows the correct response in its SOAP envelope. My only confusion would seem to be endpoint-related. A WSDL-generated client should take care of everything.
I don't know much about C# (5), the .NET framework (4.5.x), or the newer .NET versions of VS (I've been using Eclipse, IntelliJ IDEA, and even jEdit for the past decade and more).
I've seen a dozen different "solutions" to this problem, ranging from WSDL.EXE and SiteUtil.Exe through adding the WSDL file as a (web?) reference or using one of the NuGet addons. The problem is that every solution I've found appears to asssume the client app is built on one of the web templates. I have to do this as a background .exe in plain C# without any web-based support or interaction in my app.
Any suggestions on the best (hopefully simplest) way to generate client source code?
You are not alone in being confused. You have to realize classic SOAP (asmx/wsdl) is considered an "outdated" technology (by Microsoft), so these days it hidden away in the toolset in favor of newer technologies. The most basic approach is to add it as a Reference from the solution explorer within Visual Studio itself. Here is a step-by-step:
Create a console application
In the solution explorer, right-click the References node and choose Add Service Reference
Ignore most of the dialog that comes up, and go straight for Advanced:
From the bottom of the Service Reference Setttings, choose Add Web Reference...
Now fill in the location of your ASMX, hit the little arrow, wait a bit for the tooling to discover the service, then hit Add Reference
This will add a Web Reference to your project that you can then use to access the methods of the webservice.
[Edit] (to answer your question)
If you have a .WSDL file on disk, you simply enter the file location on disk in the URL box of the Add Web Reference dialog:
In that case, the generated service has a default namespace of WebReference (unless you change it, of course), and you'll most likely want to explicitly set the URL:
var service = new WebReference.GlobalWeather {Url = "http://www.webservicex.net/globalweather.asmx"};
var weather = service.GetWeather("Amsterdam", "Netherlands");
Console.WriteLine(weather);
Is in VS something like this? I need to create simple server connected to DB that will organize all data. I plan to use it for mobile app. This app will have its own, native grapic interface, so I don't need any HTML's in my project.
Why am I asking? Because when I create new web project, VS automatically creates some HTML&CSS files, all stuff that browser needs. But I won't use browser, I need siple console that will show some returned data.
Something like in node.js: single, one executable file.
What you probably want is a self-hosted ASP.NET Web API application. Self-hosted means that you are running your own, minimal web server, no IIS required. To accomplish this, take a look at this tutorial:
http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
There are multiple ways to do what you're asking.
The easiest way is to start a WebAPI project: http://www.asp.net/web-api. You can basically provide nothing but service "endpoints" for your mobile UI.
The other way is to host your own endpoints in a Console application via the use of, say, a HttpListener object. That's setting yourself up for pain though. I would recommend the WebAPI route.
I have written a commercial WCF web service.
I would like to give the service to another person to test independent of the application that will be using the web service. This is because a third party is going to be building that app and we don't have access to it at the moment.
Another thing to remember too is that the person I want to hand this off to for testing is a non programmer. That is important.
So are there any tools out there that can subscribe to a WCF web service, and recognise what data needs to be inserted, and create a GUI to try out different combinations of data?
Thanks.
I typically use SoapUI for that purpose. You can also create test suits with it and it's free. Alternatively you can use the Microsoft WcfTestClient.exe that comes with Visual Studio since version 2008 I guess...
WCF Test Client (WcfTestClient.exe)
You can find the WCF Test Client (WcfTestClient.exe) in the following location: **C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE**
Using the WCFTestClient Tool to Test Service Operations
You can consume a web service with Office InfoPath, or write a simple test application in C#.
In general, you need to publish the web service meta data (normally as a wsdl end point) - this metadata should enable any client to get all the information needed to make calls to your web service.
This may help: http://msdn.microsoft.com/en-us/library/ms734765.aspx
As may this: http://keithelder.net/blog/archive/2008/01/17/Exposing-a-WCF-Service-With-Multiple-Bindings-and-Endpoints.aspx
Update:
From the comment, is appears that you need non programmers to be able to test your service and are looking for a user interface that can be used to work directly with your web service (and automatically adapt to changes). As far as I know, there is nothing built it that will do this for you (though the old asmx services created web forms that could be used).
So you are looking for application that will create UI for manual testing of your service? Not sure if something like that is available for free. SoapUI PRO is able to do that but in contrast to common SoapUI it is not free. Basic SoapUI version requires tester to write XML messages directly.
You should think about your requirement. You want tester without any programming knowledge to test artificat which is for programmers - not for end users. You probably have to buy some tool or write your own solution to support such test.
Greetings!!
I am new to wcf and have some doubts.
I am able to create one wcf service and also able to consume that service in a client application using "add service reference".. now my question is why do we need SvcUtil.exe to create /generate proxy class and then consumed by client, the same thing can be done using 'Add ref' easily. What is the exact purpose.
You don't - I'm fairly sure they're equivalent. Obviously svcutil can be run from the command line whereas 'Add Web Reference' only works in Visual Studio but they do roughly the same thing.
We use svcutil not add web reference because the huge raft of files it creates became a version control nightmare.
I want to turn a program I have into a service so I can use it without logging it. Basically what it does is it backs up specified folders to a specified location using SSH. However the problem I'm running into is I don't know how to tell it these items. I only know how to start, stop, and run a custom command with only an integer with a parameter.
How can I do this?
Windows Service, not a Web Service
edit: The folders it backs up will not remain consistent and will be updated at every runtime
You can instantiate your service and pass command line arguments using the ServiceController class.
using (ServiceController serviceController = new ServiceController(serviceName))
{
string[] args = new string[1];
args[0] = "arg1";
serviceController.Start(args);
}
"arg1" will then be available as regular command line arguments in main() when Windows starts up the service.
I see that you (or someone) voted Sebastian Sedlak's answer down, because he mentioned hosting a WCF Service in the Windows Service. Your reply was
It's in nice bold lettering in the question. Not a Web Service, therefor WCF is out of the question
I think you misunderstood what he meant. He wasn't talking about a Web Service. He was talking about hosting a WCF Service within your Windows Service.
It's far from the same thing. You can host a WCF Service within any Windows (Forms/Console/Service) application. The point of doing so, is that the application is then reachable for communciation via its internal WCF Service, in the same fashion as you can communicate with a Web Service (you can also host WCF Services in IIS, btw, which would then make them "Web Services", in the sense you seem to be referring to).
In a Windows Service, this means you can send any command to it and also get any information you want from it - while it's running.
In fact, I am working on a project right now, which is a Windows Service that I need to be able to contact and pass commands to - and get information from - at runtime. For example, I want to be able to tell it where to store certain things, what to log, to have it reset/restart - and poll it for status messages. I do this by hosting a WCF Service inside the Windows Service. That WCF Service exposes a set of methods, that in my case includes receiving commands and returning status information. So when the Windows Service is running, I can contact it (even remotely), via its built-in WCF Service and tell it what to do.
This an extremely easy thing to implement, and in the case of Windows Services, can provide you with a much richer interface to the Service than through the basic standard commands.
However, you specified that you wanted your Windows Service to receive its folder settoings each time it starts up, which makes such a passive setup less than ideal (as it would be unable to do anything until you passed it the right folders).
One way to deal with this (using a hosted WCF Service), would be to have the Windows Service running all the time (i.e. automatic startup). Its default state would be idle. Then you could issue it a "start processing"-command, feeding it the correct folders to work on (through a call to the corresponding WCF Service method). Similarly, the WCF Service would expose methods giving you the status of the application (current folder, progress, busy/idle etc). Once the processing is done, it would go back into the idle state, waiting for the next set of folders to be supplied to it.
Doing it this way would make it very easy to control remotely - you could even make an online administration panel for it, accessible from anywhere.
The issue, is that, while passing in parameters is not difficult, when the machine restarts and windows tries to restart the service, those parameters are not there. they only exist when someone starts the service from the command line.
for example. I have a windows service which hosts a WCF service. I want the users to be able to specify a non-default port number for the WCF service to listen on. They do this by starting the windows service like so... MyService -port:xxxxx
Which works fine, until the server is rebooted, then windows restarts MyService (but without parameters) and the wcf service defaults to original port #
Any service is capable of receiving command line arguments at start-up.
Would it be possible to use a configuration file to specify these items?
Store the service's startup parameters in the registry: and then, when the registry starts, it should read its startup parameters from the registry.
Windows services have executables like any other. I believe you can write it to accept command-line parameters and specify those parameters in the Windows Service configuration. You can also have it read a config file. If you're using .NET, there are config file classes in the framework.
Why not just Host a WCF Service in the Windows Service to obatain such "admin" functions?
(Remoting is also possible)
RE: config file.
Of course a config file can be used.
And the file can be changed while the service is running.
This would be a nice solution if the config file changes in fact.
All my services use an XML config file, wrapped in a class for easy reuse.
The wrapper has an option to monitor the XML file using fileMonitor for changes, optionally refreshing the content of the config file automatically, and finally raises an event to the service class.
The service then has the option of "resetting" itself as needed to incorporate the new values in the XML configuration file.
Placing configuration into the registry has a few issues:
Security (ie: installer being granted access), depending on what tree is used
The service will not be aware of changes
Portability - although minor as the install should setup registry settings
Where an XML file is easy to copy, edit, share, view and understand.
Throw in some good COMMENT blocks and a detailed XSD file, and it becomes a source of good documentation too.
Look into XPath for easy navigation and extraction of values within the XML file.
$0.02
... david ...
Concerning the app.config file - I'm rather sure that your service will read and use those files as I write all my windows-services this way ;)
So just put everything you need in the app.config under "application" (not user) and put allway edit the "yourname.exe.config" in the folder where you "InstallUtil" the service from.