简体   繁体   中英

How to test tcp client to a webmethod that receives multiple parameters in c#

Hello everyone who can help me. I'm trying to do webmethods that makes call to a server. I want to ensure that data don't get mix with two different clients making the same call to the web method. If anyone got one example or anything that can help me will be great. If im not clear with the question please feel free to ask me.

Thanks!

Assuming you have to call from the client to server:
You can insert unique ids to your calls using Math.random() based generators or libraries .

Or when you load the page, you can throw in the SessionId from ASP.NET (or retrieve it from cookies), and add that to your request.

So it would look like this:
Server:

[WebMethod]
public static string Hello(string name, string sessionId)
{
    return "Hello " + name;
}

Client:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js" type="text/javascript"></script>
<script type ="text/javascript">
    function guid() {
      function s4() {
          return Math.floor((1 + Math.random()) * 0x10000)
           .toString(16)
           .substring(1);
      }
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
          s4() + '-' + s4() + s4() + s4();
    }
    $(document).ready(function () {
        $('#somebutton').click(function () {
            var id = 
            $.ajax({
                type: "POST",
                url: "SomeController/Hello",
                data: "{'name':'" + $('#nametxtbox').val() + "', 'sessionId':'" + guid() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    alert(msg.d);
                }
            })
            return false;
        });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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