Calling WCF REST Service using JQuery Ajax not working - wcf

I am learning WCF service. I am trying to call an RESTful service from Jquery. My Service is as below
Service Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
namespace RESTfulServiceLib
{
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
public class RestFullService : IRestFullService
{
public string Welcome(string Name)
{
return "Welcome to Restful Service " + Name;
}
}
}
Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
namespace RESTfulServiceLib
{
[ServiceContract]
public interface IRestFullService
{
[OperationContract]
[WebInvoke(UriTemplate="/Welcome/{Name}",Method="GET",ResponseFormat=WebMessageFormat.Json)]
string Welcome(string Name);
}
}
I have created a service host and the svc file goes like this
<%# ServiceHost Language="C#" Debug="true" Service="RESTfulServiceLib.RestFullService" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
I have the following config settings
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="EndPBhvr">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"
faultExceptionEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="SvcBhvr">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="RESTfulServiceLib.RestFullService">
<endpoint address="" behaviorConfiguration="EndPBhvr"
binding="webHttpBinding" bindingConfiguration="" name="EP1"
contract="RESTfulServiceLib.IRestFullService" />
</service>
</services>
</system.serviceModel>
</configuration>
After running the application , when I am browsing the url "http://localhost:2319/RESTFullService.svc/welcome/Mahesh"
it is returning the value as
"Welcome to Restful Service Mahesh"
I have tried to call this service using Jquery. But I am getting
error 200 undefined
The script is as follows
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.6.4.min.js"></script>
<script >
function ajaxcall()
{
$.ajax({
url: "http://localhost:2319/RESTFullService.svc/welcome/mahesh",
type: "GET",
contentType: "application/json; charset=utf-8",
dataType:"jsonp",
data:{},
processdata : true,
success: function(response)
{
var data= response.d;
alert(data);
},
error: function(e)
{
alert('error '+e.status + ' ' + e.responseText);
}
});
}
$().ready(function(){
$('#btntest').click(ajaxcall);
})
</script>
</head>
<body>
<button id="btntest" >Click Me</button>
</body>
</html>
What is wrong in my coding? Please help me...
Thanks
Mahesh

In the Success of your jQuery you have used
// var data= response.d;
but your are not returning a valid json.
there is no "d" in your response.

It is due to the Cross Domain Issue. I did a couple of changes in config file and it worked fine
The config file after changes. Added new Binding Configuration with crossDomainScriptAccessEnabled="true" and added to the endpoint. And put the aspNetCompatibilityEnabled="false"
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<standardEndpoints />
<bindings>
<webHttpBinding>
<binding name="Bind1" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="EndPBhvr">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"
faultExceptionEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="SvcBhvr">
<serviceMetadata httpGetEnabled="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
<services>
<service behaviorConfiguration="SvcBhvr" name="RESTfulServiceLib.RestFullService">
<endpoint address="" behaviorConfiguration="EndPBhvr" binding="webHttpBinding"
bindingConfiguration="Bind1" name="EP1" contract="RESTfulServiceLib.IRestFullService" />
</service>
</services>
</system.serviceModel>
</configuration>
Modified script, i have added "?callback=?" at the end of the URL to get the output in JSON instead of JSONP. JSONP will work without giving the callback in the URL
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.6.4.min.js"></script>
<script type="text/javascript" language="javascript">
function ajaxcall()
{
$.ajax({
url: "http://localhost:2319/RESTFullService.svc/welcome/mahesh?calback=?",
type: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: {},
processdata: true,
success: function (response) {
var data = response;
alert(data);
},
error: function (e) {
alert('error ' + e.status + ' ' + e.responseText);
}
});
}
$().ready(function(){
$('#btntest').click(ajaxcall);
})
</script>
</head>
<body>
<button id="btntest" >Click Me</button>
</body>
</html>
Thanks to my colleague Mrs.Poorani in Calsoft Labs for helping me in solving this issue

function CallService(sucessData) {
$.ajax({
// Add code for Cross Domain
headers: getHeaders(),
type: varType, //GET or POST or PUT or DELETE verb
url: varUrl, // Location of the service
data: varData, //Data sent to server
contentType: varContentType, // content type sent to server
dataType: varDataType, //Expected data format from server
processdata: varProcessData, //True or False
crossDomain: true,
cache: varCache,
timeout: 200000,
success: sucessData,
error: function (xhr) {// When Service call fails
fancyAlert("Error: " + xhr.responseText);
//fancyAlert('Error occured in Service Call');
}
});
}

Related

adding swagger to WCF

I am trying to make a proper nice documentation for a Microsoft WCF.
I added an asp web app to display swagger and installed
Swashbuckle.AspNetCore.5.6.3
I've set the solution to run both the WSF service and the asp web app.
Here is my code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
namespace asp_core_webapp
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Version = "1.0", Description = "my api " });
c.DocInclusionPredicate((_, api) => !string.IsNullOrWhiteSpace(api.GroupName));
c.TagActionsBy(api => api.GroupName);
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
services.AddControllers();
services.AddControllers();
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
}
}
I do no see anything running on
/swagger/v1/swagger.json
/swagger/v1
/swagger
There does not seem to be any valid documentation out there.
Any ideas how to make swagger work?
If you know another better alternative to Swagger, please feel free to add comments.
thanks
update: this is the main part of my config file:
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="serviceBehavior" name="WcfService1.Service1">
<endpoint address="Service1" binding="basicHttpBinding"
bindingConfiguration="" name="serviceEndPoint" contract="WcfService1.IService1" />
<endpoint address="mex" binding="mexHttpBinding"
bindingConfiguration="" name="mex" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="MyEndPointBehavior">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="webHttpBinding" scheme="http" />
</protocolMapping>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
</system.webServer>
Asp.net Core does not fully support WCF, if Swashbuckle.AspNetCore.5.6.3 is a package in AspNetCore, it may not be supported in WCF. In fact, WCF also has its own help document, you only need to enable it in the configuration file:
<endpointBehaviors>
<behavior name="ESEndPointBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>

WCF Websocket service “could not find a base address that matches schema http” error

I have a problem to make my WCF websocket service working. Until now I cannot find how to establish a connection. Both client and server side are really simple. So I think I miss something obvious here.
I currently have one WCF service running properly in my solution. The web services are hosted under IIS, the connection is properly handled using https and using basic authentication.
Here is my web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms" />
</system.web>
<system.serviceModel>
<!--webHttpBinding allows exposing service methods in a RESTful manner-->
<services>
<service behaviorConfiguration="secureRESTBehavior" name="MyApp.Services.MyService">
<endpoint address="" behaviorConfiguration="RESTfulBehavior" binding="webHttpBinding" bindingConfiguration="webHttpTransportSecurity" contract="MyApp.Services.IMyService" />
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
</service>
</services>
<!--WCF Service Behavior Configurations-->
<behaviors>
<endpointBehaviors>
<behavior name="RESTfulBehavior">
<webHttp defaultBodyStyle="WrappedRequest" defaultOutgoingResponseFormat="Json" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="secureRESTBehavior">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceAuthorization principalPermissionMode="Custom" serviceAuthorizationManagerType="MyApp.Security.CustomAuthorizationManager, MyApp">
<authorizationPolicies>
<add policyType=" MyApp.Security.AuthorizationPolicy, MyApp" />
</authorizationPolicies>
</serviceAuthorization>
</behavior>
</serviceBehaviors>
</behaviors>
<!--WCF Service Binding Configurations-->
<bindings>
<webHttpBinding>
<binding name="webHttpTransportSecurity" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Streamed" sendTimeout="00:05:00">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="Transport" />
</binding>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="CORSModule" type="Security.CORSModule" />
</modules>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://myapp.com" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Authorization" />
<add name="Access-Control-Allow-Methods" value="GET, DELETE, POST, PUT, OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
Now I'm trying use WebSocketHost to host a WebSocket server as a WCF service.
Here is my factory:
public class TRWebSocketServiceFactory: ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
try
{
WebSocketHost host = new WebSocketHost(serviceType, baseAddresses);
host.AddWebSocketEndpoint();
return host;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw ex;
}
}
}
Here is the service:
public class EchoWSService : WebSocketService
{
public override void OnOpen()
{
this.Send("Welcome!");
}
public override void OnMessage(string message)
{
string msgBack = string.Format(
"You have sent {0} at {1}", message, DateTime.Now.ToLongTimeString());
this.Send(msgBack);
}
protected override void OnClose()
{
base.OnClose();
}
protected override void OnError()
{
base.OnError();
}
}
Here is my Global.asax file:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute(
"Echo", new TRWebSocketServiceFactory(), typeof(EchoWSService)));
}
}
Here is the client side who try to establish a connection:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>WebSocket Chat</title>
<script type="text/javascript" src="Scripts/jquery-2.0.2.js"></script>
<script type="text/javascript">
var ws;
$().ready(function () {
$("#btnConnect").click(function () {
$("#spanStatus").text("connecting");
ws = new WebSocket("wss://MyServer/Echo");
ws.onopen = function () {
$("#spanStatus").text("connected");
};
ws.onmessage = function (evt) {
$("#spanStatus").text(evt.data);
};
ws.onerror = function (evt) {
$("#spanStatus").text(evt.message);
};
ws.onclose = function () {
$("#spanStatus").text("disconnected");
};
});
$("#btnSend").click(function () {
if (ws.readyState == WebSocket.OPEN) {
ws.send($("#textInput").val());
}
else {
$("#spanStatus").text("Connection is closed");
}
});
$("#btnDisconnect").click(function () {
ws.close();
});
});
</script>
</head>
<body>
<input type="button" value="Connect" id="btnConnect" /><input type="button" value="Disconnect" id="btnDisconnect" /><br />
<input type="text" id="textInput" />
<input type="button" value="Send" id="btnSend" /><br />
<span id="spanStatus">(display)</span>
</body>
</html>
On the line:
host.AddWebSocketEndpoint();
I always got the error:
Could not find a base address that matches scheme http for the endpoint with binding CustomBinding. Registered base address schemes are [https].
I'm a bit confused about the following points:
how to fix this error?
should I exose the EchoWSService in my web.config file as other services?
how the basic authentication is managed with web sockets?
Thanks!
I was missing:
Binding binding = WebSocketHost.CreateWebSocketBinding(true);
before:
host.AddWebSocketEndpoint();
Now the endpoint is correct.

Couldn't access Wcf service hosted in a server through ajax call, but can access the same Wcf service hosted in local machine

Created a Wcf Restful service and hosted in local Server. Another user is trying to consume the WCF Rest service from HTML5 page through ajax call. However always it is throwing failure message
Configuration Settings:
1.The WCF Rest Service is hosted in http://jtl_109.com/mob/AppService.svc/GetUserAuthendication
2.Created a HTML5 Application and deployed it in another server. The URL is http://jtl_110.com/SampleApplication.html
WCF Hosting Configuration Settings:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<add key="ConnectionString" value="Data Source=ServerDatabase; Initial Catalog=NewDataBase; User ID=pras; Password=rita" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Windows" />
<roleManager enabled="true" />
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="tSeyvaWCFEndPointBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="tSeyvaServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="AppServices.AppService" behaviorConfiguration="tSeyvaServiceBehavior">
<endpoint address="" behaviorConfiguration="tSeyvaWCFEndPointBehavior"
bindingConfiguration="crossDomain" binding="webHttpBinding"
contract="AppServices.IAppService">
</endpoint>
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
<handlers accessPolicy="Read, Execute, Script" />
</system.webServer>
</configuration>
And the ajax call is
var data = { 'logindetails': { 'UserName': UN, 'Password': PW } };
var st = JSON.stringify(data);
debugger;
$.ajax({
type: "POST",
url: "http://jtl_109/mob/AppService.svc/GetUserAuthendication",
data: st,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
ServiceSucceeded(result);
//// Play with response returned in JSON format
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR + "-" + textStatus + "-" + errorThrown);
}
});
Can any one Please suggest how to consume the WCF Reset Service from another Site?
Check that the port on the server isn't being blocked by the firewall. An easy way to check is to simply type into your local machine's browser address bar the URL of the web service - http:/ /ServerName:8001/ServiceClass/ServiceMethod
If you get a 404 error or something like that, check the Firewall settings (inbound) to see that port is open. However, if you get a good response in your browser, then you know it's not the Firewall.
OK, so here's a very simple htm/JavaScript program I wrote as a test harness to ping a WCF web service.
<title>SOAP JavaScript Client Test</title>
<script type="text/javascript">
function Ping() {
//set up varable
var sContent;
sContent= "<SoapXML><Your Content></XML>";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', Demo.URL.value, true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4||xmlhttp.readyState == 0) {
//alert("Ready state: " + xmlhttp.readyState.toString());
if (xmlhttp.status == 200) {
//alert("good");
Demo.pingresponse.value = "Response: " +xmlhttp.responseText;
}
if (xmlhttp.status !=200){
//alert("bad");
Demo.pingresponse.value = "Error: " +xmlhttp.status.toString() +" response text: " +xmlhttp.responseText;
}
} else {
//alert("readystate bad");
}
}
//send request
xmlhttp.setRequestHeader("POST http:localhost:8085/ServiceName HTTP/1.1");
xmlhttp.setRequestHeader("VsDebuggerCausalityData","uI8ACQAA");
xmlhttp.setRequestHeader("SOAPAction","\"http://SoapRequestHeader\"");
xmlhttp.setRequestHeader("Host","localhost:8085");
xmlhttp.setRequestHeader("Expect","100-continue");
xmlhttp.setRequestHeader("Accept-Encoding","gzip, deflate");
xmlhttp.setRequestHeader("Connection","Keep-Alive");
xmlhttp.setRequestHeader("Content-Length","639");
xmlhttp.setRequestHeader("Content-type", "text/xml; charset=utf-8");
xmlhttp.send(sContent);
}
</script>
</head>
<body>
<form name="Demo" action="" method="post">
<div>
Web Service URL (i.e. http://ServerName:8085/ServiceName <br />
<input id="URL" type="text" size="140" value="http:/ /localhost:8085/ServiceName " />
<br />
<input type="button" value="Ping" onclick="Ping();" /><br />
<textarea id="pingresponse"cols="100" rows="10">
</textarea> <br />
</div>
</form>
</body>
<html>
Obviously, this won't work for your site, but with some tweaks for the URL, Port and expected content, this might be a good starting point.

WCF web service custom authorization

I have WCF webservice using windows authentication and custom ServiceAuthorizationManager. Everything works fine, but if overridden CheckAccessCore returns false, I get error 500, instead of 401 as I expected. Service does not implement any service level error handling. How can I send 401 instead of 500 header?
Service config:
<!-- App configuration-->
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off" />
</system.web>
<appSettings>
<!-- Allowed users divided by comma -->
<add key="allowedUsers" value="DOMAIN\User1, DOMAIN\User2" />
</appSettings>
<!--Webservice-->
<system.serviceModel>
<services>
<service name="WebService.ApiService">
<endpoint binding="basicHttpBinding" bindingConfiguration="AuthenticatedBinding" bindingNamespace="http://namespace.com/customapi" contract="WebService.IApiService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceAuthorization serviceAuthorizationManagerType="WebService.Model.Services.AuthorizationService, WebService" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="AuthenticatedBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
Custom authorization manager:
class AuthorizationService : ServiceAuthorizationManager
{
private List<string> allowedUsers = new List<string>();
public AuthorizationService() : base()
{
Configure();
}
protected override bool CheckAccessCore(OperationContext operationContext)
{
base.CheckAccessCore(operationContext);
return allowedUsers.Contains(operationContext.ServiceSecurityContext.WindowsIdentity.Name);
}
private void Configure()
{
var configRow = ConfigurationManager.AppSettings["allowedUsers"];
var parts = configRow.Split(',');
if (parts.Length > 0)
{
foreach (var part in parts)
allowedUsers.Add(part.Trim());
}
}
}
Result image:
I found on the web that error code 500 is the proper way how to send SOAP fault response. So everything is fine with my webservice (I am getting 'Access denied' fault with error code 500).
SOAP specification about it
Summary on Martin Karpiseks blog

Cross Domain jQuery AJAX request to WCF - 405 Method not allowed

I am building simple WCF web service for my mobile web app.
I am stack at the point of making the $.ajax request to WCF which comes back with 405 - Method not allow, even though when I do http://http://localhost:35798/RestServiceImpl.svc/json/23 I can see result of the ReturnJSONData() in the browser.
I went through 100's of different post, but none of the answer fixed my problem.
AJAX Request
$.ajax({
type: "GET",
url: "http://localhost:35798/RestServiceImpl.svc/json/34",
contentType: "application/json; charset=utf-8",
success: function(data) {
console.log(data);
},
});
IRestServiceImpl.cs
namespace RestService{
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(
Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}"
)]
string ReturnJSONData(string id);
}
}
RestServiceImpl.svs.cs
namespace RestService {
public class RestServiceImpl : IRestServiceImpl {
public string ReturnJSONData(string id) {
return "You requested product " + id;
}
}
}
WebConfig
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
<endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name=""
helpEnabled="true"
automaticFormatSelectionEnabled="true"
defaultOutgoingResponseFormat ="Json"
crossDomainScriptAccessEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Any suggestion much appreciated.