tags:

views:

19

answers:

1

I'm creating SOAP Request using the following code

    public static string SubmitSoapRequest(string url,
                                                        string ns,
                                                        string operation,
                                                        string requestXml,
                                                        bool responseNodeOnly)
    {
        // make sure the namespace has a trailing slash
        ns = ns.TrimEnd('/') + '/';

        // format soap request
        string soap = string.Format(SOAP_REQUEST_XML, ns, operation, requestXml);

        // format soap action
        string soapAction = string.Format(@"{0}{1}", ns, operation);

        // initialize web request
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

        // add header and other soap params
        req.Headers.Add("SOAPAction", soapAction);
        req.ContentType = @"text/xml;charset=""utf-8""";
        req.Accept = "text/xml";
        req.Method = "POST";

        // make soap request
        using (Stream s = req.GetRequestStream())
        using (StreamWriter sw = new StreamWriter(s))
            sw.Write(soap);

        // get response from remote web-service
        WebResponse response = req.GetResponse();

this works fine when running from a compiled windows app, but fails when calling from a Windows Service. A '500 Internal Server Error' is caught from the exception.

is there any reason why running via a windows service could be causing this?

thanks in advance for any help / advice

A: 

Without seeing what's going on with the service, my initial guess is that it's credentials related. Does the remote service require specific credentials for access? When you're running the code as a windows app, the credentials being passed to the service are probably your network credentials; but the service is passing it's credentials (network service).

You may want to set the appropriate credentials in your request before making the service request. Here's an example that I got off MSDN:

CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(new Uri("http://foo.com/"),"Basic", new NetworkCredential("user1","passwd1","domain1"));
WebRequest myWebRequest = WebRequest.Create("http://foo.com");
NetworkCredential myCredential = myCredentialCache.GetCredential(new Uri("http://foo.com"),"Basic");
myWebRequest.Credentials = myCredential; 
WebResponse myWebResponse = myWebRequest.GetResponse();

Without seeing the service, this would be something to initially investigate based on the situation you described. I hope this helps!!

David Hoerster