How to reference a Web Service from an ASP.NET MVC project? - c#

I want to add a web service to my ASP.NET MVC 5 project.
I do that by right click on the project in solution explorer > add > service reference.
Then I add a web service url like this and it creates a new file in Service References folder called ServiceReference1
Then I'm able to call web service methods in C# by using ServiceReference1 namespace and VS intellisense works.
The problem is that when I add this web service url, it adds a new namespace (ServiceReference2) to that folder, but I can't call the new namespace and it says ServiceReference2 namespace could not be found.
Is it me doing something wrong or the web service has a problem?

After you open your service window, click on the advanced button like below.
In general, I agree it's best not to use the older versions of web-services like I'm showing here, but in real life people have deadlines and such.
Saying that though, there is no huge drawback to using this tech, as long as performance isn't an issue.
Then, click 'add Web Reference' like the picture below.
Then enter URL and click on the little arrow button. Name your reference and click 'Add Reference'. see pic below

Related

Adding and consuming web reference using wsdl definition in asp.net client

First let me say that I'm new to web services and though I have google'd and watched tutorials I"m still having issues figuring out why I cannot consume the service. I'm not exactly sure how to ask the question I'm trying to lay it out nicely. If I've missed some needed info I'll be happy to add it to the question. Thanks for your time.
I was given a service reference to consume of https://demo.cashlinc.com/IntegrationService/Service.svc to consume.
I then proceeded to perform the following steps from visual studio...
From the client (consumer) application RMB > Add Service Reference...
From the Add Service UI, Address: https://demo.cashlinc.com/IntegrationService/Service.svc?wsdl , namespace = DivDatService
The service reference structure is like this...
Now here is the part where I'm fumbling...
There is for example a method GetPaymentProfiles...now that I've added the web reference how do I call the method?

how to reference web service with wsdl file only

I'm writing asp.net/c# project and need to call web services which other client provide. I've found that most people right click "service reference" to add url and use it. The thing is, I got .wsdl file only, is there any way to use it directly?
I've tried the solutions in here and they didn't work.
Thank you for answering.

How to add vb webservice in C# project

Hi i am new to C# web services,
I need a clarification. In my company , i was given a webservice url (http://servicename/psWs/psWs.asmx) and the methods in this service are writtn in VB.net . But my problem is , i know only C# and i am a beginner in C# , is there a way to create C# Web Service or WCF and call the service reference url in VSTS where the web service url methods is written in VB .net. Can u please help to any solution . Your help is more valuable.
Finally what i am trying to achieve is ,
1.create a wcf C# application or asp.net web application
2.Add the reference url (http://servicename/psWs/psWs.asmx) where the methods is written in VB.net
3.Test the web service by giving input and validating the output
Please dont downvote if my question is irrelavant.
Just right click the project (in which you want to add web service reference) and add the web service reference. Language doesn't matters here. From MSDN
To add a Web reference to a project In Solution Explorer,
right-click the name of the project that you want to add the service
to, and then click Add Service Reference. The Add Service Reference
dialog box appears.
In the Add Service Reference dialog box, click
the Advanced button. The Service Reference Settings dialog box
appears.
In the Service Reference Settings dialog box, click Add Web
Reference. The Add Web Reference dialog box appears.
In the URL box,
enter the URL of the Web service to use. If you do not know the URL,
use the links in the browse pane to locate the Web service you want.
In the Web services found at this URL box, select the Web service to use.

Consume web service in asp.net app from a class library

My team and I have a asp.net web forms application and are using several class libraries. In one of those libraries, we are trying to consume a web service. The web reference was added in the web app project and the appropriate references have been added. The app compiles. When attempting to consume said web service in the class library, the credentials don't seem to work, and the call fails. However, if we take the web service call out of the class library, and consume it within the web app, it works.
Any ideas why this is not working in the class library.
Double check your configuration file includes the correct information for the Web service.
Try changing the URL behavior to dynamic as well.
Also, as John stated, I'm assuming you're adding the service to the class library because you intend to use it from the library, as opposed to other areas of the Web application.
"the credentials don't seem to work, and the call fails"...can you give a small stack trace of the error?
Just to clarify, in my current project, we use WCF endpoints within a class library with bindings and credentials. The same can be done for a SOAP ASMX Web reference as you're attempting.
You can add a web service reference by doing the following steps:
right click on the project on the Solution Explorer
click Add Service Reference
click Advanced
you will find "Add Web Reference" at the end of the form
If you are adding the reference in application and then consuming it from class library... How you call the class library.. by adding reference and invoking the method of class library and then how you are accessing proxy from the class library you need to reference it... It seems to me a circular reference. Which shouldn't be compiled at first place... Are you describing your structure correctly???
It's always better to add a simple project with just web reference and then add the reference of this project on all the projects which requires it.
You can add a web service reference by doing the following steps:
right click on the project on the Solution Explorer
click Add Service Reference
click Advanced
you will find "Add Web Reference" at the end of the form
By #AMgdy 's solution,It'll auto generate a Reference.cs class.It defined all of method of webservices.
May be you called it wrong!!
Here is an example:
var serviceName = new ServiceName
{
Credentials = new NetworkCredential("Username", "Password", "Domain"),
Url = "Here you put the correct url of the web service if you published somewhere else"
};
serviceName.CallWebMethod();
make sure that you entered the correct Credential username and password and make sure the you published the webservice to a place you access it.
Have you defined any credential information in a config file in the web app? If so, the class library probably can't fetch them correctly. Just a guess though. And John Saunders is right. Seems a bit backwards reading your description of your apps structure.

Web Reference vs. Service Reference

I just hit a huge brick wall with Paypal. I had created a regular C# project to create some wrapper classes using their WSDL.
If you create a non-web project, the only option you get to add a wsdl is a Web Service Reference. And this builds kinda the same set of proxy classes as a Web Reference would but not really..it adds more that even the PayPal guys are not aware of.
So I was this entire time looking for the right Interface in this list of proxy classes to use as the service (SoapBinding) and the PayPalAPIAASoapBinding was not there I kept telling our PayPal point in contact.
I could only see the following 2 Interfaces that appeared to me what I needed to use since I did not see a PayPalAPIAASoapBinding which you CAN see in a Web Reference based service reference:
PayPalAPIAAInterfaceClient
PayPalAPIInterfaceClient
So I figured out oh, I probably had created a Service Reference vs. a Web Reference which Web Reference is an option in a Web project. But I don't want my service reference tightly coupled to my web project. So that's why I created the C# Project.
So what the heck is a Service Reference vs. Web Reference? And how am I supposed to separate this out into anther project if Service Reference is going to throw me a loop and give me a set of different interfaces than a Web Reference would?
Also, to make things even MORE confusing, VS 2008 has a Web Service Application project.
So what do I use? We're using the .NET 3.5 framework and we're not ready to move to WCF. So can I still use the new Service Reference even if not using WCF or what? IF you're using .NET 3.5 and not WCF yet and you still want to do basic web services, do you still go the Service Reference route and just not use the WCF framework? Meaning can it be used like a .NET 2.0 Web Reference still, just that you're going to get an entirely different generation of the WSDL?
Add Web Reference is the old-style, deprecated ASP.NET webservices (ASMX) technology (using only the XmlSerializer for your stuff) - if you do this, you get an ASMX client for an ASMX web service. You can do this in just about any project (Web App, Web Site, Console App, Winforms - you name it).
Add Service Reference is the new way of doing it, adding a WCF service reference, which gives you a much more advanced, much more flexible service model than just plain old ASMX stuff.
Since you're not ready to move to WCF, you can also still add the old-style web reference, if you really must: when you do a "Add Service Reference", on the dialog that comes up, click on the [Advanced] button in the button left corner:
and on the next dialog that comes up, pick the [Add Web Reference] button at the bottom.
If I understand your question right:
To add a .net 2.0 Web Service Reference instead of a WCF Service Reference, right-click on your project and click 'Add Service Reference.'
Then click "Advanced.." at the bottom left of the dialog.
Then click "Add Web Reference.." on the bottom left of the next dialog.
Now you can add a regular SOAP web reference like you are looking for.
Adding a service reference allows you to create a WCF client, which can be used to talk to a regular web service provided you use the appropriate binding. Adding a web reference will allow you to create only a web service (i.e., SOAP) reference.
If you are absolutely certain you are not ready for WCF (really don't know why) then you should create a regular web service reference.
In the end, both do the same thing. There are some differences in code: Web Services doesn't add a Root namespace of project, but Service Reference adds service classes to the namespace of the project. The ServiceSoapClient class gets a different naming, which is not important. In working with TFS I'd rather use Service Reference because it works better with source control. Both work with SOAP protocols.
I find it better to use the Service Reference because it is new and will thus be better maintained.

Categories