Hey all I am using the following code on my local machine to give me a temporary WCF service:
- using System;
- using System.Windows.Forms;
- using System.ServiceModel;
- using TestService;
-
- namespace TestClient
- {
- public partial class Form1 : Form
- {
- IService1 patientSvc = null;
-
- public Form1()
- {
- InitializeComponent();
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- EndpointAddress address = new EndpointAddress(new Uri("net.tcp://localhost:2202/PatientService"));
- NetTcpBinding binding = new NetTcpBinding();
- ChannelFactory factory = new ChannelFactory(binding, address);
- patientSvc = factory.CreateChannel();
- }
- }
- }
And the class1:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.ServiceModel;
- using System.Runtime.Serialization;
-
- namespace TestService
- {
- [ServiceContract()]
- public interface IService1
- {
- [OperationContract]
- Patient GetPatient(Int32 index);
-
- [OperationContract]
- void SetPatient(Int32 index, Patient patient);
- }
-
- [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
- public class PatientService : IService1
- {
- Patient[] pat = null;
-
- public PatientService()
- {
- pat = new Patient[3];
-
- pat[0] = new Patient();
- pat[0].FirstName = "Bob";
- pat[0].LastName = "Chandler";
-
- pat[1] = new Patient();
- pat[1].FirstName = "Joe";
- pat[1].LastName = "Klink";
-
- pat[2] = new Patient();
- pat[2].FirstName = "Sally";
- pat[2].LastName = "Wilson";
- }
-
- public Patient GetPatient(Int32 index)
- {
- if (index <= pat.GetUpperBound(0) && index > -1)
- return pat[index];
- else
- return new Patient();
- }
-
- public void SetPatient(Int32 index, Patient patient)
- {
- if (index <= pat.GetUpperBound(0) && index > -1)
- pat[index] = patient;
- }
- }
-
- [DataContract]
- public class Patient
- {
- string firstName;
- string lastName;
-
- [DataMember]
- public string FirstName
- {
- get { return firstName; }
- set { firstName = value; }
- }
-
- [DataMember]
- public string LastName
- {
- get { return lastName; }
- set { lastName = value; }
- }
- }
- }
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:
- @{
- ViewBag.Title = ViewBag.Message;
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
-
-
class
="col-sm-offset-2 col-sm-8" style="margin-bottom: 10px; margin-top: 10px; text-align: center;"> - ="tips btn btn-success btn-outline" style="margin-right: 10px;" id="WCF" data-tooltip="@Html.Raw(tips["Continue"])">WCF TEST
-
And the JavaScript:
- $(document).ready(function () {
- $("#WCF").on("click", function () {
- $.ajax({
- type: "POST",
- url: ajaxDirPath + "WCF",
- data: '{"patNum": "1"}',
- contentType: "application/json",
- success: ServiceSucceeded,
- error: ServiceFailed
- });
- });
- });
-
- function ServiceFailed(result) {
- console.log('Service call failed: ' + result.status + ' ' + result.statusText);
- }
-
- function ServiceSucceeded(result) {
- resultObject = result.MyFunctionResult;
- console.log("Success: " + resultObject);
- }
And the code behind:
- [HttpPost]
- public string WCF(string patNum)
- {
- Dictionary<string, string> resultsBack = new Dictionary<string, string>();
- EndpointAddress address = new EndpointAddress(new Uri("net.tcp://localhost:2202/PatientService"));
- NetTcpBinding binding = new NetTcpBinding();
- ChannelFactory factory = new ChannelFactory(binding, address);
- IService1 patientSvc = factory.CreateChannel();
-
- Patient patient = patientSvc.GetPatient(Convert.ToInt32(patNum));
-
- if (patient != null)
- {
- resultsBack.Add("dback", "GOOD");
- resultsBack.Add("return1", patient.FirstName);
- resultsBack.Add("return2", patient.LastName);
- }
-
- return JsonConvert.SerializeObject(resultsBack, Formatting.Indented);
- }
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 -->