I've been fighting with trying to get data from a WCF service with no luck, I would appreciate your help, this is my code and I'm always getting "0 undefined" in the error function in the ajax call:
Service1.svc.cs
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData()
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
string output = "It worked";
return serializer.Serialize(output);
}
}
IService1.cs
[OperationContract]
[System.ServiceModel.Web.WebInvoke(Method = "POST",
ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json)]
string GetData();
Web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="ServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service behaviorConfiguration="ServiceBehavior" name="MyService.Service1">
<endpoint address="" binding="webHttpBinding" contract="MyService.IService1" behaviorConfiguration="ServiceAspNetAjaxBehavior">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</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"/>
</system.webServer>
</configuration>
jQuery call
<script type="text/javascript">
$(document).ready(function () {
CallMyService();
});
function CallMyService() {
$.ajax({
type: "POST",
url: "http://localhost:54368/Service1.svc/GetData",
data: '',
dataType: "json",
contentType: "application/json",
success: ServiceSucceeded,
error: ServiceFailed
});
}
function ServiceFailed(result) {
alert(result.status + ' ' + result.statusText);
}
function ServiceSucceeded(result) {
var resultObject = result.GetDataResult;
alert(resultObject);
}
</script>
If I try to consume the WCF service from a C# project by adding a service reference, I'm getting this:
Could not find default endpoint element that references contract 'myReference.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
Could it be related to the error? The WCF service is hosted in IIS locally.
I just don't know what else to check.
UPDATE:
I just found the solution, it turns out that I need to a Global.asax file in the WCF service solution with the following code:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
Where localhost is where your WCF service is located, after that, it works! Thanks a lot to the both of you that told me about the debugger for Javascript, that gave me the clue!!!
UPDATE:
I just found the solution, it turns out that I need to a Global.asax file in the WCF service solution with the following code:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
Where localhost is where your WCF service is located, after that, it works! Thanks a lot to the both of you that told me about the debugger for Javascript, that gave me the clue!!!
Related
I created a basic WCF REST service with default methods.
It is working when i request for svc file, but it returns 404 error while placing a request with rest parameters.
i.e. it gives response when i call http://localhost/FirstWCFRestApp/RestServiceImpl.svc but returns 404 error when i called http://localhost/FirstWCFRestApp/RestServiceImpl.svc/xml/12.
It is very basic service with only 1 method and confusing me as why its not working.
I have pasted the code below.
Please let me know where it went wrong and why its not working.
Interface`
using System.ServiceModel;
using System.ServiceModel.Web;
namespace FirstWCFRestApp
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IRestServiceImpl" in both code and config file together.
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method="Get",UriTemplate="/xml/{id}",RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json)]
string DoWork(string id);
}
}
Class File`
namespace FirstWCFRestApp
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "RestServiceImpl" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select RestServiceImpl.svc or RestServiceImpl.svc.cs at the Solution Explorer and start debugging.
public class RestServiceImpl : IRestServiceImpl
{
public string DoWork(string id)
{
return "You requested Id is "+ id;
}
}
}
SVC file
<%# ServiceHost Language="C#" Debug="true" Service="FirstWCFRestApp.RestServiceImpl" CodeBehind="RestServiceImpl.svc.cs" %>
Web.Config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<behaviors>
<endpointBehaviors>
<behavior name="FWRBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="htBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="FirstWCFRestApp.RestServiceImpl" behaviorConfiguration="htBehaviour">
<endpoint address="Stud" binding="webHttpBinding"
contract="FirstWCFRestApp.IRestServiceImpl" behaviorConfiguration="FWRBehaviour"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
</system.serviceModel>
<system.webServer>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
The address http://localhost/FirstWCFRestApp/RestServiceImpl.svc is the service metadata address.
The practical service address should base on the UriTemplate property and the Address property of the service address.
UriTemplate="/xml/{id}"
binding="webHttpBinding"
contract="FirstWCFRestApp.IRestServiceImpl" >behaviorConfiguration="FWRBehaviour">
In addition, the Method property of the WebInvoke should be capitalized.
[WebInvoke(Method ="GET",ResponseFormat =WebMessageFormat.Json,UriTemplate ="/xml/{id}")]
In summary, the service address should be,
http://localhost/FirstWCFRestApp/RestServiceImpl.svc/Stud/xml/12
Feel free to let me know if there is anything I can help with.
Like Abraham said:
Web Invoke Format:{SVC Path}/{webHttpBinding Endpoint Address}/{WebInvoke UriTemplate}
In your case, it should be:
http://localhost/FirstWCFRestApp/RestServiceImpl.svc/Stud/xml/12
I am working on wcf first time so created just sinple wcf ,problem is when i run service from visual studio its working fine but when i deploy it on iis it giving HTTTP 500 Error
my web config file is as below
<?xml version="1.0"?>
<configuration>
<appSettings/>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime/>
<pages controlRenderingCompatibilityVersion="4.0"/>
</system.web>
<system.serviceModel>
<services>
<service name="WcfService2.Service1" behaviorConfiguration="servicebehavior">
<endpoint address="" binding="webHttpBinding" contract="WcfService2.IService1" behaviorConfiguration="web"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="servicebehavior">
<!-- To avoid disclosing metadata information, set the values below to false 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="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"/>
</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>
</configuration>
and svc file is as below
namespace WcfService2
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public string JSONData(string id)
{
return "you requested prduct" + id ;
}
public void DoWork()
{
}
}
}
and interface is as below
namespace WcfService2
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/json/{id}")]
string JSONData(string id);
void DoWork();
}
}
https://stackoverflow.com/a/11460220/2211593
my iis server was not having feature of HTTP activation enabled so after lot of googling i found this solution. very useful for me i spent almost half of day to find the solution of this small issue
I created a WCF service, that is supposed to be consumed by Javascript(Json) on client side. Websites are generated by ASP.NET MVC. I got everything running on localhost IIS-Express.
Problem is, when I call this WCF service from Javascript, it throws and undefined error.
One of many thoughts, why this error occurs is, that WCF web.config is not set correctly and doesn't allow cross site scripting.
Here is web.config file
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service behaviorConfiguration="Default"
name="Wcf_Categories.Categories">
<endpoint address=""
behaviorConfiguration="webBehavior"
binding="webHttpBinding"
contract="Wcf_Categories.ICategories" />
<endpoint address="sc"
behaviorConfiguration="script"
binding="webHttpBinding"
bindingConfiguration="crossDomain"
contract="Wcf_Categories.ICategories"/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding"
address="mex" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp helpEnabled="true" />
</behavior>
<behavior name="script">
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Default">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</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>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>
If I call service directly from browser for example localhost:1813/Categories.svc/sc/GetData?name=undefined
it returns suggested data and behaves normally (sites running at localhost/TestSites/Products# )
Here is script for wcf service call
function GetCategories(categoryName) {
$.ajax({
type: "GET",
async: "false",
url: "localhost:1813/Categories.svc/sc/GetData",
contentType: "application/json; charset=utf-8",
dataType: "json",
data : 'name='+categoryName,
processData: true,
success: function (result) {
ServiceSucceeded(result);
},
error: ServiceFailed
});
}
function ServiceSucceeded(result) {
var resultObject;
if (DataType == "json") {
resultObject = result.GetUserResult;
for (i = 0; i < resultObject.length; i++) {
alert(resultObject[i]);
}
}
}
function ServiceFailed(xhr) {
alert(xhr.responseText);<---------------------- Alert output is "undefined". Exception details = [Exception... "<no message>" nsresult: "0x805e0006 (<unknown>)" location: "JS frame :: http://localhost/TestSites/content/js/jquery-1.10.2.min.js :: .send :: line 6" data: no] ------->
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." });
}
return;
}
I'm trying to write a backend for my app in asp.net but I can't get my WCF service return a POST response after Ajax request from Ext. Instead I'm getting a 405 'Method not allowed' error.
This is my request :
Ext.Ajax.request({
type: 'POST',
url: 'http://localhost:35798/RestServiceImpl.svc/export',
params: {
html : {'array': document.body.innerHTML}
},
success : function(response){
console.log('RESPONSE !', response);
//me.onSuccess(response, callback, errback);
},
failure : function(response){
console.log('RESPONSE FAIL !', response);
}
});
This is my interface :
namespace RestService
{
public class RestServiceImpl : IRestServiceImpl
{
#region IRestServiceImpl Members
public string JSONData()
{
return "Your POST request";
}
#endregion
}
}
and the service code :
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Web.Script.Services;
namespace RestService
{
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[ScriptMethod]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "export")]
string JSONData();
}
}
And finally the config :
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</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="web">
<!--
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"/>
<!-- 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="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
This may be a cross-domain issue. E.g if your service is at http://localhost:35798 and the web page making the AJAX request is at http://localhost:80 , it's still considered a different domain by the browser (even though only the port number is different). Some browsers in this case issue a CORS request, and if your service doesn't handle it, will return a 405. Your options then are to either (1)avoid cross-domain requests, (2)implement CORS (will not help browsers that don't support it), or (3) use JSONP.
I am writing a WCF service, (json REST) and I have it working fine when using the wcftestclient.exe
When I run that test tool it triggers my break points while debugging and everything works as expected.
but, when using a browser to navigate to the the service and method, no break point is triggered. it seems as though the request isnt even getting to the code.
I receieve no errors on when navigating with web browser to the service, it just doesn't get any data, or trigger the break points.
Apologies if this is a duplicate, I have read and tried many many different configurations found in answers to similar questions, but nothing seems to work.
Many thanks for any help, I've posted my code below.
Martyn
I have setup:
ServiceContract
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
List<Country> GetAllCountries();
The Service CLass:
public List<Country> GetAllCountries()
{
ControlServiceRepository rep = new ControlServiceRepository();
return rep.GetAllCountries().ToList() ;
}
and my web config
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="OmniData" behaviorConfiguration="ServiceConfig">
<!-- Service Endpoints -->
<host>
<baseAddresses>
<add baseAddress="http://localhost:55641/"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" contract="ControlService.IOmniData" behaviorConfiguration="rest" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="rest">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceConfig">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
I think there are some things missing in your contract
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetAllCountries", RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
List<Country> GetAllCountries();
Try this.Let me know if it helps.
I got this working in the end by deleting all the end points in the config and using
RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(OmniData)));
if anyone else has issues, this is even easier than setting up end points because you can just specify the type of responses and end points within the classes themselves.
so:
Add a global.asax if one does exist and include this:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(OmniData)));
}
decorate your Service class with
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
here is mine:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class OmniData : IOmniData
{
public Country[] GetAllCountries()
{
ControlServiceRepository rep = new ControlServiceRepository();
return rep.GetAllCountries().ToArray() ;
}
}
then the interface you setup your endpoing and types using WebGet or WebInvoke
public interface IOmniData
{
[OperationContract]
[WebGet(UriTemplate = "OmniData/GetAllCountries", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
Country[] GetAllCountries();
}
the UriTemplate is the end point, so to access the method you would use: http://MyService.com/OmniData/GetAllCountries
and finally, web config
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false"/>
</webHttpEndpoint>
</standardEndpoints>
<services>
<service name="OmniData">
<!-- Service Endpoints -->
<host>
<baseAddresses>
<add baseAddress="http://localhost:55641"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" contract="ControlService.IOmniData" behaviorConfiguration="rest" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="rest">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Default">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Alot of help from here
but, importantly for what I wanted, json results, you need to make sure:
automaticFormatSelectionEnabled="false" is in there so it will use the response format specified in the interface. Otherwise you end up with XML instead.
hopefully this helps someone else
And thanks again for fiddler!
Martyn