Windows Service使用HTTP Web请求 [英] Windows Service using HTTP web request

查看:132
本文介绍了Windows Service使用HTTP Web请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

大家好,

请问是否有人可以在这方面为我提供指导.
我正在运行Windows服务,该服务实际上请求一个Web URL并发布
xml文件以获取响应.只要它使用asp.net环境或控制台应用程序运行,它就可以正常工作,但是当我使用Windows服务运行它时.坏了我添加了一个日志功能
,以了解我的代码在Web服务中中断的时间.
我发现只要Windows服务运行以下行,它就会中断.

Hi All,

Please if some body can guide me in this regards.
I am running a windows service which actually requests a web URL and post
xml file to recive a response. As long as it runs using asp.net environment or console application
it works fine but when i run it using windows service. It breaks. I have added a log function
there to find out at what point my code is break in webservice.
I have found that whenever the my windows service runs the following line, it breaks.

 

var httpWebRequest =( ) WebRequest

var httpWebRequest = (HttpWebRequest)WebRequest.Create(Uri);

 

它给出的错误
."配置系统无法初始化".

我在这里编写完整的代码供大家参考.请有人帮助我
真的很紧急.在此先感谢您
如果有人可以提供示例示例.

公开

The error it gives
 " Configuration system failed to initialize"

I am writing my complete code here for you guys to see. Please if someone can help me,
it is really very urgent. Many thanks in advance
Please if someone can provide sample example of it.

public

字符串 字符串 @xml)

{

static string GetResponse(string @xml)

{

//**//Debug.WriteLine (xml);

//xml = xml.Replace(" ; \",string.Empty);

DoSomeFileWritingStuff(

DoSomeFileWritingStuff(

" InsideGetResponse" );

"InsideGetResponse");

 

var httpWebRequest =( ) WebRequest

var httpWebRequest = (HttpWebRequest)WebRequest.Create(Uri);  //MyCode breaks here when i runwindows service

DoSomeFileWritingStuff(

"CreateURI" );

httpWebRequest.AllowAutoRedirect = AllowRedirect;

"CreateURI");

httpWebRequest.AllowAutoRedirect = AllowRedirect;

字节 [] requestBytes = .ASCII.GetBytes(@xml);

httpWebRequest.Method =方法;

Byte[] requestBytes = Encoding.ASCII.GetBytes(@xml);

httpWebRequest.Method = Method;

httpWebRequest.ContentType = ContentType;

httpWebRequest.ContentType = ContentType;

httpWebRequest.ContentLength = requestBytes.Length;

httpWebRequest.ContentLength = requestBytes.Length;

httpWebRequest.KeepAlive =

httpWebRequest.KeepAlive =

false ;

DoSomeFileWritingStuff(

false;

DoSomeFileWritingStuff(

开始流式处理API" );

"start to stream API");

 

//向API请求流

尝试

{

使用 ( requestStream = httpWebRequest.GetRequestStream())

{

using (Stream requestStream = httpWebRequest.GetRequestStream())

{

requestStream.Write(requestBytes,0,requestBytes.Length);

requestStream.Write(requestBytes, 0, requestBytes.Length);

}

}

捕获 ( 我们)

{

catch (WebException we)

{

如果 (我们响应== ) 返回 字符串 .Empty;

if (we.Response == null) return string.Empty;

 

;

}

throw;

}

StringBuilder 构建器= StringBuilder

StringBuilder builder = new StringBuilder();

 

尝试

{

使用 ( res =( HttpWebResponse

using (var res = (HttpWebResponse)httpWebRequest.GetResponse())

{

使用 ( sr = StreamReader (res.GetResponseStream(), .ASCII))

{

using (var sr = new StreamReader(res.GetResponseStream(), Encoding.ASCII))

{

builder.Append(sr.ReadToEnd());

builder.Append(sr.ReadToEnd());

}

}

}

捕获 ( webException)

{

catch (WebException webException)

{

//**//Debug.WriteLine (发生远程服务器错误:");

//**//Debug.WriteLine (webException.InnerException);

}

DoSomeFileWritingStuff(

DoSomeFileWritingStuff(

完成获取回复" );

"Finish Get response");

 

返回 builder.ToString();

}

return builder.ToString();

}

#endregion

#endregion

}

推荐答案

您的问题不太清楚..:)

Your question is not that much clear.. :)

无论如何,我想这会有所帮助.

Anyhow I guess this will help..

 

在Windows服务中,不能放置长代码.因为onStart方法必须在一段时间后返回操作系统.尝试在单独的线程中运行您的方法,然后在onStart方法中启动该线程.

In your windows service you cannot put long codes. Because the onStart method has to return to the OS after a while. Try running your method inside a separate thread and start the thread inside your onStart method.

 

 


这篇关于Windows Service使用HTTP Web请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆