从winform / ASP.net jquery ajax调用WCF服务

时间:2018-04-27 18:53:50

标签: c# ajax asp.net-mvc wcf nettcpbinding

嘿所有我有以下代码通过jQuery AJAX调用将json字符串发送到后面的asp.net代码并将其放入其类名中。

JavaScript Ajax: enter image description here

ASP.net代码背后: enter image description here

WCF代码: enter image description here

AJAX的代码:

$("#WCF").on("click", function () {
    var scannerSettings = {
        "FileType": "TEST1",
        "FileName": "C:\Program Files (x86)\somethibg\Sample\ScanTest\VCS 2008\bin\Debug\image#####",//$('').text(),
        "JpegQuality": "TEST2",
        "ScanContinue": false,
        "Resolution": "TEST3",
        "Rotation": "TEST4",
        "PaperSupply": "TEST5",
        "PaperSize": "TEST6",
        "Orientation": "TEST7",
        "CustomPaperWidth": 8.268,
        "CustomPaperLength": 11.693,
        "JobControl": "TEST8",
        "PunchHoleRemoval": "TEST9"
    };

    $.ajax({
        type        : "POST",
        contentType : "application/json",
        dataType    : "json",
        url         : ajaxDirPath + "WCF",
        data        : JSON.stringify({ "obj": scannerSettings }),
        cache       : false,
        success     : ServiceSucceeded,
        error       : ServiceFailed
    });
});

代码背后的ASP.net代码(HomeController.cs):

[HttpPost]
[Audit(EventTypeName = "{controller}/{action} ({verb})", IncludeHeaders = true, IncludeModel = true)]
public string WCF(scannerSettings obj)
{
    Dictionary<string, string> resultsBack = new Dictionary<string, string>();
    EndpointAddress address = new EndpointAddress(new Uri(ConfigurationManager.AppSettings["EndpointAddress"].ToString()));
    NetTcpBinding binding = new NetTcpBinding();
    ChannelFactory<IService1> factory;
    IService1 SvcCall;

    binding.Security.Mode = SecurityMode.Message;
    binding.SendTimeout = new TimeSpan(0, 1, 0);
    factory = new ChannelFactory<IService1>(binding, address);
    SvcCall = factory.CreateChannel();

    string scanerData = SvcCall.Scan(obj);

    if (scanerData != null)
    {
        resultsBack.Add("dback", "GOOD");
        resultsBack.Add("img", "");
        resultsBack.Add("filename", obj.FileName);
    }

    return JsonConvert.SerializeObject(resultsBack, Formatting.Indented);
}

WCF代码:

public string Scan(scannerSettings obj)
{
    TestDelegate d = new TestDelegate(startScan);
    IAsyncResult ar = d.BeginInvoke(obj, null, null);

    string result = d.EndInvoke(ar);

    return result;

}

从上面的图片中可以看出,它从 AJAX-&gt; HomeController.cs 发送数据很好,但是一旦我再次将其发送到WCF服务器,如 AJAX-&gt; HomeController.cs-&gt; WCF 它不再具有所需的值 - 它们都是 null

json课程:

[DataContract]
public class scannerSettings
{
    [DataMember]
    public string FileType { get; set; }
    [DataMember]
    public string FileName { get; set; }
    [DataMember]
    public string JpegQuality { get; set; }
    [DataMember]
    public bool ScanContinue { get; set; }
    [DataMember]
    public string Resolution { get; set; }
    [DataMember]
    public string Rotation { get; set; }
    [DataMember]
    public string PaperSupply { get; set; }
    [DataMember]
    public string PaperSize { get; set; }
    [DataMember]
    public string Orientation { get; set; }
    [DataMember]
    public string CustomPaperWidth { get; set; }
    [DataMember]
    public string CustomPaperLength { get; set; }
    [DataMember]
    public string JobControl { get; set; }
    [DataMember]
    public string PunchHoleRemoval { get; set; }
}

我通过此地址使用 NetTcpBinding 在本地连接到WCF服务:

  

的net.tcp://本地主机:2202 / theService / tech6140

任何人都知道这是为什么以及如何解决它?

1 个答案:

答案 0 :(得分:0)

如果班级&#39; WCF服务可能无法将请求对象识别为scannerSettings。命名空间在服务器和客户端上是不同的。几个星期前我犯了这个错误。

在服务器上,我有请求对象

Acme.Service.RequestObject

当我生成服务代理时,它包含了请求对象。出于美观原因,我将客户端上的命名空间更改为

SomeCoolNamespace.Service.RequestObject

调用服务时,服务将收到一个请求对象,其名称空间与预期的名称空间不匹配,参数将为null。