I have a very simple WCF service called pilltrkr.svc. I am trying to call this service from jQuery through the following code:
var jsondata = JSON.stringify();
$.ajax({
type: "POST",
async: false,
url: './pilltrakr.svc/DoWork/',
contentType: "application/json; charset=utf-8",
data: jsondata,
dataType: "json",
success: function (msg) {
alert(msg);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
}
});
I do it locally (so using localhost). DoWork just returns a string. When I call this function, I get http: // localhost: 57400 / pilltrakr / pilltrakr.svc / DoWork / 404 Not Found
How do I call the WCF service? I tried several different options (after research). I was able to call this service using the code behind (client) method. I am sure it is very simple. Please inform.
More code -
It seems that every post in Stack includes an interface and an actual class for the service, so I also put them here, in case something is missing:
Interface:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Web;
namespace serviceContract
{
[ServiceContract]
public interface Ipilltrakr
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
string DoWork();
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
int addUser(string userName, string userPhone, string userEmail, string userPwd, string acctType);
}
}
class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using pillboxObjects;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
namespace serviceContract
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class pilltrakr : Ipilltrakr
{
string Ipilltrakr.DoWork()
{
return "got here";
}
int Ipilltrakr.addUser(string userName, string userPhone, string userEmail, string userPwd, string acctType)
{
userAccount ua = new userAccount();
int uId;
ua.userName = userName;
ua.userPhone = userPhone;
ua.userEmail = userEmail;
ua.userPwd = userPwd;
ua.userCreateDate = DateTime.Now;
ua.userAccountType = acctType;
uId = ua.add();
return uId;
}
}
}
web configuration:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="xxxConnectionString" connectionString="Data Source=xxx;Initial Catalog=xxx;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_Ipilltrakr" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:57400/pilltrakr/pilltrakr.svc/pilltrakr"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Ipilltrakr"
contract="svcPilltrakr.Ipilltrakr" name="BasicHttpBinding_Ipilltrakr" />
</client>
<services>
<service name="serviceContract.pilltrakr" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint contract="serviceContract.Ipilltrakr" binding="basicHttpBinding" address="pilltrakr" bindingNamespace="serviceContract"/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
</system.serviceModel>
</configuration>