For some reason I'm not able to get a very simple AJAX post to my web service (svc) to work.
I'm sure it's a problem with my web.config, but I tried everything I can find online.
In "MyService.svc" if is use
[OperationContract]
[WebInvoke(Method ="POST", RequestFormat = WebMessageFormat.Json, ResponseFormat =WebMessageFormat.Json, BodyStyle =WebMessageBodyStyle.Wrapped)]
public string DoWork()
{
string result = "did it";
//result = test;
return (new JavaScriptSerializer().Serialize(result)); ;
}
where DoWork only returns a value, Ajax succeeds. However, if I pass a parameter to DoWork(string test)
Ajax returns an error. Bad Request.
My ajax is:
function DoTest() {
testData = { "test": "Hello" };
var jsonData = JSON.stringify(testData);
var POSTURL = "MyService.svc/DoWork";
//alert(jsonData);
//alert(POSTURL);
$.ajax({
type: "POST",
url: POSTURL,
datatype: "json",
data: jsonData,
ContentType: "application/json; charset=utf-8",
success: function (result) {
alert("success: " + result.d);
},
error: function (xhr, status, error) {
alert("Opps: " + xhr + " " + status + " " + error);
}
})
}
});
This is in my web.config:
<behaviors>
<endpointBehaviors>
<behavior name="MyServiceAspNetAjaxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="MyService" behaviorConfiguration="MyServiceBehavior">
<endpoint address="" behaviorConfiguration="MyServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="MyService" />
</service>
</services>
</system.serviceModel>
I need another set of eyes on this otherwise simple program. Thanks
"Bad Request" is usually an error in the request format. You can use the help document in WCF to view the request format. If you want to enable the help document, you need to change the WebMessageBodyStyle to bare:
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle =WebMessageBodyStyle.Bare)]
Then enable the help document in the configuration file:
<endpointBehaviors>
<behavior name="ESEndPointBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
View the requested format in the browser, you need to send the data in its format.
I removed an extra <endpoint address="" I had in my revision. The service call is being made from AJAX now. Phew. This WCF works great when it's working, and very difficult to diagnose when it does not work. Thanks for listening and good luck to everyone.
Related
I have created the Two Methods one is to Get and another one is to Create,
For Getting the Data is working fine, but when i trying to post the Data is showing the Error like Method not Found(404)
The Code is as following.
Contract Methods :
public interface IContactPositionService {
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "GET",RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetContactPositionList")]
List<ContactPosition> GetContactPositionList();
[OperationContract]
[WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.WrappedRequest,RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "AddContactPosition")]
int AddContactPosition(ContactPosition position);}
And Corresponding Serivice class is like :
public class ContactPositionService : IContactPositionService
{
public List<ContactPosition> GetContactPositionList()
{
DataSet ds = clsObj.GetContactPositionsList();
return ConverterHelper.Convert<ContactPosition>(ds.Tables[0]);
}
public int AddContactPosition(ContactPosition position)
{
// throw new System.NotImplementedException();
}
And in Web.config file i have configured the End Points As :
<services>
<service name="VirtusMobileService.ContactPositionService" behaviorConfiguration="VMS.Position.ServiceBehaviour">
<endpoint address="" behaviorConfiguration="VMS.Position.EndPointBehaviour"
binding="webHttpBinding" bindingConfiguration="" name="VMS.Position.EndPoint"
contract="VirtusMobileService.IContactPositionService" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="VMS.Position.EndPointBehaviour">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="VMS.ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="VMS.Position.ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
From the Client Asp.net page we call the Services for adding the Contact person object as like below :
private void AddData()
{
string sURL = "http://localhost:51293/ContactPositionService.svc/AddContactPosition";
ContactPosition order = new ContactPosition
{
PositionCode="10550",
PositionFinbaseId=11,
PositionTitle="Manager"
};
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ContactPosition));
MemoryStream mem = new MemoryStream();
ser.WriteObject(mem, order);
string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
webClient.UploadData(sURL, "PUT", mem.ToArray()); // Here it is showing Error
Console.WriteLine("Order placed successfully...");
}
While it comes to , " uploadData " it is showing error like "
remote server returned an error: (404) Not Found."
If i am trying to Get the Data using the "GetContactPositionList"
it showing the data Correctly , but when i am trying to work with
"PUT" method it is showing that error.
I tested the url in Fiddler , it is showing the same error in Response
Header "HTTP/1.1 405 Method Not Allowed"
<div id="content">
<p class="heading1">Service</p>
<p xmlns="">Method not allowed. Please see the <a rel="help-page" href="http://localhost:51293/ContactPositionService.svc/help">service help page</a> for constructing valid requests to the service.</p>
</div>
Please Suggest the answer.
Thanks.
In fiddler you have to specify application/json as Content Type and it will hit the service Try this link
Instead of "webClient.UploadData", try this..
webClient.UploadString(sURL, "PUT", data);
I think you could probably make this much easier on yourself by using System.ServiceModel.ChannelFactory. It will simplify greatly your interaction with the service, as you will have strongly-typed variables, the method names themselves, and not have to mess with the serialization yourself.
You can replace your AddData method as:
private void AddData()
{
// Create the service channel; 'factory' and/or 'svc' can be class members so we can re-use them.
var factory = new ChannelFactory<IContactPositionService>(new WebHttpBinding(), new EndpointAddress("http://localhost:51293/ContactPositionService.svc"));
factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
var svc = factory.CreateChannel();
ContactPosition order = new ContactPosition
{
PositionCode = "10550",
PositionFinbaseId = 11,
PositionTitle = "Manager"
};
try
{
var result = svc.AddContactPosition(order);
Console.WriteLine("Order# " + result.ToString() + " placed successfully...");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Note: This will require your calling project to have a reference to System.ServiceModel.dll and System.ServiceModel.Web.dll. Additionally, the calling project will need to know about IContactPositionService and ContactPosition. I your calling project is not the same as your WCF project, and does not already reference your WCF project, then you can move your service interfaces and objects into a third class library, which you could then share between the WCF host and any consumers.
ILeaveManagement class
[ServiceContract]
public interface ILeaveManagement
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "get")]
List<ServiceReference1.LeaveRequest> GetLeaveDetails();
}
LeaveManagement class
public class LeaveManagement : ILeaveManagement
{
public List<ServiceReference1.LeaveRequest> GetLeaveDetails()
{
try
{
var entities = new ServiceReference1.leaverequest_Entities(new Uri(serviceUrl));
var result = entities.LeaveRequestCollection;
return result.ToList();
}
catch
{
return new List<ServiceReference1.LeaveRequest>();
}
}
}
configuration
<service behaviorConfiguration="DRLExternalList.LeaveManagementBehavior" name="DRLExternalList.LeaveManagement">
<endpoint address="" binding="wsHttpBinding" contract="DRLExternalList.ILeaveManagement"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<behavior name="DRLExternalList.LeaveManagementBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
I have deployed the project in IIS 7.5. When i run the application , it is saying BadRequest.
I have verrified in fiddler. i saw 400 error.
Please help me on this.
Try using webHttpBinding in your endpoint instead of the wsHttpBinding, or add it as an additional one and change the address. I use a bindingNamespace in my project, but I don't think you need it.
<endpoint address="XMLService"
binding="webHttpBinding"
behaviorConfiguration="restXMLBehavior"
contract="DRLExternalList.ILeaveManagement">
</endpoint>
Add an Endpoint Behavior
<endpointBehaviors>
<!-- Behavior for the REST endpoint -->
<behavior name="restXMLBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
I also annotate the OperationContract slightly differently, but it shouldn't make all that much of a difference. I'll give it to you just in case...
[WebGet(UriTemplate = "/GetLeaveDetails", ResponseFormat = WebMessageFormat.Xml)]
To call the service, it would look like this using the XMLService endpoint name:
http://myWebHost.com/WebService/MyService.svc/XMLService/GetLeaveDetails
Hosting an wcf service into a website issue : System.ArgumentException: ServiceHost only supports class service types
the above link helped me to solve my issue.
<%# ServiceHost Language="C#" Debug="true" Service="restleave.ProductRESTService" %>
I'm new to using WCF services and i hope you can help me out on some problems invoking an external REST webservice. I have create two applications to try and test my service.
Console application to test my class lib
An umbraco application which hosts my services
Both localhost.
Here is the code of my service:
namespace HorecaWebservices.Webservices
{
[ServiceContract]
public interface IWS_Test
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
Agenda GetAgenda(String mobileAppKey);
}
}
namespace HorecaWebservices.Webservices
{
public class WS_Test : IWS_Test
{
public Agenda GetAgenda(String mobileAppKey)
{
return BL_Agenda.GetAgenda(mobileAppKey);
}
}
}
Web config of my service:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true"
automaticFormatSelectionEnabled="true"
defaultOutgoingResponseFormat="Json">
</standardEndpoint>
</webHttpEndpoint>
</standardEndpoints>
<services>
<service name="HorecaWebservices.Webservices.WS_Test"
behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="wsHttpBinding"
contract="HorecaWebservices.Webservices.IWS_Test"/>
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding" address="mex"/>
</service>
</services>
</system.serviceModel>
Here is the code from my console application which invokes the service method:
public static class BL_Agenda
{
public static Agenda GetAgenda()
{
Agenda agenda = new Agenda();
WebRequest request = WebRequest.Create("http://localhost:63462/Webservices/WS_Test.svc/GetAgenda");
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
string postData = "{\"mobileAppKey\":\"HEMDZ\"}"; //encode your data
using (Stream s = request.GetRequestStream())
{
using (StreamWriter sw = new StreamWriter(s))
sw.Write(postData);
s.Close();
}
try
{
using (Stream s = request.GetResponse().GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
var jsonData = sr.ReadToEnd();
agenda = JsonConvert.DeserializeObject<Agenda>(jsonData);
}
s.Close();
}
}
catch (WebException e)
{
agenda = null;
WebExceptionStatus status = e.Status;
Console.WriteLine(status.ToString());
}
return agenda;
}
Now whenever i run this code the request.GetResponse() throws a System.Net.WebException "(415) Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'"
After a couple of hours trying to find out what the problem is i still cant figure it out.. Can someone help me out on this?
At a first glance your endpoint uses the wrong binding (wsHttpBinding):
<endpoint address="" binding="wsHttpBinding" contract="HorecaWebservices.Webservices.IWS_Test"/>
Whereas it should be webHttpBinding:
<endpoint address="" binding="webHttpBinding" contract="HorecaWebservices.Webservices.IWS_Test"/>
For a complete webHttp-sample binding take a look at this article: http://weblogs.asp.net/kiyoshi/archive/2008/10/08/wcf-using-webhttpbinding-for-rest-services.aspx
When changing the binding other issues are likely to occur. One thing is, that you'll probably don't need the <standardEndpoints>-node any more. So I'd suggest staying close to the linked sample.
Additionally it's always a good idea to activate tracing. Having tracing activated you'll get more detailed information about your 500 error (and probably others too).
This example is not exactly the same, but maybe the Factory attribute suggested will give you some clues.
HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'
I've got it! I finally sorted out the bad request error. I had to replace the BodyStyle = WebMessageBodyStyle.Bare setting to BodyStyle = WebMessageBodyStyle.WrappedRequest.
Thnx for all the help guys!
I am creating a simple WCF Restful service. Currently when I browse to: localhost/AzamSharpService.svc it shows me the web services default page where I can examine WSDL.
I want to browse to localhost/AzamSharpService.svc/LatestArticles and get the json from the GetLatestArticles method. Currently, when the browse to the /LatestArticles url it says page not found.
The implementation is shown below:
[ServiceContract]
public interface IAzamSharpService
{
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat =WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json, UriTemplate = "/LatestArticles")]
List<ArticleContract> GetArticles();
}
public class AzamSharpService : IAzamSharpService
{
public List<ArticleContract> GetArticles()
{
var articles = new List<ArticleContract>()
{
new ArticleContract() {Title = "iOS"},
new ArticleContract() { Title="Android"},
new ArticleContract() { Title = "Windows 7"}
};
return articles;
}
}
The configuration is shown below:
<system.serviceModel>
<services>
<service name="AzamSharpNewLook.AzamSharpService">
<endpoint address="AzamSharpService.svc"
binding="webHttpBinding"
contract="AzamSharpNewLook.IAzamSharpService"
behaviorConfiguration="webby"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webby">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
</system.serviceModel>
A couple of things to try... set endpoint address to empty string...in the webHttp node try enabling help... and you should be able to navigate to localhost/AzamSharpService.svc/help and get more info. Lastly I would use fiddler and construct a get request to the appropriate address, then just check the response and you should have what you need. Hope this helps...
I have a asmx web-service that returns a list of countries for a continent. When using JQuery to call the web-service I use:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "InternationalLookup.asmx/LoadCountries",
data: '{ continentName: "' + $(this).val() + '" }',
dataType: "json",
success: function (response) {
//..code
},
error: function (response) {
//..code
}
});
This works fine with the asmx code but when using the WCF service I have to change it to:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "InternationalLookup.svc/LoadCountries",
**data: '{ \"continentName\": "' + $(this).val() + '" }',**
dataType: "json",
success: function (response) {
//..code
},
error: function (response) {
//..code
}
});
Note the difference in the data I have to pass in, it now requires additional quotation marks around the continent name. My WCF service and it's configuration:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="InternationalLookupBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="InternationalLookup">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="InternationalLookupBehavior"
name="USQ.Websites.RefreshLayout.Webservices.UsqInternationalLookup">
<endpoint address="" binding="wsHttpBinding" contract="IInternationalLookup">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
[ServiceContract]
public interface IInternationalLookup
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string LoadCountries(string continentName);
}
Despite having quite some troubles getting it to work I would like to know why the parameter for the WCF web-service has to be wrapped in additional quotation marks.
The JSON specification states that object member names must be enclosed by double quotes - see www.json.org - so this is what WCF enforces. I don't know why the JSON parser used by ASMX services chose to be more lax in enforcing the syntax.