David G

David G

  • 1.4k
  • 224
  • 13.9k

Calling local WFC service from ASP.net AJAX

Apr 26 2018 9:36 AM

Hey all I am using the following code on my local machine to give me a temporary WCF service:

  1. using System;  
  2.  using System.Windows.Forms;  
  3.  using System.ServiceModel;  
  4.  using TestService;  
  5.   
  6.  namespace TestClient  
  7.  {  
  8.  public partial class Form1 : Form  
  9.  {  
  10.  IService1 patientSvc = null;  
  11.   
  12.  public Form1()  
  13.  {  
  14.  InitializeComponent();  
  15.  }  
  16.   
  17.  private void Form1_Load(object sender, EventArgs e)  
  18.  {  
  19.  EndpointAddress address = new EndpointAddress(new Uri("net.tcp://localhost:2202/PatientService"));  
  20.  NetTcpBinding binding = new NetTcpBinding();  
  21.  ChannelFactory factory = new ChannelFactory(binding, address);  
  22.  patientSvc = factory.CreateChannel();  
  23.  }  
  24.  }  
  25.  }  

 

And the class1:
  1. using System;  
  2.  using System.Collections.Generic;  
  3.  using System.Text;  
  4.  using System.ServiceModel;  
  5.  using System.Runtime.Serialization;  
  6.   
  7.  namespace TestService  
  8.  {  
  9.  [ServiceContract()]  
  10.  public interface IService1  
  11. {  
  12.  [OperationContract]  
  13.  Patient GetPatient(Int32 index);  
  14.   
  15.  [OperationContract]  
  16.  void SetPatient(Int32 index, Patient patient);  
  17.  }  
  18.   
  19. [ServiceBehavior(IncludeExceptionDetailInFaults = true)]  
  20.  public class PatientService : IService1  
  21.  {  
  22.  Patient[] pat = null;  
  23.   
  24.  public PatientService()  
  25.  {  
  26.  pat = new Patient[3];  
  27.   
  28.  pat[0] = new Patient();  
  29.  pat[0].FirstName = "Bob";  
  30.  pat[0].LastName = "Chandler";  
  31.   
  32. pat[1] = new Patient();  
  33.  pat[1].FirstName = "Joe";  
  34.  pat[1].LastName = "Klink";  
  35.   
  36.  pat[2] = new Patient();  
  37.  pat[2].FirstName = "Sally";  
  38.  pat[2].LastName = "Wilson";  
  39.  }  
  40.   
  41.  public Patient GetPatient(Int32 index)  
  42.  {  
  43.  if (index <= pat.GetUpperBound(0) && index > -1)  
  44.  return pat[index];  
  45.  else  
  46.  return new Patient();  
  47.  }  
  48.   
  49.  public void SetPatient(Int32 index, Patient patient)  
  50.  {  
  51.  if (index <= pat.GetUpperBound(0) && index > -1)  
  52.  pat[index] = patient;  
  53.  }  
  54.  }  
  55.   
  56.  [DataContract]  
  57.  public class Patient  
  58.  {  
  59.  string firstName;  
  60.  string lastName;  
  61.   
  62.  [DataMember]  
  63.  public string FirstName  
  64.  {  
  65.  get { return firstName; }  
  66.  set { firstName = value; }  
  67.  }  
  68.   
  69.  [DataMember]  
  70.  public string LastName  
  71.  {  
  72.  get { return lastName; }  
  73.  set { lastName = value; }  
  74.  }  
  75.  }  
  76. }  
This works just fine if I have the call to PatientService with a 1. It shows me the values Joe and Klink.
However, doing the same on my ASP.net website page with the following:
.cs page:
  1. @{  
  2.  ViewBag.Title = ViewBag.Message;  
  3.  Layout = "~/Views/Shared/_Layout.cshtml";  
  4.  }  
  5.   
  6.  class="col-sm-offset-2 col-sm-8" style="margin-bottom: 10px; margin-top: 10px; text-align: center;">  
  7.  class="tips btn btn-success btn-outline" style="margin-right: 10px;" id="WCF" data-tooltip="@Html.Raw(tips["Continue"])">WCF TEST  
  8.  
  
And the JavaScript:
  1. $(document).ready(function () {   
  2.  $("#WCF").on("click", function () {  
  3.  $.ajax({  
  4.  type: "POST",  
  5.  url: ajaxDirPath + "WCF",  
  6.  data: '{"patNum": "1"}',  
  7.  contentType: "application/json"// content type sent to server  
  8.  success: ServiceSucceeded,  
  9.  error: ServiceFailed  
  10.  });  
  11.  });  
  12.  });  
  13.   
  14. function ServiceFailed(result) {  
  15.  console.log('Service call failed: ' + result.status + ' ' + result.statusText);  
  16.  }  
  17.   
  18. function ServiceSucceeded(result) {  
  19.  resultObject = result.MyFunctionResult;  
  20.  console.log("Success: " + resultObject);  
  21.  }  
And the code behind:
  1. [HttpPost]  
  2.  public string WCF(string patNum)  
  3.  {  
  4.  Dictionary<stringstring> resultsBack = new Dictionary<stringstring>();  
  5.  EndpointAddress address = new EndpointAddress(new Uri("net.tcp://localhost:2202/PatientService"));  
  6.  NetTcpBinding binding = new NetTcpBinding();  
  7.  ChannelFactory factory = new ChannelFactory(binding, address);  
  8.  IService1 patientSvc = factory.CreateChannel();  
  9.   
  10. Patient patient = patientSvc.GetPatient(Convert.ToInt32(patNum));  
  11.   
  12. if (patient != null)  
  13.  {  
  14.  resultsBack.Add("dback""GOOD");  
  15.  resultsBack.Add("return1", patient.FirstName);  
  16.  resultsBack.Add("return2", patient.LastName);  
  17.  }  
  18.   
  19. return JsonConvert.SerializeObject(resultsBack, Formatting.Indented);  
  20.  }  
The patient variable is NULL for everything it returns back.
What would I be doing incorrectly?
 
Steps in debug:
 
This is the steps in debugging from both the javascript and the WCF service:


Next -->

Next -->

Next -->

Next -->

Next -->