TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
David G
1.5k
224
14.5k
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:
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 K
link
.
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;"
>
class
=
"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"
,
// content type sent to server
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 -->
Reply
Answers (
0
)
Request Entity Too large 413 error
how to avoid time out error in wcf service