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

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.

Related

How can I use a web service from local WSDL file?

I have been given a WSDL file from a provider and I have added it as a Service Reference to my C# project in Visual Studio 2013.
I can see the relevant classes I need but when I call the functions on them nothing is transmitting from my program. I have configured Wireshark to listen but no data is coming from my program when I run it.
Where in Visual Studio can I see the IP address/URL that the web service is trying to connect to? At some point I assume it establishes a HTTP connection, where can I see this code to check the URL/IP address?
The WSDL file does not contain the address of the service endpoint. You probably created a Service Reference or a Web Reference, which has created a client class for you. If you instantiate this client (lets call it ExampleClient) with the default constructor:
var serviceCLient = new ExampleClient();
Then the URI will be the path of the WSDL file you imported. This mostly works fine if you import a generated WSDL file file the actual service URI, but in your case, you need to tell it where the service is running. You can either pass the service URI in the constructor:
var serviceCLient = new ExampleClient("http://example.com/service/endpoint");
Or edit your app.config or web.config (depending on project type). It will have something like this:
<system.serviceModel>
<client>
<endpoint address="C:\path\to\your.wsdl" etc etc etc... />
</client>
</system.serviceModel>
And you should change the address attribute there.
For old style web references, you can right click on the reference in Visual Studio (under your project in the Web References folder, and select "properties". The properties screen contains a "Web Reference Url" which you can edit to point to the actual service URI.

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.

Consuming a WCF Service that is hosted in a Windows Service from outside solution

I've set up a WCF Library hosted in a Windows Service using the following walk-through:
http://msdn.microsoft.com/en-us/library/ff649818.aspx
The consumer winforms is in the same solution, which is located locally on my work PC's C: drive.
The walk-through works i.e. the winforms button gives me the correct answer.
If I create a new Solution on the C-Drive with a single Windows Forms project in it I cannot successfully add a service reference to this running service, i get the following message:
The detailed message says the following:
The URI prefix is not recognized. Metadata contains a reference that
cannot be resolved: 'net.tcp://localhost:8526/Service1'. Could not
connect to net.tcp://localhost:8526/Service1. The connection attempt
lasted for a time span of 00:00:02.0020000. TCP error code 10061: No
connection could be made because the target machine actively refused
it 127.0.0.1:8526. No connection could be made because the target
machine actively refused it 127.0.0.1:8526 If the service is defined
in the current solution, try building the solution and adding the
service reference again.
Why can I add this Service Reference ok to a project within the same Solution as the Service but not from a project in a different solution?
EDIT
My colleague found an error in the MSDN article - I have detailed his find HERE
The step by step walkthrough article at MSDN ends unfortunately where it gets interesting, so let's continue here. Because there are many possibilities which may cause the error, I've described several options (= scencarios which may cause the issue) below, which should help troubleshooting:
1st option: Try to specify
net.tcp://localhost:8526/Service1/mex
when you add the service reference to your new client - ensure that the service is installed and running before you do that.
Explanation: The suffix "mex" stands for "metadata exchange" and allows Visual Studio to download details of the WCF contract. This suffix is also used in the walk-through example, it was added automatically (you will see it in the Address field if re-open the added service reference by right-clicking on "Configure Service-Reference...").
2nd option: What I noticed when I tested the walk-through is that it helps sometimes to right-click on the service reference and select in the contect menu "Update Service-Reference".
After a while in the systray you can see the balloon message "Your service(s) have been hosted.", after which you can start the client within the same solution. In this case, the service has been temporarily created but is not deployed permanently - which means, if you stop debugging, it is removed. As a result, you can't use this service from a remote PC, it is just visible within the solution in Visual Studio. Visual Studio internally invokes the tool
WcfSvcHost.Exe /Service:<Service1Binary> /Configuration:<Service1Config>
supporting it with the right parameters to register the service properly (you can find this tool in Visual Studio's Common7\IDE subdirectory, and there is also WcfTestClient.Exe available - a tool which acts as a client, very useful to debug WCF).
For instance, if you have stopped debugging, and launch the client.exe from Windows Explorer outside of Visual Studio, then it does not find the service and you're getting exactly the error message you have described in your question.
There are two interesting links regarding this matter at Microsoft:
Problem with Metadata Exchange and Publishing Metadata
Note that this is different from deploying it as described in the 3rd option.
3rd option: Have you used InstallUtil to deploy the service? In this case it can happen that you have accidently removed the [...]/bin/Debug subdirectory and the service fails to start, because the .EXE file is missing.
Note: This can be avoided if you're using a ServiceInstaller project, which copies the binaries before the service is registered. Or - if you want to use InstallUtil for simplicity - you can copy the service binaries to a target directory (including the .config files and .dlls) before you register it.
4th option: If you run the service on a remote computer, you need to specify the proper host name or IP address of the host instead of localhost, and you need to ensure that the personal firewall (windows firewall or 3rd party) doesn't block the port 8526 (the port number which was used in the example). Specify an exception to allow this port for incoming and outgoing traffic.
5th and final option (UPDATE): Naming conflict - Service1 is the service but also the class name in the Wcf library. Either fully qualify the class name you're using from the WCF library in the service, i.e. WcfServiceLibrary1.Service1 or rename the class. Whytheq has found it himself with a colleague and as posted it here.
More reading: Check out this article, which I've found recently: "WCF: a few tips". It explains very well troubleshooting WCF. The only change I would made to the console hosting example is to replace the using statement by a
ServiceHost host = new ServiceHost(typeof(Service));
try
{
host.Open();
Console.WriteLine("WCF Service is ready for requests." +
"Press any key to close the service.");
Console.WriteLine();
Console.Read();
Console.WriteLine("Closing service...");
}
finally
{
if (host!=null) {
host.Close();
host=null;
}
}
If you want to know more about the reason why, check out this article: "Proxy open and close".
You can get round this as follows:
Browse the service's WSDL URL and save the WSDL to a local file.
Then make the following changes to the file:
Remove the namespace prefix from the name used for the wsdl:binding i.e. change
name="wb:wsclocks-inboundSoapBinding" to be
name="wsclocks-inboundSoapBinding"
Change the binding attribute of the wsdl:port attribute to match, and also remove the namespace prefix from the value of the name attribute, so it is just wsclocks-inbound.
Then run svcutil /o:Client\WBServices /noConfig

C# client how to invoke wsdl file

My customer gave me a .wsdl file to access their webservices. Using VS2008 I can create a project, but I don't know how to use the .wsdl file in it.
You don't invoke WSDL file, you add service reference from the file.
To add reference, right click on the project, select Add Service Reference. Paste path to your wsdl file and hit Go.
If you want to use legacy Web Service client, select Add Web Reference and paste path to the wsdl file from there.
I recommend to use WCF (Add Service Reference option).
To use the service reference add code like this:
var serviceClient = new ServiceReferenceName.MyClassClient();
serviceClient.DoSomething();
You also need to update config file with the server URL that you customer should provide you with:
<client>
<endpoint address="http://UrlFromYourCustomerHere"
binding="basicHttpBinding"
bindingConfiguration="xxx"
contract="MyServiceReference.xxx"
name="xxx/>
</client>
A Web reference enables a project to
consume one or more XML Web services.
Use the Add Web Reference Dialog Box
to search for Web services locally, on
a local area network, or on the
Internet.
After adding a Web reference to your
current project, you can call any
methods exposed by the Web service.
To add a Web Reference
On the Project menu, click Add Web Reference.
In the URL box of the Add Web Reference dialog box, type the URL to obtain the service description of the Excel Web Services, such as http:////_vti_bin/excelservice.asmx or http:///_vti_bin/excelservice.asmx. Then click Go to retrieve information about the Web service.
Note Note:
You can also open the Add Web Reference dialog box in the Solution Explorer pane by right-clicking References and selecting Add Web Reference.
In the Web reference name box, rename the Web reference to ExcelWebService.
Click Add Reference to add a Web reference for the target Web service.
Visual Studio downloads the service description and generates a proxy class to interface between your application and Excel Web Services.
Read
How to: Add and Remove Web References

Categories

Resources