visual studio 2010 web service - c#

can not find the new way of referencing / using the web service.
there is the old way of adding WEB REFERENCE (.net 2.0) but I would like to use the new service reference.
following tutorial: http://sarangasl.blogspot.com/2010/09/create-simple-web-service-in-visual.html or: http://www.youtube.com/watch?v=qOqEKpYbTzw I can do it the old way. and calling the web reference like they say works - but how to do it with a SERVICE REFERENCE instead.
I can make the service reference itself, but don't know how to use it.
anyone can help find a tutorial? or knows what code to use instead of:
(code for: .net 2.0 - web reference)
service1.Service1 s1 = new service1.Service1();
String result s1.HelloWorld();
Trace.WriteLine(result);
(code for: .net 4.0 - service reference)
here is what i tried
Service1.Service1SoapClient s1 = new Service1.Service1SoapClient();
String result = s1.HelloWorld();
Trace.WriteLine(result);
but it gives an error:
Could not find default endpoint element that references contract 'Service1.Service1Soap' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

If you are referring how to add the old asmx style references in vs2010
Right click references and select "Add Service Reference"
then click "Advanced", then click "Add Web Reference"
For a wcf service just simply right click and say "add reference"
to use it - enter in the name of your class. Since I don't know your namespace you may have to include an imports at the top. But basically -
ServiceReference1.WhateverClient cleint = new ServiceReference1.WhateverClient();
When you do ServiceReference1 in your code, you should see the class name that was generated. You just create an instance of that and call it.

The missing paramerters in your class invocation, are in the your Web.Config
Search for the <client> section:
<client>
<endpoint address="http://ioe_test:8080/crypto.asmx" binding="basicHttpBinding"
bindingConfiguration="cryptoSoap" contract="wscol_crypto.cryptoSoap"
name="cryptoSoap" />
</client>
And, the missing parameters are:
endpointConfigurationName corresponds to "name"
remote Address corresponds to "address"
So, in my example should be:
ws_cryp.cryptoSoapClient cryp = new ws_cryp.cryptoSoapClient("cryptoSoap", "http://ioe_test:8080/crypto.asmx");
That should be enough.

Open your app.config and look for the name of the endpoint configuration element generated in there. Then use an overload for the new ServiceClient() call to specify the endpoint.

The issue (found elsewhere) is that I had to move config to the project that initiated the call. Somehow strange (seems out of place now), but now it seems to work.

This is the client section example, in my Web.Config
<client>
<endpoint address="http://ioe_test:8080/crypto.asmx" binding="basicHttpBinding"
bindingConfiguration="cryptoSoap" contract="ws_cryp.cryptoSoap"
name="cryptoSoap" />
</client>
And, the missing parameters are:
endpointConfigurationName corresponds to "name"
remote Address corresponds to "address"

Create a new C# console application project.
Here, I have specified name of the project as “MyFirstWebServiceConsumerApp“.
Click on "Ok" button to proceed further.
Go to Solution Explorer and right click on your Console Application Project Name.
In this case, right click on “MyFirstWebServiceConsumerApp” and select “Add Service Reference…” from the drop down menu.
Click on “Advance” button.
Click on “Add Web Reference..” button.
An Add Web Reference window will appear to consume a web service in c#.net.
Enter here URL of the web service.
Now, click on button(See below image) to retrieve all web service methods in this window.
Enter the Web Service URL and click on -> button to check whether the Web Service URL is valid or not. If the URL is valid, it would show you the available Web Methods and the status will appear as “1 Service Found:”
Enter web service reference name. I used “MyFirstWebServiceReference” as a web service reference name as shown in above image. Now, click on “Add Reference” button..
Now check the Solution Explorer. “MyFirstWebServiceReference” must get added under web reference folder.
The next step is to add Reference of this web service to our c# code, so that we can use this in code files. Add following line in code file consume a web service in c#.net –
using MyFirstWebServiceConsumerApp.MyFirstWebServiceReference;
Add below code to call the web method.
So the final code will appear as:
using System;
using System.Collections.Generic;enter code here
using System.Linq;
using System.Text;
using MyFirstWebServiceConsumerApp.MyFirstWebServiceReference;
namespace MyFirstWebServiceConsumerApp
{
class Program
{
static void Main(string[] args)
{
Service1 webService = new Service1();
Console.WriteLine(webService.MyFirstWebMethod("Dhiraj”, “Kumar”));
Console.ReadLine();
}
}
}
Now save the files and Execute the application. Press F5 button on your keyboard to execute the code. The result should appear as below screenshot –
Your fi­rst web service consumer console application is ready.

Related

Methods not showing up in WCF Client

I created a WCF ServiceContract with a few methods but when I add the reference to the client none of them show up. The WCF Test Client loads correctly and all the methods show up there. They also show up in the 'Add Service Reference' window when I try to add the service reference and click 'Go' ('Discover' does not bring anything up). What may be keeping the Interface on the client side empty?
Here is the code for the service with one method:
namespace WS
{
[ServiceContract]
public interface Itest
{
[OperationContract] method_name(int num);
}
}
The client app recognizes the namespace but does not find any methods in it.
Thanks!
Try following things.
Make sure your service is working using test client in visual studio.
Update your service reference by right clicking on it.
Or delete it and add new service reference again.
Can you try using svcutil.exe and see if you are able to create a proxy file. If you are then you can try referencing that in you client project.
something like:
svcutil.exe <<url where service is deployed>> /o:ClientProxy.cs

config web service from code behind

I am creating a windows mobile 6 application which will consume a web service (.asmx) for different clients.
As I know, I will need to manually “Add Web Reference”; then I will be able to call those functions.
Is it possible to configure web reference as a variable from code behind?
That way I can keep the url of web service in a text file. For different client, I just need to edit that text file instead of recompile that application again.
You'll have to add the Web Reference at design time.
At runtime, you can modify the URL of your target web service using the Url property. Here's an example of pulling the target URL from the app.config:
var ws = new MyWebService();
ws.Url = ConfigurationManager.AppSettings["SomeUrl"].ToString();
The only catch here is that the WSDLs of the design-time and run-time services must match.
Yes, just add something like :
<configuration>
<appSettings>
<add key="WebReference" value="URLofASMX"/>
...
then call it by :
string URL = ConfigurationManager.AppSettings["WebReference"].ToString();
You'll need to possibly add a new reference to System.Configuration to the project if you can't access ConfigurationManager just by including System.Configuration.

SOAP header Action was not understood

I am trying to consume a webservice in C#. Whenever i try to call the function from the web service class I am getting a "SOAP header Action was not understood".I've added web reference[not service reference] pointing the web service in my project. The following steps were taken to add the web reference
1) right click on the project -> Add WebReference
when i examined the web service in web browser i found this in the header
<wsdl:definitions name="MyService" targetNamespace="http://tempuri.org/">
<wsp:Policy wsu:Id="WSHttpBinding_ICAIService_policy">
<wsp:ExactlyOne><wsp:All><sp:TransportBinding>
<wsp:Policy>
I've done the following code to call the web service functions
WebStruct webS = new WebStruct();
webS.Name = "Peter";
webS.ID = 22;
webS.Find(webS);
Remove the Web Reference and add a Service Reference instead.
Try to use simple service endpoint binding BasicHttpBinding. This binding is compatible with .NET 2.0.
Can you check with your webservice provider? Seems they need some customized header information to process your request. Adding such headers by the service providers are very much common to increase the security.
SoapAction errors only went away when I went into the "Configure Service Reference..." properties and updated the Service Reference with these options ticked.
and ticked these two boxes:
TICK Generate asynchronous operations
TICK Always generate message contracts

Connect to Unknown SOAP Web Service

I would like to build an app in C# that connects to an Apache AXIS web service and performs the following operations via SOAP.
Login in to the server.
POST string data to server
Receive and display server response
Here's the tough part. I do not have access to the server, nor do I know where the .JWS file is located on the server. I was able to get to the WSDL file in my web browser, so I know a "Login" operation exists as well as an operation to take in data.
I have tried accessing the web service via URL, but I keep getting this message:
Hi there, this is an AXIS service!
Perhaps there will be a form for
invoking the service here...
In summary, is there anyway I can connect to this web service when all I have is the URL of the WSDL file? Are web services accessible via URL?
Thank you
Use WCF, and generate client proxies to the web service using the svcutil.exe tool.
running svcutil.exe http://url.to/webservice?WSDL the_wsdl.wsdl /language:C# should generate proxy classes you can use in your C# project, and you'd call the service e.g. like
BasicHttpBinding myBinding = new BasicHttpBinding(); //might not even need these
// 2 lines if you ran svcutil.exe directly on the web service URL
EndpointAddress myEndpoint = new EndpointAddress("http://url.to/webservice");
TestClient client = new TestClient(myBinding,myEndpoint); //the generated classes
// svcutil.exe created
client.SomeOperation(42); // call an SomeOperation of the web service
Thanks for everyone's help. Looking back on this question, I can see how severely confused I was. Here is the solution I followed.
Assuming you know the URL of the WSDL file of the service you wish to connect to then just do the following.
Boot up Visual Studio
On the top toolbar navigate to Data -> Add New Data Source then choose Service in the new dialog
In the address bar, enter the URL of the wsdl file (EXAMPLE: http://server.com/services/displayName?wsdl)
Near the bottom of the dialog, change the namespace to something relevant to the project (EXAMPLE: sampleService)
Now Visual Studio should compile the client proxies for you that you can use to access the web services on your server. To access one of the services, all you need to do is create a new object from the class.
//Example
sampleService.ClassName test = new sampleService.ClassName();
test.displayName("Jack");
See http://msdn.microsoft.com/en-us/library/bb552364.aspx for a starting point

Getting an Instance of your Web Service Reference

So I believe all you have to do with .NET 2.0 vanilla web services (not WCF) is the following:
1) Add your service reference. In my case I'm using the PayPal WSDL
2) Before you can use any proxy class, you must first create an instance of your service reference
3) Once you create an instance of your service reference, then just do [servicereference].ProxyClassName.Method or whatever you're trying to access from those classes
right?
Ok, so I tried that. I added a service reference and named it SandboxSoapAPI. So that's what you see under references in my C# project.
In code I tried this:
SandboxSoapApi reference = new SandboxSoapApi();
but it doesn't recognize SandboxSoapAPI. Am I doing something wrong? I just want to start calling class methods, etc. with PayPal and I can't seem to get this right.
And if I'm not incorrect, as of .NET 2.0+ it handles the low level sending of the actual request over Http for SOAP web service references?
SandboxSoapAPI is not the SOAP client proxy type name. It's a namespace.
To check this, in VS.NET tick 'show all files' and drill into the Web References, open up the Reference.cs file, you will see the SandboxSoapApi is a subnamespace (not your SOAP client proxy name!) in the project's root namespace.
So either use the fully qualified name:
SandboxSoapAPI.YourProxyType client = new SanboxSoapAPI.YourProxyType();
Or use using SandboxSoapAPI; in your code where you need the SOAP client.

Categories