2

I'm calling a specific method of my web service, but as soon as I create a new StreamWriter object, I get the above error. I'm not sure why this happens, as I've never done this before. The code I'm using to do this was written using this link - Invoking an ASP.NET web service method via an http request as reference. Here's the snippet -

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/Helpers/Symbology.asmx");
httpWebRequest.Headers.Add("SOAPAction", "\"http://tempuri.org/GetRelatedSymbols\"");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
JavaScriptSerializer serializer = new JavaScriptSerializer();

*using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))*//error occurs here
{
    string json = serializer.Serialize(relatedSymbols);
    streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
string responseText = String.Empty;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
     responseText = streamReader.ReadToEnd();
}

Heres a snippet of my web-service code -

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Symbology: System.Web.Services.WebService
{

    [WebMethod]
    public List<string> GetRelatedSymbols()
    {
        try
        {
             //code here
        }
        catch(){}
    }
}

Can anyone help in pointing out what I may be doing wrong?

Community
  • 1
  • 1
neuDev33
  • 1,573
  • 7
  • 41
  • 54
  • Where is the site being hosted from? – mellamokb Apr 24 '12 at 14:28
  • Currently, I'd like to have it running just on my localhost. – neuDev33 Apr 24 '12 at 14:29
  • What hapens if you paste http://localhost/Helpers/Symbology.asmx into a web browser, do you get the web service dfinition page or some kind of error? – Tim Apr 24 '12 at 14:29
  • Try to call the webservice-methods from the webbrowser. http://localhost/Helpers/Symbology.asmx – HW90 Apr 24 '12 at 14:29
  • @Tim, HW90 - Yes, you are right, it says "Cannot connect to localhost" – neuDev33 Apr 24 '12 at 14:33
  • 1
    @neuDev33: No, I meant where are you hosting it now? It has to be running in VS Dev server, or IIS, etc. in order for you to make web requests to it. – mellamokb Apr 24 '12 at 14:33
  • 1
    That means that you don't hae the web service set up on localhost. If you do that, it should work. – Tim Apr 24 '12 at 14:34
  • @HW90 - What would be the right URL to call the web-service in that case? I tried "~/Helpers/Symbology.asmx", but this did not work either. – neuDev33 Apr 24 '12 at 14:35
  • @Tim - I've written the Web Service myself, so it is present on localhost. If I run the Symbology.asmx page directly, it works fine. However, I'm trying to call it's GetRelatedSymbold method from another place, and that's where i'm running into this error. Do you think it has something to do with the URI? – neuDev33 Apr 24 '12 at 14:36
  • Are you sure, that your service is running? Have a look in your IIS for the correct path. – HW90 Apr 24 '12 at 14:36
  • 1
    If you just run Visual Studio in debug mode, then check you will see it is running. However, the moment you stop debugging in Visual Studio, it's no longer running and can't be accessed by other places. You need to host it for example, in IIS, or keep Visual Studio debugging open on your machine. – mellamokb Apr 24 '12 at 14:39
  • @mellamokb I see what you are saying. I didn't know that, it's my first time writing my own web service. I'll search about hosting it on IIS. – neuDev33 Apr 24 '12 at 14:46

2 Answers2

0

when I run a webserver, I always need to connect with an URL like this http://localhost:PORTNUMBER/...

user1264255
  • 305
  • 1
  • 5
  • 13
0

I just posted an answer about multiple start up projects for another question about a site consuming a webservice.

Some instructions here... Web Reference Help: No connection could be made because the target machine actively refused it

This assumes your both your start up projects are in the same solution.

Community
  • 1
  • 1
Kenneth Ito
  • 5,201
  • 2
  • 25
  • 44