开发者

Testing a Web Service

开发者 https://www.devze.com 2023-02-07 20:07 出处:网络
I have a web service application in C#.When开发者_StackOverflow社区 I run the Debug in Visual Studio, it starts the service at http://localhost:###### and shows the Default.htm page with links to test

I have a web service application in C#. When开发者_StackOverflow社区 I run the Debug in Visual Studio, it starts the service at http://localhost:###### and shows the Default.htm page with links to test the different calls that can be made to the web service.

Is there a way to call this locally running version to test it from another computer? I tried making a call that the links on the test page make, but replaced localhost with the IP address of the machine running it, but it said that it could not connect to the server.


The embedded Visual Studio web server (Cassini) is only accessible from the local machine. If you want to use the web service from a different computer, you will need to deploy the service to IIS and then you'll be able to get to it at http://xxx.xxx.xxx.xxx/MyWebService/MyWebService.asmx (replace xxx with your IP Address and MyWebService with the appropriate IIS web site path).

Then to create a test application, you can create either an ASP.NET website or WinForms/Console application and import a Web Service reference in Visual Studio. That will build the proxy classes for you automatically and make calling the service very easy.


Just wrap your service in a console application so it can be accessed.

  1. Create a new console application project in your solution, naming it something that makes sense (If your service is named MyService maybe MysServiceConsoleHost or some facsimile).
  2. Include a reference to the service project in this project.
  3. In your new project (program.cs) have something like the following:

using System;
using System.ServiceModel;

class Program
{
  static String TITLE_TEXT = "MyService -- Console Host ({0})" + (System.Diagnostics.Debugger.IsAttached?" [DEBUG]":"");
  static void Main(string[] args)
  {
    Console.Title = String.Format(TITLE_TEXT, "Not Running");
    try
    {
      ServiceHost host = new ServiceHost(typeof(MyService));

      Console.Title = String.Format(TITLE_TEXT, "Starting");
      host.open();

      Console.Title = String.Format(TITLE_TEXT, "Running");
      Console.WriteLine("Service is started, press any key to exit.");
      Console.ReadKey();

      Console.Title = String.Format(TITLE_TEXT, "Closing");
      host.close();
      host = null;
      Console.Title = String.Format(TITLE_TEXT, "Closed");
    }
    catch (Exception ex)
    {
      Console.Title = String.Format(TITLE_TEXT, "Exception");
      Console.WriteLine("An error occured while running the host:");
      Console.WriteLine(ex.Message);
      Console.WriteLine();
      Console.WriteLine(ex.StackTrace);
      Console.ReadLine();
    }
  }
}

Replace the instances of MyService as necessary, then run it. Make sure you have a .config file that specifies the endpoints of your service, port to run on, etc. Then anyone can access your service and, if necessary, you can debug through the service during live calls.


A really simple solution to this is to use localtunnel:

http://progrium.com/localtunnel/

It will set up a proxy that will point to your localmachine and can be accessed outside of your machine.


If you are using Visual Studio's built in web server (Cassini) then this can only be called locally. If you want to connect remotely you will have to install IIS and configure your project to use it.


Yes and no. If you are using the built in ASP.NET dev server (most likely, since you have :####), I don't believe that can be contacted externally.

But, you can "deploy" to the local IIS instance and it then can be hit from other computers. Web Service Studio is a good test tool for web services. There are also some really good open source tools out there. I like SoapUI myself.


I can test the webservice on localhost. I have a windows CE Motorola terminal, connected to my computer. I have VS2008 on the computer. My computer address is xxx.xxx.x.xxx and webserver is localhost:62209, so at the terminal i use webservice address: http://xxx.xxx.x.xxx/62209/MyWebservice.asmx and it works. Maybe because the terminal is connected directly to the computer wher the webserver runs? Anyway, it works.

0

精彩评论

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