简体   繁体   English

可以向1个Web服务发送相同的参数但名称不同的Web服务吗?

[英]Can you send 1 web service the same parameter but different names?

We're creating a web service to receive a value (string). 我们正在创建一个Web服务来接收值(字符串)。 Based on the string, we determine if it's "type 1" or "type 2", and what needs to be done in terms of processing. 根据字符串,我们确定它是“类型1”还是“类型2”,以及在处理方面需要做什么。 So my web service is set up to receive data using: http://www.mysite.com/service.asmx?op=ProcessData?DataID=string 因此,我的Web服务设置为使用以下数据接收数据: http : //www.mysite.com/service.asmx?op=ProcessData?DataID=string

The client sending the string wants to send it using 2 different requests: http://www.mysite.com/service.asmx?op=ProcessData?DataIDType1=string http://www.mysite.com/service.asmx?op=ProcessData?DataIDType2=string 发送字符串的客户端希望使用2个不同的请求发送该字符串: http : //www.mysite.com/service.asmx ? op = ProcessData? DataIDType1 = string http://www.mysite.com/service.asmx?op = ProcessData?DataIDType2 =字符串

Is this possible for me to know though which type he's sending? 我是否可以知道他发送的是哪种类型? I can't set up different signatures for this right? 我不能为此设置不同的签名吗? Because they're both the same parameter? 因为它们都是相同的参数?

It's akin to writing: 这类似于写作:

public void DoSomething(String inputA)
{
    ...
}

public void DoSomething(String inputB)
{
    ...
}

It won't work (and for good reason!) - the method signature is the same. 它不起作用(并且有充分的理由!)-方法签名是相同的。

Imagine the call: 想象一下电话:

MyClass.DoSomething("TEST");

Which would it call? 会叫什么? Which should it call? 应该叫哪个?

Your options (as I see them): 您的选择(如我所见):

public void DoThingA(String input)
{
    ...
}

public void DoThingB(String input)
{
    ...
}

Which will give you different method signatures AND represent the fact that you're doing two distinct operation s(which is subsequently cleaner, IMO). 这将为您提供不同的方法签名,并表示您正在执行两个不同的操作s(此操作后来更干净,IMO)。

If you insist on a single method signature, you could do: 如果您坚持使用单个方法签名,则可以执行以下操作:

public void DoSomething(String input, object operationType) //where object is whatever type you see fit...
{
    if(operationType == ...)
    {
        DoThingA(input);
    }
    else
    {
        DoThingB(input);
    } 
}

Or... 要么...

public void DoSomething(String input)
{
    switch(input)
    {
        case "A":
            ...
            break;
        case "B":
            ...
            break;
        default:
            ...
            break;
    }
}

The most suitable depends on your available options. 最合适的取决于您的可用选项。 But I'd create two methods. 但是我会创建两个方法。

A method should have a single responsibility, therefore I would recommend two methods. 一种方法应负有单一责任,因此我建议两种方法。

public void FirstMethod(string param)
{
// Do something.
}

public void SecondMethod(string param)
{
// Do something.
}

This represents good design and the headache of maintainability later down the line when the client wants to add more functionality will be undoubtedly easier for you! 这代表了良好的设计,当客户想要添加更多功能时,后期的可维护性无疑将使您更加轻松!

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

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