C# 使用statemtn cause error在内部返回语句;并非所有代码路径返回值;
这段代码对我来说很好,我想在using语句中使用return语句并没有什么错,但编译器说并不是所有的代码路径都返回值。我应该如何修复它才能返回字符串结果C# 使用statemtn cause error在内部返回语句;并非所有代码路径返回值;,c#,return,using,C#,Return,Using,这段代码对我来说很好,我想在using语句中使用return语句并没有什么错,但编译器说并不是所有的代码路径都返回值。我应该如何修复它才能返回字符串结果 public static string CallWebService(string an, string xmlcommand) { //test //Dim _url = "http://testapi.interface-xml.com/appservices/ws/Fron
public static string CallWebService(string an, string xmlcommand)
{
//test
//Dim _url = "http://testapi.interface-xml.com/appservices/ws/FrontendService"
//live
dynamic _url = "http://212.170.239.18/appservices/ws/FrontendService";
try
{
string soapResult = null;
XmlDocument soapEnvelopeXml = CreateSoapEnvelope(xmlcommand);
HttpWebRequest webRequest = CreateWebRequest(_url, an);
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
asyncResult.AsyncWaitHandle.WaitOne();
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (BufferedStream bs = new BufferedStream(webResponse.GetResponseStream()))
{
using (GZipStream gz = new GZipStream(bs, CompressionMode.Decompress))
{
using (StreamReader rd = new StreamReader(gz))
{
if (an == "HotelValuedAvailRQ")
{
soapResult = rd.ReadLine();
}
else
{
soapResult = rd.ReadToEnd();
}
return soapResult; //This is what I want to return
}
}
}
}
}
catch (TimeoutException ex)
{
StringBuilder s = new StringBuilder(2000);
s.AppendFormat("<b>К сожалению превышено время ожидания.</b>: {0} <br /><b>Источник</b>: {1}<br /><a href='javascript:history.back();'>Вернуться</a>", ex.Message, ex.InnerException);
HttpContext.Current.Session["bug"] = s.ToString();
HttpContext.Current.Response.Redirect("../errors/error.aspx");
}
}
publicstaticstringcallwebservice(stringan,stringxmlcommand)
{
//试验
//Dim_url=”http://testapi.interface-xml.com/appservices/ws/FrontendService"
//生活
动态url=”http://212.170.239.18/appservices/ws/FrontendService";
尝试
{
字符串soapResult=null;
XmlDocument soapEnvelopeXml=CreateSoapEnvelope(xmlcommand);
HttpWebRequest webRequest=CreateWebRequest(_url,an);
添加(“接受编码”、“gzip、deflate”);
插入SOAPEnvelopeInToWebRequest(SOAPEnvelopeExML,webRequest);
IAsyncResult asyncResult=webRequest.BeginGetResponse(null,null);
asyncResult.AsyncWaitHandle.WaitOne();
使用(WebResponse WebResponse=webRequest.EndGetResponse(asyncResult))
{
使用(BufferedStream bs=new BufferedStream(webResponse.GetResponseStream()))
{
使用(GZipStream gz=newgzipstream(bs,CompressionMode.decompresse))
{
使用(StreamReader rd=新StreamReader(gz))
{
如果(an==“HotelValuedAvailRQ”)
{
soapResult=rd.ReadLine();
}
其他的
{
soapResult=rd.ReadToEnd();
}
return soapResult;//这是我想要返回的
}
}
}
}
}
捕获(TimeoutException例外)
{
StringBuilder s=新StringBuilder(2000);
s、 附录格式(如:信息、内部异常);
HttpContext.Current.Session[“bug”]=s.ToString();
HttpContext.Current.Response.Redirect(“../errors/error.aspx”);
}
}
问题不在于你的使用语句,而在于你的try
语句:异常可能会在try
块中的任何地方中止,然后代码会进入捕获(TimeoutException ex)
处理程序,但是这里没有return
语句
我看到您的代码似乎是web应用程序的一部分,但是这看起来像是一个“后端”方法,将内容返回给消费方法。您不应该从此处写入Response
,而是返回null
或不捕获异常,而是让CallWebService
的调用方负责显示错误消息
我发现您还犯了一些其他错误,例如使用动态
而不是字符串
,这就是我编写代码的方式:
public static string CallWebService(string an, string xmlcommand)
{
String url = @"http://212.170.239.18/appservices/ws/FrontendService";
try
{
String soapResult = null;
XmlDocument soapEnvelopeXml = CreateSoapEnvelope(xmlcommand);
HttpWebRequest webRequest = CreateWebRequest(_url, an);
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
using(HttpWebResponse response = webRequest.GetResponse())
using (Stream s = webResponse.GetResponseStream())
using (StreamReader rd = new StreamReader(gz))
{
if (an == "HotelValuedAvailRQ")
{
soapResult = rd.ReadLine();
}
else
{
soapResult = rd.ReadToEnd();
}
return soapResult;
}
}
catch (WebException)
{
return null;
}
catch (TimeoutException)
{
return null;
}
}
- 您错误地使用了
dynamic
<代码>动态
与VB的Dim
语句不等价
您使用的是HttpWebRequest
的异步方法,但使用的是阻塞,因此您最好只使用阻塞方法(GetResponse
)
您不应该将响应流包装在BufferedStream
中,而应该直接使用它
您可以使用
块组合
您不需要自己执行GZip解压缩,HttpWebResponse
会自动执行以下操作:
我认为从后端方法控制ASP.NET HTTP响应不是一个好主意,因为它使CallWebService
的使用者处于未知状态,相反,使用者应该检查CallWebService
是否返回null,如果是,则呈现错误消息
请注意,在这两个catch
块中,您可能希望记录异常,因为它们的详细信息正在被忽略。它们可能表示服务中断或其他问题。问题不在于您的使用语句,而在于您的尝试语句:异常可能会在尝试块中的任何位置中止,然后代码将进入捕获(TimeoutException ex)
处理程序,但是这里没有return
语句
我看到您的代码似乎是web应用程序的一部分,但是这看起来像是一个“后端”方法,将内容返回给消费方法。您不应该从此处写入Response
,而是返回null
或不捕获异常,而是让CallWebService
的调用方负责显示错误消息
我发现您还犯了一些其他错误,例如使用动态
而不是字符串
,这就是我编写代码的方式:
public static string CallWebService(string an, string xmlcommand)
{
String url = @"http://212.170.239.18/appservices/ws/FrontendService";
try
{
String soapResult = null;
XmlDocument soapEnvelopeXml = CreateSoapEnvelope(xmlcommand);
HttpWebRequest webRequest = CreateWebRequest(_url, an);
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
using(HttpWebResponse response = webRequest.GetResponse())
using (Stream s = webResponse.GetResponseStream())
using (StreamReader rd = new StreamReader(gz))
{
if (an == "HotelValuedAvailRQ")
{
soapResult = rd.ReadLine();
}
else
{
soapResult = rd.ReadToEnd();
}
return soapResult;
}
}
catch (WebException)
{
return null;
}
catch (TimeoutException)
{
return null;
}
}
- 您错误地使用了
dynamic
<代码>动态
与VB的Dim
语句不等价
您使用的是HttpWebRequest
的异步方法,但使用的是阻塞,因此您最好只使用阻塞方法(GetResponse
)
您不应该将响应流包装在BufferedStream
中,而应该直接使用它
您可以使用
块组合
您不需要自己执行GZip解压缩,HttpWebResponse