Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于android的IIS上的wcf_Android_Json_Wcf_Iis - Fatal编程技术网

用于android的IIS上的wcf

用于android的IIS上的wcf,android,json,wcf,iis,Android,Json,Wcf,Iis,我试图实现一个托管在IIS中的wcf服务,作为使用在线数据库的adroid应用程序中的链接。现在,我只是按照教程来学习这些技术。我一直致力于让wcf服务在IIS中正常工作 我按照以下示例创建wcf: 在我的本地PC上,它工作正常,使用控制台应用程序托管wcf服务。在我的浏览器中请求下面的url时,它会按预期以json格式向我显示数据。这也是我下一步需要的 http://localhost:8090/MyFirstRESTfulService/EmployeeService/Employee

我试图实现一个托管在IIS中的wcf服务,作为使用在线数据库的adroid应用程序中的链接。现在,我只是按照教程来学习这些技术。我一直致力于让wcf服务在IIS中正常工作

我按照以下示例创建wcf:

在我的本地PC上,它工作正常,使用控制台应用程序托管wcf服务。在我的浏览器中请求下面的url时,它会按预期以json格式向我显示数据。这也是我下一步需要的

http://localhost:8090/MyFirstRESTfulService/EmployeeService/Employee 
在IIS中托管

现在我想在IIS中托管此wcf。我遵循这条线索:(我见过其他几个人,也用他们做过实验,但归根结底都是一样的)

该服务的物理地址为C:\inetpub\wwwroot\MyFirstRESTfulService。 wcf的实际代码已融化在一个文件(service.cs)中,并保存在子文件夹App_code中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;

namespace MyFirstRESTfulService
{
[DataContract]
public class Employee
{
    [DataMember]
    public int EmpId { get; set; }
    [DataMember]
    public string Fname { get; set; }
    [DataMember]
    public string Lname { get; set; }
    [DataMember ]
    public DateTime JoinDate { get; set; }
    [DataMember]
    public int Age { get; set; }
    [DataMember]
    public int Salary { get; set; }
    [DataMember]
    public string Designation { get; set; }
}


public partial class EmployeeData
{
    private static readonly EmployeeData _instance = new EmployeeData();

    private EmployeeData() { }

    public static EmployeeData Instance
    {
        get
        {
            return _instance;
        }
    }


    private List<Employee> empList = new List<Employee>()
    {
        new Employee() { EmpId  = 1, Fname = "Sam", Lname = "kumar", JoinDate=new DateTime(2010,7, 21), Age=30,Salary=10000,Designation="Software Engineer"},
        new Employee() { EmpId = 2, Fname = "Ram", Lname = "kumar", JoinDate=new DateTime(2009,6,8), Age=35,Salary=10000,Designation="Senior Software Engineer"},    
        new Employee() { EmpId = 3, Fname = "Sasi", Lname = "M", JoinDate=new DateTime(2008,3,5), Age=39,Salary=10000,Designation="Projet Manager"},  
        new Employee() { EmpId = 4, Fname = "Praveen", Lname = "KR", JoinDate=new DateTime(2010, 5,1), Age=56,Salary=10000,Designation="Projet Manager"},
        new Employee() { EmpId = 5, Fname = "Sathish", Lname = "V", JoinDate = new DateTime(2006,12,15), Age=72,Salary=10000,Designation="Senior Software Engineer"},  
        new Employee() { EmpId = 6, Fname = "Rosh", Lname = "A", JoinDate=new DateTime(2009,2,2), Age=25,Salary=10000,Designation="Software Engineer"}
    };

    public List<Employee> EmployeeList
    {
        get
        {
            return empList;
        }
    }


    public void Update(Employee updEmployee)
    {
        Employee existing = empList.Find(p => p.EmpId == updEmployee.EmpId);

        if (existing == null)
            throw new KeyNotFoundException("Specified Employee cannot be found");

        existing.Fname = updEmployee.Fname;
        existing.Lname = updEmployee.Lname;
        existing.Age = updEmployee.Age;
    }

    public void Delete(int empid)
    {
        Employee existing = empList.Find(p => p.EmpId == empid);
        empList.Remove(existing);
    }
    public void Add(Employee newEmployee)
    {
        empList.Add(new Employee
        {
            EmpId = newEmployee.EmpId,
            Fname = newEmployee.Fname,
            Lname = newEmployee.Lname,
            Age = newEmployee.Age,
            JoinDate = DateTime.Now,
            Designation = newEmployee.Designation,
            Salary = newEmployee.Salary
        });
    }
}












[ServiceContract()]
public interface IEmployeeService
{
    [WebGet(UriTemplate = "Employee", ResponseFormat=WebMessageFormat.Json )]
    [OperationContract]
    List<Employee> GetAllEmployeeDetails();

    [WebGet(UriTemplate = "GetEmployee?id={id}", ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    Employee GetEmployee(int Id);

    [WebInvoke(Method = "POST", UriTemplate = "EmployeePOST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    [OperationContract]
    void AddEmployee(Employee newEmp);

    [WebInvoke(Method = "PUT", UriTemplate = "EmployeePUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    [OperationContract]
    void UpdateEmployee(Employee newEmp);

    [WebInvoke(Method = "DELETE", UriTemplate = "Employee/{empId}", ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    void DeleteEmployee(string empId);
}












[AspNetCompatibilityRequirements(RequirementsMode= AspNetCompatibilityRequirementsMode.Allowed )]
public  class EmployeeService: IEmployeeService 
{

   public List <Employee> GetAllEmployeeDetails()
    {
        return EmployeeData.Instance.EmployeeList;
    }

    public Employee GetEmployee(int id)
    {
        IEnumerable<Employee> empList = EmployeeData.Instance.EmployeeList.Where(x => x.EmpId == id);

        if (empList != null)
            return empList.First<Employee>();
        else
            return null;
    }


   public void AddEmployee(Employee newEmp)
   {
       EmployeeData.Instance.Add(newEmp);
   }


    public void UpdateEmployee( Employee newEmp)
    {
        EmployeeData.Instance.Update(newEmp);
    }


     public void DeleteEmployee(string empId)
    {
        EmployeeData.Instance.Delete(System.Convert .ToInt32 (empId));
    }
}

}
在浏览器中,我会看到一个页面,上面写着“您已经创建了一个服务。要测试这个服务,您需要创建一个客户端并使用它调用该服务。”等等

就像这个链接一样

http://localhost:8090/MyFirstRESTfulService/EmployeeService/Employee 
在使用控制台应用程序时返回json格式的数据,我现在希望在使用

http://localhost/MyFirstRESTfulService/EmployeeService/Employee 
或许

http://localhost/MyFirstRESTfulService/service.svc/EmployeeService/Employee 
但我不能让它工作。我在这些链接中发现了“找不到页面”之类的错误

我做错了什么


很抱歉发了这么长的帖子,非常感谢

您需要向端点添加endpointbehavior以启用RESTfulity。我还认为您需要使用webHttpBinding而不是basicHttpBinding

<endpoint address="" binding="webHttpBinding" contract="MyFirstRESTfulService.IEmployeeService" behaviorConfiguration="WebHttp">

<endpointBehaviors>
    <behavior name="WebHttp">
        <webHttp/>
    </behavior>
</endpointBehaviors>


当你自己主持WCF的时候,我猜你用的是WebServiceHost?这为您提供了restful行为的正确配置。

您说得对,谢谢。与此同时,我发现了另一个很好的链接:确认你的答案。
http://localhost/MyFirstRESTfulService/service.svc/EmployeeService/Employee 
<endpoint address="" binding="webHttpBinding" contract="MyFirstRESTfulService.IEmployeeService" behaviorConfiguration="WebHttp">
<endpointBehaviors>
    <behavior name="WebHttp">
        <webHttp/>
    </behavior>
</endpointBehaviors>