asmx WebService 坚持在具有可为空参数的其他类中调用的方法
我试图通过采用几个可为 null 的方法/属性来简化 winform 和 webapp (由其他人编写)之间共享的库中的一些代码,但对于这些方法/属性,永远不会传入可为 null 的值/null 永远不会被消耗为非可为空的。
这在 winform 应用程序中工作得很好,并且 web 应用程序没有任何编译时问题,但是当我第一次运行它时,它未能报告错误,它找不到采用 DateTime? 的方法的旧版本?
参数,尽管事实上传递的值是不可为 null 的 DateTime
。当我用 DateTime?
参数添加回重载时,由于属性已从 int?
更改为 ,它只会在代码中的其他位置生成错误int
。
在确认使用 DateTime?
参数的重载可以作为修复方法后,我对重载进行了评论,以尝试找出为什么它坚持调用该版本。在我摆弄它的某个时候(进行强制重建等),它突然停止生成错误,即使我将两个 .cs 文件恢复到它们最初失败的版本。此时它正在工作,但我不知道最初出了什么问题,也不知道它是如何修复的,并且我非常怀疑继续下去,直到我知道发生了什么。
大概的错误消息(凭记忆): 找不到方法:'null NameSpace.StaticClass.StaticMethod( IFoo foo, IBar[] bar, DateTime? padStart, DateTime? padEnd )'.
实际错误消息: 找不到方法:'System.Nullable
1 NameSpace.OtherClass.get_Number()'.`
MyWebService.asmx 中的方法:
[WebMethod( EnableSession = true )]
public string DoStuff( string jsonFoo, string startDate, string endDate )
{
try
{
IFoo foo = Deserialize<IFoo>( jsonFoo );
IBar[] bar = null;
DateTime padStart = DateTime.Parse(startDate);
DateTime padEnd = DateTime.Parse(endDate);
StaticClass.StaticMethod( foo, bar, padStart, padEnd );
}
}
StaticClass 中的方法:
public static void StaticMethod( IFoo foo, IBar[] bar, DateTime padStart, DateTime padEnd )
{
}
I was trying to simplify some code in a library shared between a winform and a webapp (written by someone else) by taking several methods/properties that were nullable, but for which nullable values were never passed in/nulls were never consumed into non-nullable ones.
This worked fine in the winform app, and the webapp doesn't have any compile time problems but when I first ran it, it failed reporting an error that it couldn't find the old version of the method that took DateTime?
parameters, despite the fact that the values being passed were non-nullable DateTime
s. When I added back an overload with DateTime?
parameters it worked only to generate an error elsewhere in the code due to a property that had been changed from int?
to int
.
After confirming that an overload with DateTime?
parameters would work as a fix, I commented the overload back out to try and figure out why it was insisting on calling that version. At some point while I was fiddling with it (doing forced rebuilds, etc), it suddenly stopped generating the errors even though I'd reverted the two .cs files to the version where they were initially failing. At this point it's working, but I have no idea what went wrong initially or how it was fixed, and I'm very leery of continuing on until I know what happened.
Approximate error message (from memory):Method not found: 'null
NameSpace.StaticClass.StaticMethod( IFoo foo, IBar[] bar, DateTime? padStart, DateTime? padEnd )'.
Actual error message:Method not found: 'System.Nullable
1 NameSpace.OtherClass.get_Number()'.`
Method in MyWebService.asmx:
[WebMethod( EnableSession = true )]
public string DoStuff( string jsonFoo, string startDate, string endDate )
{
try
{
IFoo foo = Deserialize<IFoo>( jsonFoo );
IBar[] bar = null;
DateTime padStart = DateTime.Parse(startDate);
DateTime padEnd = DateTime.Parse(endDate);
StaticClass.StaticMethod( foo, bar, padStart, padEnd );
}
}
Method in StaticClass:
public static void StaticMethod( IFoo foo, IBar[] bar, DateTime padStart, DateTime padEnd )
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了首先解决明显的问题 - 您是否重建了针对该服务构建的任何客户端代理/存根?客户端代码可能仍然会生成对 v1 服务定义的服务请求,而不是对闪亮的新 v2 服务定义。只是一个想法?
In the interest of clearing out the obvious problems first - did you rebuild any client side proxies/stubs that were built against the service? The client code could still have been generating service requests to the v1 service definition rather than the shiny new v2 one. Just a thought?
听起来这只是 .NET 运行时如何处理网站项目中的动态编译的问题。在 Web 应用程序中,您必须在每次进行更改时重新构建它,并且整个应用程序会像其他项目一样编译到 .dll 中。在具有动态编译的网站中,运行时基本上决定了页面如何分成 .dll 以及何时需要重建它们。由于您的情况涉及静态方法,听起来像是一个单独的项目/.dll,但您站点中的页面从未更改,因此运行时可能从未重新链接到更新的 .dll。然后,如果在某个时刻您对页面进行了更改,导致页面重新编译,它会突然识别静态类中的新方法。当然,目前这只是猜测;)
不过,如果您再次遇到这样的问题,我建议您在解决方案资源管理器中右键单击网站并执行“重建”
It sounds like it's just an issue with how the .NET runtime handles dynamic compilation in a Website project. In a Web Application you have to rebuild it anytime you make changes, and the whole app gets compiled into a .dll like other projects. In a Website with dynamic compilation, the runtime basically determines how your pages get separated into .dll's and when they need to be rebuilt. Since your situation involved static methods in what sounds like is a separate project/.dll, but the page in your site never changed, the runtime probably just never re-linked against the updated .dll. Then if at some point you made a change to the page that would have caused it to be recompiled, it would suddenly recognize the new methods in your static class. Of course at this point, it's just guessing ;)
If you have a problem like this again, though, I'd recommend right-clicking the Website in Solution Explorer and doing a "Rebuild"