开发者

visual studio 2010 web service

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-ser开发者_JAVA百科vice-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"


  1. Create a new C# console application project. Here, I have specified name of the project as “MyFirstWebServiceConsumerApp“. Click on "Ok" button to proceed further.

  2. 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.

  3. Click on “Advance” button.

  4. Click on “Add Web Reference..” button.

  5. 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.

  6. 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:”

  7. 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..

  8. Now check the Solution Explorer. “MyFirstWebServiceReference” must get added under web reference folder.

  9. 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;

  10. 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();

        }

    }

}
  1. 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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜