Wcf服务编写PUT / DELETE方法会使服务崩溃

时间:2017-01-16 23:42:48

标签: jquery asp.net ajax wcf

我正在创建一个与数据库交互的Web服务,它应该执行CRUD操作。现在它适用于读取和后期操作,WCF服务应该有一个方法,只要页面完成加载以检索所有提供程序的列表,就可以从jQuery中调用它,但是,当我更新WCF服务时它工作正常用这样的方法

[OperationContract]
[WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Object PutSupplier(int id, Supplier oParameter)
{
    // Do work
}

OR

[OperationContract]
[WebInvoke(Method = "DELETE", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Object DeleteSupplier(int id)
{
    // Do work
}

当页面加载并且插入操作也失败并且我得到500内部错误时,有些事情发生并且没有数据回来?!就像整个WCF服务看不见或不起作用一样。

这是我的其他WCF服务方法和调用jQuery / Ajax

[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public List<Supplier> GetSupplier(int id)
{
    // Add your operation implementation here
    return DbContext.DbContextManager.GetSupplier(id.ToNullableInt());
}
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Supplier PostSupplier(Supplier oParameter)
{
    return DbContext.DbContextManager.PostSupplier(oParameter);
}

<script>
$(function () {
    $.ajax({
        method: 'GET',
        url: '/WebServices/NameService.svc/GetSupplier',
        data: JSON.stringify({ id : 0 }),
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (item) {
            $.each(item, function (i) {
                var _tr = '<tr><td class="readonly-"><input type="text" class="form-control" value="' + item[i].CompanyName + '" style="display:table-cell; width:100%" readonly/></td><td>' +
                    '<button type="button" id="pencilbutton_' + item[i].SupplierId + '"  class="btn btn-success">' +
                    '<span class="glyphicon glyphicon-pencil"></span> Pencil</span></button>' +
                    '<button type="button" id="removebutton_' + item[i].SupplierId + '"  class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Remove</button></td></tr>';
                $('tbody').append(_tr);
            });
        }
    });
    $('table').on('focus', 'input[type="text"]', function () {
        $(this).removeAttr('readonly');
    });
    $(':button[class*="btn-primary"]').click(function () {
        if (Page_ClientValidate("MyValidationGroup")) {
            $.ajax({
                method: 'POST',
                url: '/WebServices/NameService.svc/PostSupplier',
                data: JSON.stringify({ 'SupplierId': 0, 'CompanyName': $(':text[class="form-control"]').val() }),
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                success: function (item) {
                    var _tr = '<tr><td class="readonly-"><input type="text" class="form-control" value="' + item.CompanyName + '" style="display:table-cell; width:100%" readonly/></td><td>' +
                             '<button type="button" id="pencilbutton_' + item.SupplierId + '"  class="btn btn-success">' +
                             '<span class="glyphicon glyphicon-pencil"></span> Pencil</span></button>' +
                             '<button type="button" id="removebutton_' + item.SupplierId + '"  class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Remove</button></td></tr>';
                    $('tbody').append(_tr);
                }
            });
        }
    });
});

0 个答案:

没有答案