0

我是 WCF 的新手,我正在构建一项服务来执行 CRUD 操作。我创建了一个带有两个参数的新 void 方法。我设置了一个断点并在调试模式下粘贴了这个 URL:

http://localhost:55152/WcfDataService.svc/AddNewNote()?ParamNoteTitle='dfdfdf'&ParamNoteText='dfdfdfdf'

这是我的代码:

 [WebGet]
    public void AddNewNote(string ParamNoteTitle, string ParamNoteText)
    {
        //My hardcoded values for now...
        int ParentID = 8879;
        int JobID = 1000088150;
        int ContactID = 309;
        Guid UserID = Guid.NewGuid();
        string RelatedType = "Advertiser Contact";
        bool IsShared = true;

        tblNote N = new tblNote
        {
          NotesTitle = ParamNoteTitle,
          NotesText = ParamNoteText,
          ParentID = ParentID,
          ContactID = ContactID,
          JobID = JobID,
          UserID = UserID,
          GroupID = null,
          RelatedType = RelatedType,
          IsShared = IsShared
        };
        this.CurrentDataSource.tblNotes.Add(N);
        this.CurrentDataSource.SaveChanges();

    }

我收到 404 错误。我的查询字符串/URL 有问题吗?

4

2 回答 2

0

我最终将我的方法类型更改为 IQueryable 并在插入后调用一个方法来检索带有我的 ID 的新行。我最初想返回一个整数或布尔值,所以在我的 Javascript 中,我可以通过查看返回值来处理成功或失败。

 [WebGet]
    public IQueryable<vw_Note> AddNewNote(string ParamNoteTitle, string ParamNoteText)
    {
        //My hardcoded values for now...
        int ParentID = 8879;
        int JobID = 1000088150;
        int ContactID = 309;
        Guid UserID = new Guid("8b0e303a-68aa-49a5-af95-d994e2bdd5ac");
        Guid NoteID = Guid.NewGuid();
        string RelatedType = "Advertiser Contact";
        bool IsShared = true;

        tblNote N = new tblNote
        {
            NotesID = NoteID,
            NotesTitle = ParamNoteTitle,
            NotesText = ParamNoteText,
            ParentID = ParentID,
            ContactID = ContactID,
            JobID = JobID,
            UserID = UserID,
            GroupID = null,
            RelatedType = RelatedType,
            IsShared = IsShared
        };
        try
        {
            this.CurrentDataSource.tblNotes.Add(N);
            this.CurrentDataSource.SaveChanges();
            return GetNoteByID(NoteID);

        }
        catch (Exception ex)
        {
            return GetNoteByID(NoteID);
        }

    }

如您所见,它将返回一组数据。我被困在如何使用 WCF 处理插入数据然后响应客户端请求,但我设法解决了它。不管怎么说,还是要谢谢你!

于 2013-05-15T11:31:41.913 回答
0

首先,通过将此 URL 粘贴到浏览器中来检查服务本身是否正常运行:

http://localhost:55152/WcfDataService.svc

如果是这样,我建议你看看这个类似的 SO question

希望有帮助!

于 2013-05-15T08:52:03.603 回答