Wcf服务编写PUT / DELETE方法会使服务崩溃 [英] Wcf service writing PUT/DELETE methods crashes the service

查看:69
本文介绍了Wcf服务编写PUT / DELETE方法会使服务崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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









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





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



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



 [OperationContract] 
[WebInvoke(Method =GET,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
public List< Supplier> ; GetSupplier(int id)
{
//在这里添加你的操作实现
return DbContext.DbContextManager.GetSupplier(id.ToNullableInt());
}
[OperationContract]
[WebInvoke(Method =POST,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
public Supplier PostSupplier(Supplier oParameter)
{
返回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 =textclass = form-controlvalue ='+ item [i] .CompanyName +'style =display:table-cell; width:100%readonly />< / td>< td>'+
'< button type =buttonid =pencilbutton_'+ item [i] .SupplierId +'class =btn btn-success>'+
'< span class =glyphicon glyphicon-pencil>< / span>铅笔< / span>< / button>'+
'< button type =buttonid =removebutton_'+ item [i] .SupplierId +'class =btn btn-danger>< span class =glyphicon glyphicon-remove>< / span>删除< /按钮>< / 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( {
方法:'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',
成功:function(item){
var _tr ='< tr>< td class =readonly - >< input type =textclass =form-controlvalue ='+ item.CompanyName +'style =display:table-cell; width:100%readonly />< / td>< td>'+
'< button type =按钮id =pencilbutton_'+ item.SupplierId +'class =btn btn-success>'+
'< span class =glyphicon glyphicon-pencil>< / span>铅笔< / span>< / button>'+
'< button type =buttonid =removebutton_'+ item.SupplierId +'class =btn btn-danger>< span class =glyphicon glyphicon-remove>< / span>删除< /按钮>< / TD>< / TR>;
$('tbody')。append(_tr);
}
});
}
});
});
< / script>





我的尝试:



我正在尝试使用POST而不是

解决方案

(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 =textclass =form-controlvalue ='+ item [i] .CompanyName +' style =display:table-cell; width:100%readonly />< / td>< td>'+
'< button type =buttonid =pencilbutton_'+ item [i] .SupplierId +'class =btn btn-success>'+
'< span c lass =glyphicon glyphicon-pencil>< / span>铅笔< / span>< / button>'+
'< button type =buttonid =removebutton_'+ item [i] .SupplierId +'class =btn btn-danger> < span class =glyphicon glyphicon-remove>< / span>删除< /按钮>< / TD>< / TR>;

I am creating a web service to interact with a database and it is supposed to perform the CRUD operations. Right now it works fine with the read and post operation the WCF service supposed to have a method that is called from within jQuery whenever the page completes load to retrieve a list of all providers and it works fine, however, when I update the WCF Service with a method like this

[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
    }



Something happen and no data comes back when the page loads and the insertion operation failed too and I got a 500 internal error?!. Like if the whole WCF Service got unseen or not functioning.

Here is my other WCF Service methods and the calling 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);
                    }
                });
            }
        });
    });
</script> 



What I have tried:

I am trying to use POST instead

解决方案

(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>';


这篇关于Wcf服务编写PUT / DELETE方法会使服务崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆