Visual Studio中的WCF Web服务

时间:2013-10-17 03:03:39

标签: android asp.net wcf web-services wcf-rest

我很新,需要一些帮助。 我在visual studio中创建了一个wcf服务应用程序,并编写了一个简单的登录方法,它接收一个参数userid&然后,密码返回已记录用户对象的对象,该对象具有两个字段名称和组织。

LoggedUser类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;

namespace crmpp_android_service
{
    public class LoggedUser
    {
        public string name;
        public string organization;
        [DataMember]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        [DataMember]
        public string Organization
        {
            get { return organization; }
            set { organization = value; }
        }
    }
}

OperationContract的:

[OperationContract]
        [WebInvoke(Method = "GET",
           ResponseFormat = WebMessageFormat.Json,
             RequestFormat = WebMessageFormat.Json,
            UriTemplate = "login/?userid={userid} & pass={pass}"
        )]
        [Description("User Login return username & Organization currently affiliated")]
        LoggedUser login(string userid, string pass);

Service1.svc中的方法实现:

  public LoggedUser login(string userid , string pass)
        {
            LoggedUser log_usr = new LoggedUser();
            log_usr.name = "Ahsan Mehdi";
            log_usr.organization = "JDC";
            return log_usr;
        }

但是当我通过按ctrl + F5运行应用程序时,它打开一个像这样的wcf测试客户端并返回数据。 enter image description here

当我为服务编写URL时,我无法从Android应用程序或只是从浏览器获得响应,它会向我显示这样的页面。

http://localhost:57127/Service1.svc

enter image description here

当完成这样的网址时

http://localhost:57127/Service1.svc/login/?userid=abc123&pass123

它会尽快返回任何指南或帮助。

2 个答案:

答案 0 :(得分:1)

根据我的理解,您希望访问托管在您的localhost计算机上而不是IIS上的此WCF网址。要访问localhost webservice,请使用this而不是localhost

http://10.0.2.2:Port/Service1.svc/login/?userid=abc123&pass123

在IIS上使用分配给您的系统IP。

http://192.168.0.123:Port/Service1.svc/login/?userid=abc123&pass123

我希望这会清除你关于连接到android项目的问题。

答案 1 :(得分:0)

获取WCFstorm。当您运行它时,添加一项新服务,并复制该服务的URL(在屏幕截图中,它显示为:

localhost:57127/Service.svc

这将允许您测试服务。

这是我在工作中用于测试WCF服务的内容。

我使用的另一个工具是Fiddler。