在阅读老项目的时候,发现有人在webservice上用了out类型的入参
[WebMethod(Description = "数据交互统一接口")]
public string Request(string MethodName, string inParaXml, out string outParaXml)
{
string ret = string.Empty;
string outPara = string.Empty;
BsRequest bsReq = new BsRequest();
ret = bsReq.Request(MethodName, inParaXml, ref outPara);
outParaXml = outPara;
return ret;
}
于是当我用postman正常方法调用的时候始终不行,按照项目原来对接方式是通过soap的方式调用的。
于是在在postman中用soap的方式:content-type = text/xml。调用是可以成功返回值的,此时outParaXml自动匹配到了如图所示位置
注意soap入参中没有包含outParaXml。
既然soap方式可以调用,我在想新版本的controller中如果加入一个out入参会发生什么。
首先是.net6中
[HttpPost("/helloworld")]
public string helloworld(out string b)
{
b = "momomo";
return "helloworld";
}
我们随意返回一个momomo,发现最后postman没有任何返回,代码中打了断点也没有进入该方法。
查阅资料https://github.com/dotnet/aspnetcore/issues/43256
中有回答
改成.net 7 测试
会报错提示不能包含ref类型
官方改进文章https://github.com/dotnet/aspnetcore/pull/43842
当具有非常模糊消息的类型时,检测 Try/Parse 方法的机制将失败
似乎还是没解决这个问题,不知道大伙有啥看法。
总之我们尽量不要用out,ref这种类型的入参!