Can someone give an example of a WCF service in C#, that can be called both from code behind, client script (same domain & different domain)
I will give example of a simple service that will have 2 operation contracts.
My service contract
using System.ServiceModel;
using System.ServiceModel.Web;
namespace Sum_WcfService
{
[ServiceContract(Namespace = "JsonpAjaxService")]
public interface IService1
{
// Default method for WebInvoke is "POST", cross domain requests are made on GET method
// I will Invoke this operation on post request
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
int AddNums(int Num1, int Num2);
// I will Invoke this operation on get request, from different domain
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
string ReturnNum(int Num);
}
}
Service Implementation
using System.ServiceModel.Activation;
namespace Sum_WcfService
{
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public int AddNums(int Num1, int Num2)
{
return Num1 + Num2;
}
public string ReturnNum(int Num)
{
return "Hey, You called ReturnNum with:" + Num;
}
}
}
To make the service callable from different domain, add Factory property as follows:
<%# ServiceHost Language="C#" Debug="true" Service="Sum_WcfService.Service1" CodeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>
Now Config of the project having service
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
<!--To make service callable from code behind & script as well-->
<client>
<endpoint address="http://localhost/Sum_Wcf/Service1.svc" binding="webHttpBinding"
behaviorConfiguration="EndPointBehavior" contract="SumServiceReference.IService1" name="WebHttpBinding_Well" />
</client>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
<!--Calling from different domain -->
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint name="" crossDomainScriptAccessEnabled="true">
</standardEndpoint>
</webScriptEndpoint>
</standardEndpoints>
<behaviors>
<endpointBehaviors>
<behavior name="EndPointBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Now add an empty webform to your project, to test your service
add following code in aspx page, include a jquery file.
<script type="text/javascript">
$(document).ready(
var Num1 = 10;
var Num2 = 20;
$.ajax({
type: "POST",
url: 'Service1.svc/AddNums',
contentType: "application/json; charset=utf-8",
data: '{"Num1": "' + Num1 +'"' +',' + '"Num2": "' + Num2 + '"}',
dataType: "json",
processData: false,
success: function (data) {
alert("success:" + data.d);
},
error: function (result) {
alert("error: " + result);
}
})
);
This is how you can call from same domain client script.
Now to call from code behind, you need to create proxy class of the service. Add service reference to your service.
I added service reference with name "SumServiceReference".
My code behind is as follows:
using System;
using System.Globalization;
using Sum_WcfService.SumServiceReference;
namespace Sum_WcfService
{
public partial class AddServiceClient : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var ServiceProxy = new Service1Client();
var Sum = ServiceProxy.AddNums(43, 37);
Page.ClientScript.RegisterStartupScript(GetType(), "ShowAlert", "alert(" + Sum + ")",true);
}
}
}
Now let's see how to call from cross domain script. Create a new project add a webform & to it's aspx page add following code:
<script type="text/javascript">
var Num = 7;
$(document).ready(
$.ajax({
type: "GET",
url: 'http://localhost:50345/Service1.svc/ReturnNum?Num=' + Num,
dataType: "jsonp",
processdata: false,
success: function(data) {
alert("success:" + data);
},
error: function(result) {
alert("Failed to call service");
}
})
);
</script>
Now web.config of your new project(for consuming wcf service) should be:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
Try this code out & report if face any issue.
Related
I am trying to get my first WCF restful service to work. But I keep getting a warning when i run it through VS2013. it says it cannot find any service metadata.
How do i set this up?
The App.config file is as follows,
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="WcfJsonRestService.Service1">
<endpoint address="http://localhost:8732/service1"
binding="webHttpBinding"
contract="WcfJsonRestService.IService1"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
The IService1.cs
namespace WcfJsonRestService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
Service1.Person GetData(string id);
}
}
The Service1.cs
namespace WcfJsonRestService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
public class Service1 : IService1
{
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "data/{id}")]
public Person GetData(string id)
{
// lookup person with the requested id
return new Person()
{
Id = Convert.ToInt32(id),
Name = "Leo Messi"
};
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
}
}
I'm going to implement basic authentication in WCF. I'm very new in all this stuff and my program is based on this series of articles http://leastprivilege.com/2008/01/11/http-basic-authentication-against-non-windows-accounts-in-iisasp-net-part-0-intro/ I do use webHttpBinding and HTTPS is on.
So the main idea is implementation of IHttpModule in this way:
When user requests some resource a module checks if Authorization header is present.
In case of Authorization is present, the module extracts the header's value, decodes and checks login and pass
In the other case the module sends a response with 401 code and a header "WWW-Authenticate".
Here is my implementation of the module:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Management;
using System.Text;
namespace MyProj_A
{
public class MyHTTPModule : IHttpModule
{
void IHttpModule.Dispose()
{
}
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += Context_BeginRequest;
context.AuthenticateRequest += OnEnter;
context.EndRequest += OnLeave;
}
private void Context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
context.Response.Write("BeginRequest");
}
void OnEnter(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
if (IsHeaderPresent())
{
if (!AuthenticateUser())
{
DenyAccess();
}
}
else
{
// if anonymous requests are not allowed - end the request
DenyAccess();
}
}
bool IsHeaderPresent()
{
return HttpContext.Current.Request.Headers["Authorization"] != null;
}
bool AuthenticateUser()
{
string username = "", password = "";
string authHeader = HttpContext.Current.Request.Headers["Authorization"];
if (authHeader != null && authHeader.StartsWith("Basic"))
{
// extract credentials from header
string[] credentials = ExtractCredentials(authHeader);
username = credentials[0];
password = credentials[1];
if (username.CompareTo("tikskit") == 0 && password.CompareTo("") == 0)
{
return true;
} else
{
return false;
}
}
else
{
return false;
}
}
private static void DenyAccess()
{
HttpContext context = HttpContext.Current;
context.Response.StatusCode = 401;
context.Response.End();
}
void OnLeave(object sender, EventArgs e)
{
// check if module is enabled
if (HttpContext.Current.Response.StatusCode == 401)
{
SendAuthenticationHeader();
}
}
private void SendAuthenticationHeader()
{
HttpContext context = HttpContext.Current;
context.Response.StatusCode = 401;
context.Response.AddHeader(
"WWW-Authenticate",
"Basic realm=\"yo-ho-ho\""
);
}
}
}
I publish it under IIS 7.5 on remote computer and connect to it with remote debugger from my Visual Studio. I set breakpoints at Context_BeginRequest, OnEnter and OnLeave.
Then I access to my WCF from a browser using URL and here is what happens:
After I inputted an URL and pressed the Enter Context_BeginRequest is fired
In VS I can see that the Authorization header isn't present
OnEnter is fired and eventually it assigns 401 code to the response
OnLeave is executed as well and it sets WWW-Authenticate to the response header
In the browser the standart login dialog is shown
I input the user name and password and press OK
Now Context_BeginRequest is fired again and I can see that Authorization header is present and consists a value like "Basic ", which is right
OnEnter isn't executed at all this time
OnLeave is fired but a value of HttpContext.Current.Response.StatusCode is 401 by some reason
Here is my Web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2"/>
<customErrors mode="Off" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp automaticFormatSelectionEnabled="false"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Default" >
<serviceMetadata httpGetEnabled="false" />
<serviceMetadata httpsGetEnabled="false"/>
<serviceAuthenticationManager authenticationSchemes="Basic"/>
<serviceCredentials>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="MyBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="MyProj_A.Service1">
<endpoint address="" binding="webHttpBinding" contract="MyProj_A.IService1"
behaviorConfiguration="webBehavior"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost/" />
</baseAddresses>
</host>
</service>
</services>
<diagnostics>
<endToEndTracing activityTracing="false" messageFlowTracing="true" propagateActivity="true"></endToEndTracing>
</diagnostics>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="MyHTTPModule"
type="MyProj_A.MyHTTPModule,MyProj-A"/>
</modules>
<directoryBrowse enabled="false"/>
</system.webServer>
</configuration>
So my questions are
1. Why OnEnter isn't fired second time, in 8, and how is 401 assigned in an item 9?
2. How to work around this behaviour, I mean do I need to move all the authentication processing from AuthenticateRequest (OnLeave) to BeginRequest (Context_BeginRequest) for example? Or maybe there is a better place for such processing?
Thanks!
Case is closed
I've forgotten to refer to binding configuration in endpoint configuration:
<endpoint address="" binding="webHttpBinding"
contract="MyProj_A.IService1"
behaviorConfiguration="webBehavior"
**bindingConfiguration="MyBinding"**/>
I have a WCF service created with VS2012 on Win8. If I start the service via VS (localhost:port) I'm able to do GET's and POST's. When I deploy to IIS on the same machine only GET works. The POST return 404 Not Found. I've tried deploying directly to IIS by creating an application off of my Default Web Site as well as using VS Publish.
POST URL: http://www.server.com/RestService/RestServiceImpl.svc/auth
POST Request Header: contents of PostData.xml below
Web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off"/>
</system.web>
<system.serviceModel>
<services>
<service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="RestServiceImplEndpointBehavior">
<!-- Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity automatically. -->
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="RestServiceImplEndpointBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
</system.webServer>
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "c:\log\Traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
iRestServerImpl.cs:
using System.ServiceModel;
using System.ServiceModel.Web;
namespace RestService
{
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/{id}")]
string XMLData(string id);
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}")]
string JSONData(string id);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "auth")]
ResponseData Auth(RequestData rData);
}
}
RestServiceImpl.svc
<%# ServiceHost Language="C#" Debug="true" Service="RestService.RestServiceImpl" CodeBehind="RestServiceImpl.svc.cs" %>
RestServiceImpl.svc.cs
namespace RestService
{
public class RestServiceImpl : IRestServiceImpl
{
#region IRestServiceImpl Members
public string XMLData(string id)
{
return "You requested product " + id;
}
public string JSONData(string id)
{
return "You requested product " + id;
}
public ResponseData Auth(RequestData rData)
{
// Call BLL here
var data = rData.details.Split('|');
var response = new ResponseData
{
Name = data[0],
Age = data[1],
Exp = data[2],
Technology = data[3]
};
return response;
}
#endregion
}
}
RequestData.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
namespace RestService
{
[DataContract(Namespace = "http://www.eysnap.com/mPlayer")]
public class RequestData
{
[DataMember]
public string details { get; set; }
}
}
ResponseData.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
namespace RestService
{
[DataContract]
public class ResponseData
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Age { get; set; }
[DataMember]
public string Exp { get; set; }
[DataMember]
public string Technology { get; set; }
}
}
PostData.xml
<RequestData xmlns="http://www.eysnap.com/mPlayer">
<details>Ashu|29|7 Years|.NET</details>
</RequestData>
I found the problem. IIS Request Filtering for the application had POST set to not allowed.
Your code works fine for POST and GET, though I have VS 2010 and IIS 7, but I see no problem in that. I published service as an application (POSTDataIssue) under a website.
I used fiddler to first test GET request (URL - http://localhost:82/POSTDataIssue/RestServiceImpl.svc/json/5) and it gave expected result.
Then I tried POST with following content to the URL http://localhost:82/POSTDataIssue/RestServiceImpl.svc/auth-
User-Agent: Fiddler
Host: localhost:82
Content-Type: application/xml
Content-Length: 110
<RequestData xmlns="http://www.eysnap.com/mPlayer">
<details>Ashu|29|7 Years|.NET</details>
</RequestData>
Response
HTTP/1.1 200 OK
Content-Length: 218
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Thu, 01 Aug 2013 02:34:58 GMT
<ResponseData xmlns="http://schemas.datacontract.org/2004/07/RestService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Age>29</Age><Exp>7 Years</Exp><Name>Ashu</Name><Technology>.NET</Technology></ResponseData>
For my recent project, I created a Web service that returns an array of custom type to jquery client-side code. WCF is called by $.ajax command and is in the same domain.
When I run my web applicaiton on localhost (which is IIS run on local machine), everything works fine. When I deploy it to our integration server, suddenly ajax call to WCF ends with an error: "parsererror - unterminated string constant" and status of 200. Returned message is however something like "[{\"Text\":\"Test dodatnih naslov", which of course is not a correct json format.
Correct response should have been: "[{"Text":"Test dodatnih naslovov","Value":"100"},{"Text":"Test dodatnih naslovov - ISO2","Value":"101"},{"Text":"UPDATE","Value":"102"}]"
I have traced WCf service for malfuncitons, but it does not seem to be crashing. I also tried and set timeout to ajax call, but to no avail. Some help would be much appreciated.
My IIS is IIS7, where integration runs IIS6 on Windows Server 2008.
js file
function InsuranceClientContact_ItemsRequesting(o, e) {
var $ = $telerik.$;
var urlSvc = ServiceBaseUrl + '/GetContacts'
$.ajax({
type: "POST",
url: urlSvc,
data: '{"ixClient": ' + selectedItemId + '}', //selectedItemId is a positive number
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
// do something
},
error: function (result) {
var msg = result.status + " - " + result.statusText;
setTimeout(function () { throw new Error(msg) }, 0);
}
});
wcf interface
namespace Sid.Skode.Web.Services.Populate {
[ServiceContract]
public interface IInsuranceClientContactService {
[OperationContract]
[WebInvoke(Method="POST",
BodyStyle=WebMessageBodyStyle.WrappedRequest,
ResponseFormat=WebMessageFormat.Json)]
Contact[] GetContacts(long ixClient);
}
[DataContract]
public class Contact {
[DataMember]
public string Text;
[DataMember]
public string Value;
}
}
wcf service implementation
namespace Sid.Skode.Web.Services.Populate {
[AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed )]
public class InsuranceClientContactService : IInsuranceClientContactService {
public Contact[] GetContacts( long ixClient ) {
return GetContactsFromDatabase( ixClient );
}
#region Private methods
private Contact[] GetContactsFromDatabase( long ixClient ) {
DataTable dt = GetDataFromDataBaseById( ixClient );
return ConvertDataTableToContactArray( dt );
}
private DataTable GetDataFromDataBaseById( long ixClient ) {
AutoCompleteBLL model = new AutoCompleteBLL();
return model.SearchContactsByPartner( ixClient );
}
private Contact[] ConvertDataTableToContactArray( DataTable dt ) {
Contact[] rgContact = new Contact[dt.Rows.Count];
int cnContact = 0;
foreach (DataRow dr in dt.Rows) {
if (!dr.IsNull( "NAZIV" )) {
Contact contact = new Contact();
contact.Text = dr["NAZIV"].ToString();
contact.Value = dr["ID_DODATEN_KONTAKT"].ToString();
rgContact[cnContact++] = contact;
}
}
return rgContact;
}
#endregion
}
}
web.config wcf part
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="httpServiceBehavior">
<serviceMetadata httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="httpEndpointBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithTransportWindowsSecurity">
<security mode="Transport">
<transport clientCredentialType="Windows" />
</security>
</binding>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" aspNetCompatibilityEnabled="true" />
<services>
<service name="Sid.Skode.Web.Services.Populate.InsuranceClientContactService" behaviorConfiguration="httpServiceBehavior">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithTransportWindowsSecurity"
contract="Sid.Skode.Web.Services.Populate.IInsuranceClientContactService"
behaviorConfiguration="httpEndpointBehavior">
</endpoint>
<endpoint
address="mex"
binding="mexHttpsBinding"
bindingConfiguration=""
contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
As described here, you need remove all instances of RadCompression http module from web config. Then, it works.
I'm getting problems with jquery call wcf. I've already search and found some solutions but it still raise me an error "405 method not allowed". Below is may code
-Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.ServiceModel;
namespace WcfServiceLibrary
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IContactService" in both code and config file together.
[ServiceContract]
public interface IContactService
{
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
Contact GetContactInfo();
}
}
My service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.ServiceModel;
namespace WcfServiceLibrary
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ContactService" in both code and config file together.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ContactService : IContactService
{
public Contact GetContactInfo()
{
ContactBL contactBL = new ContactBL();
return contactBL.GetContact();
}
}
}
My object
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime;
using System.Runtime.Serialization;
namespace WcfServiceLibrary
{
[DataContract]
public class Contact
{
[DataMember]
public int Id {get;set;}
[DataMember]
public string Fullname {get; set;}
[DataMember]
public string email { get; set; }
}
}
BL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WcfServiceLibrary
{
class ContactBL
{
public ContactBL() { }
public Contact GetContact()
{
return new Contact {email="thang.nguyen#saas.com.vn", Fullname="NVTThang",Id=2 };
}
}
}
And also my WCF configuration:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webBinding">
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="myBehavior" name="WcfServiceLibrary.ContactService">
<endpoint address="ajaxEp" behaviorConfiguration="epAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="webBinding" name="epWebHttp"
contract="WcfServiceLibrary.IContactService" />
<endpoint address="mex" binding="mexHttpBinding" name="epMex"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="epAjaxBehavior">
<webHttp />
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="myBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
<baseAddressPrefixFilters>
<add prefix="http://localhost"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
The I deploy my wcf on IIS 7.5 then I created a web client using jquery ajax call to my service.
$.ajax({
type: "GET",
url: "http://localhost/WcfTestService/Service.svc/ajaxEp/GetContactInfo",
data: '',
contentType: "application/json;charset=utf-8",
dataType: "json",
processdata: true,
success: function (msg) {
alert(msg);
//ServiceSucceeded(msg);
},
error: ServiceFailed
});
function ServiceFailed(err){
alert(err.responseText);
return;
}
when I call my service it always raises me "405 Method Not Allowed" and I've tried aspnet_regiis -i and ServiceModelReg -i but it didn't effect. Please suggest me any solutions.
Thanks in advance!
I can see a few problems in your service:
For "GET" requests, you should use WebGet, instead of WebInvoke, on your contract:
[ServiceContract]
public interface IContactService
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
Contact GetContactInfo();
}
In your endpoint behavior you have 2 behaviors for scripting: webHttp and enableWebScript. You only need the former. The latter is for integration with ASP.NET AJAX specifically.
<endpointBehaviors>
<behavior name="epAjaxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
This probably isn't causing any issues, but in the $.ajax call, you don't need to specify a content-type, since it's a GET request (so there's no content). Also, since you aren't passing any parameter, you also don't need the data parameter.
$.ajax({
type: "GET",
url: "http://localhost/WcfTestService/Service.svc/ajaxEp/GetContactInfo",
dataType: "json",
success: function (msg) {
alert(msg);
//ServiceSucceeded(msg);
},
error: ServiceFailed
});
Try this in your service:
[ScriptMethod]
public Contact GetContact(...)