创建服务主机?

时间:2013-05-20 10:26:59

标签: asp.net-mvc-3 wcf

我已经创建了一个WCF服务。我正试图从另一个域调用它。我启用了跨域选项。但是我对如何创建服务主机感到疑惑?

!-<%@ ServiceHost Language="C#" Debug="true" Service="jsonwcf.Service1"
      CodeBehind="Service1.svc.cs"
      Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory%>

当我将标记更改为this.it显示servicehost missing.I我该怎么办?

service.cs

namespace jsonwcf
{

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
    [WebInvoke(
   Method = "GET",
   ResponseFormat = WebMessageFormat.Json,
   BodyStyle = WebMessageBodyStyle.WrappedRequest
 )]
    public List<UserDetails> SelectUserDetails()
    {
        pasDataContext db = new pasDataContext();
        List<UserDetails> results = new List<UserDetails>();

        foreach (User u in db.Users)
        {
            results.Add(new UserDetails()
            {
                UserID = u.UserID,

                EmpName = u.EmpName,
                Email = u.EmailID,
                UserName = u.UserName,
                UserRole = u.UserRole,
                Password = u.Password,
                Telephone = u.Telephone

            });
        }
        return results;
    }


    [WebInvoke(
  Method = "POST",
  ResponseFormat = WebMessageFormat.Json,
  BodyStyle = WebMessageBodyStyle.WrappedRequest
)]
    public string ins(string uid, string pwd, string uname, string ename, string tel, string urole, string eid)
    {

        pasDataContext db = new pasDataContext();
        User u = new User();
        u.UserID = uid;
        u.UserName = uname;
        u.UserRole = urole;
        u.Telephone = tel;
        u.Password = pwd;
        u.EmailID = eid;
        u.EmpName = ename;
        db.Users.InsertOnSubmit(u);
        db.SubmitChanges();
        return "inserted successfully";
    }

}
}

iservice.cs

namespace jsonwcf
{

 [ServiceContract(Namespace = "JsonpAjaxService")]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]

   List<UserDetails> SelectUserDetails();

    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,UriTemplate = "Service/ins")]
    string ins(string uid, string pwd, string uname, string ename, string tel, string urole,string eid);
}
[DataContract]
public class UserDetails
{
      [DataMember]

    public string UserID
    {

        get;

        set;

    }

    [DataMember]

    public string Password
    {

        get;

        set;

    }

    [DataMember]

    public string UserName
    {

        get;

        set;

    }

    [DataMember]

    public string Email
    {

        get;

        set;

    }
       [DataMember]
    public string EmpName
    {

        get;

        set;

    }
       [DataMember]
    public string UserRole
    {

        get;

        set;

    }
       [DataMember]
    public string Telephone
    {

        get;

        set;

    }


}
}

1 个答案:

答案 0 :(得分:0)

.svc should包含以下内容:

<%@ServiceHost ... %>

错误说:

  

缺少必需的指令'ServiceHost'。

您的.svc以:

开头
!-<%@ ServiceHost ... %>

发现差异。