简体   繁体   English

从同一Web服务调用Web方法

[英]Calling web methods from the same web service

I need to write a wrapper for a collection of web methods that are exposed in a particular web service. 我需要为特定Web服务中公开的Web方法集合编写包装器。 It makes sense to stick this new wrapper method in the same service since it's affecting the same type of object. 将这个新的包装器方法粘贴在同一个服务中是有意义的,因为它会影响相同类型的对象。

Most of these web methods are just thin methods which call static methods in other files, which is fine, but they also have some security logic done before these static method calls. 大多数这些Web方法只是在其他文件中调用静态方法的瘦方法,这很好,但是在这些静态方法调用之前它们也有一些安全逻辑。 Rather than recreate the security logic before each method call I want to wrap, is it possible to just call these other web methods from inside the same service locally, or is this bad practice? 而不是在我想要包装的每个方法调用之前重新创建安全逻辑,是否可以从本地内部的同一服务中调用这些其他Web方法,或者这是不好的做法?

Here's an example: 这是一个例子:

[WebMethod]
public int SmallMethod1(int a)
{
    //SecurityLogic
    return AnObject.StaticMethod1();
}

[WebMethod]
public int SmallMethod2(int b)
{
    //SecurityLogic
    return AnObject.StaticMethod2();
}

[WebMethod]
public int WrapperMethod(int c)
{
    return AnObject.StaticMethod1() + AnObject.StaticMethod2();
}

In general you will want to separate the public interface of your web service from the actual implementation as cleanly as possible, in your example you have done this by encapsulating them in AnObject which allows unit testing the encapsulated methods separately (which is a big problem otherwise especially with web methods). 一般情况下,您希望尽可能干净地将Web服务的公共接口与实际实现分开,在您的示例中,通过将它们封装在AnObject来完成此操作, AnObject允许单独测试封装的方法(否则这是一个大问题)特别是使用网络方法)。

Having said that from a testing perspective I would suggest rather doing this: 从测试的角度说,我建议这样做:

[WebMethod]
public int WrapperMethod(int c)
{
    return AnObject.WrapperMethod(c)
}

This would allow you to write tests that test WrapperMethod directly (encapsulated in AnObject ), rather than trying to recreate testing for AnObject.StaticMethod1() + AnObject.StaticMethod2() in your unit tests - this gets messy quickly because now you have the same logic in two different spots. 这将允许您编写直接测试WrapperMethod测试(封装在AnObject ),而不是尝试在单元测试中重新创建AnObject.StaticMethod1() + AnObject.StaticMethod2()的测试 - 这会很快变得混乱,因为现在你有相同的两个不同点的逻辑。

Not at all. 一点也不。 Its a very good idea to encapsulate that logic. 封装该逻辑是一个非常好的主意。 You could have a AnObject.WrapperStaticMethod() . 你可以有一个AnObject.WrapperStaticMethod()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM