简体   繁体   中英

Call WCF From CodeBehind Asp.net

I am making wcf calls and getting back data in my application via this javascript code..

function setCurrentResponseValue(response) {

    var applicationData = null;

    $.ajax({
        type: "POST",
        url: "ClientService.svc/REST/SetCurrentResponseValue",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ response: response }),
        dataType: "json",
        async: false,
        success: function (msg) {

            applicationData = msg;

        },
        error: 'An Error'
    });

    return applicationData;

}

I need to set session variables my svc file uses from C# code behind. How do I call a method in my svc file from code behind instead the ajax call above?

There are lots of very similar questions already on SO. This has perhaps the best answers: How to Consume a Restful Service in .NET?

@Darrel Miller's answer is probably the most useful there, with a link to his blog post showing how to use HttpClient to consume REST data: http://www.bizcoder.com/index.php/2012/01/09/httpclient-it-lives-and-it-is-glorious/

(The fact that the service is WCF is largely irrelevant to the client if you're using loose coupling against REST).

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