0

我创建了一个 WCF 服务。我正在尝试从另一个域调用它。我启用了跨域选项。但我怀疑如何创建服务主机?

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

当我将标记更改为 this.it 显示 servicehost 丢失。我该怎么办?

服务.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;

    }


}
}
4

1 回答 1

0

A.svc 应该包含以下内容:

<%@ServiceHost ... %>

错误说:

缺少必需的指令“ServiceHost”。

你的.svc开头是:

!-<%@ ServiceHost ... %>

找出差异。

于 2013-05-20T12:21:59.933 回答