简体   繁体   中英

How to access a WCF Service within my webpage in vs2010?

I have created a simple Web Service application and used SvcUtil.exe to generate the two needed files named MyService.cs and app.config .

What I want to do now is be able to use this webservice from a web-app or webpage. What I was doing until now was creating my website and creating an angular factory that would use ajax to call the various functions on a class that I used to create for the purpose of dealing with those requests only. For example I would have the following function inside a file name fhandler.aspx.cs :

namespace MyApp
{
    [ScriptService]
    public partial class fHandler: System.Web.UI.Page
    {            
        // Save Interests Stress Test in List.
        [WebMethod]
        public static string saveDataJS(string data)
        {
...

This would get called by an ajax request that would return a promise like this:

saveData: function(data) {
    var senddata = {
        'data': data
    };
    return $http({
        method: 'POST',
        url: 'http://' + location.host + '/fHandler.aspx/saveDataJS',
        data: senddata,
        headers: {
            "Content-Type": "application/download; charset=utf-8"
        }
    });
},

Now that I am using web-services though I am not sure how to approach this. What would be the best set-up for consuming a WCF service from a webpage?

Assuming we skip the create another project inside your solution, add MyService.cs and app.config to it and add a Reference to your service, part.

This tutorial once helped me a great deal with creating and consuming a WCF service.

To consume a WCF service, you simply have to add a service reference to your project. Have a look at the tutorial, it is all described with pictures.

Solution Explorer > {your project} > Add Service Reference .

AddServiceReference

After that, you can enter the URL of your service. Afterwards, you should be able to see all public methods your service offers:

服务

Add a service reference into your project, in solution explorer, just right click service references and add service reference, paste your web service url. And then create a client instance for service reference to use its methods in your codebehind.

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