简体   繁体   中英

I have set up a web service but have no idea how to get data from it in MVC 5 c#

I have the following namespace for the web service and I can see lots of functions I can access...

using MyService.api;

public class HomeController : Controller
{
    public ActionResult Index()
    {
        //What needs to go here?

        // LoginRequest and Response are types from the web service reference

        LoginRequest loginRequest = new LoginRequest();
        loginRequest.user = "blah";
        loginRequest.password = "abc1234";

        LoginResponse loginResponse = new LoginResponse();
        string sessionID = loginResponseSession.session.sessionId; // This has nothing in it

        return View("Index", sessionID );
    }
    ...

the api exposes all of these methods, things like:

MachineRequest machineRequest = new MachineRequest();
string[] machines = machineRequest.machines;

But ofcourse when I call that, it does not magically go and collect machines from the url.

How do I make and respond to the requests? What is the Service reference actually for?

Your question is a bit unclear. If you've generated a WCF service reference, you can use the "proxy" or "client" like this:

var client = new MyService.api.ApiReferenceClient();
var loginResponse = client.LoginRequest(loginRequest);

That will perform the SOAP call (or whatever binding is being used).

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