My service model looks as follows.
<system.serviceModel>
<services>
<service name="Web.General" behaviorConfiguration="common">
<endpoint address="basic"
binding="basicHttpBinding"
contract="Web.IGeneral" />
<endpoint
...
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="common">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment
aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
When I hit .../general.svc/ping/1 I get error 400 Bad Request, which I can't explain. What is that and how can I kill it?
My service has interface as follows.
[ServiceContract]
public interface IGeneral
{
//[OperationContract(Name = "Ping")]
[WebInvoke(
Method = "GET",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
//RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "Ping/{input}")]
String Ping(String input);
}
Mate, I'v e been in to this situation and the best approach to solve this out is through Fiddler. 404 can be due to anything at server level. How about running a fiddler and see the RAW request - response
http://www.telerik.com/download/fiddler
Otherwise, enable WCF logging.
http://www.codeproject.com/Articles/383162/Logging-and-Tracing-WCF-Soap-Messages
And then use trace viewer:
http://msdn.microsoft.com/en-us/library/ms732023(v=vs.110).aspx
If you want us to give a concrete answer then you probably need to provide us more details.
Related
I created a wcf service with two different endpoints using basicHttpBinding and webHttpBinding. I have a asp mvc client consuming the basicHttpBinding endpoint and angular controller consuming the webHttpBinding. However when I call the endpoint using the webHttpBinding via browser and angular controller I am getting connection refused error. How can I fix this problem or what am I doing wrong (New to WCF).
Service Contract:
[ServiceContract]
public interface IVacationService
{
[WebInvoke(UriTemplate = "create",ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
VacationDC RequestVacation(VacationDC vacation);
[WebGet(UriTemplate = "approved",ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
IEnumerable<VacationDC> GetApprovedVacations();
[WebGet(UriTemplate = "all", ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
IEnumerable<VacationDC> GetAllVacations();
[WebInvoke(UriTemplate = "remove/{id}", Method = "DELETE",RequestFormat = WebMessageFormat.Json)]
[OperationContract]
void RemoveVacation(long id);
}
Web.config:
<services>
<service name="WcfService.VacationService">
<endpoint address="basic" binding="basicHttpBinding" name="Soap" contract="WcfService.IVacationService" />
<endpoint address="api" binding="webHttpBinding" name="rest" contract="WcfService.IVacationService" behaviorConfiguration="webRest"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8092/vacation" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webRest">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
I created WCF service. when i test it with WCF Test Client , the service works fine.
I decided to configure the end point to display the result using the browser.
I have a blank page with the text Endpoint not found. with no more details.
Here is my web.config
<system.serviceModel>
<services>
<service name="mCollectorService.CollectorService" behaviorConfiguration="mCollectorService.CollectorServiceBehavior">
<endpoint address="../CollectorService.svc"
binding="webHttpBinding"
contract="mCollectorService.ICollectorService"
behaviorConfiguration="webBehaviour"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mCollectorService.CollectorServiceBehavior">
<!-- 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="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
Here is my ICollectorService
[ServiceContract]
public interface ICollectorService
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Authenticate/{agentcode}/{pin}/{deviceIMEI}/{gpslat}/{gpslong}")]
Authentification Authenticate(string agentcode,string pin, string deviceIMEI, string gpslat, string gpslong);
}
Any Help.
Try modifying your Service like this:
<services>
<service name="mCollectorService.CollectorService" behaviorConfiguration="mCollectorService.CollectorServiceBehavior">
<endpoint address="../CollectorService.svc"
binding="webHttpBinding"
contract="mCollectorService.ICollectorService"
behaviorConfiguration="webBehaviour"
/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:51855/CollectorService.svc" />
</baseAddresses>
</host>
</service>
</services>
And Change your OperationContract
[ServiceContract]
public interface ICollectorService
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Authenticate/{agentcode}/{pin}/{deviceIMEI}/{gpslat}/{gpslong}")]
Authentification Authenticate(string agentcode,string pin, string deviceIMEI, string gpslat, string gpslong);
}
And Call your URL in this way!
http://localhost:51855/CollectorService.svc/CollectorService.svc/Authenticate/YourAgentCode/YourPin/YourDeviceIMEI/YourGPSLat/YourGPSLong
Hope this will help. Thank You!
EDIT: I will recommend you to wrap your request in JSON instead of sending it in the Request URL if you are implementing Authentication mechanism.
I get a error 400, Bad Request.
I have a set of pages that i was trying to write. (Listing only relevant)
./Webpage.aspx
./webservices/WebCalls.svc
./webservices/IWebCalls.cs
./Web.config
and i was looking all over to allow my frontend to communicate with the Webcalls page like follows:
$.ajax({
url: "webservices/WebCalls.svc/DoWork",
success: function(){
console.log(arguments);
}
});
but it doesnt seem to work.
I was thinking to create the files as such:
WebCalls.svc:
public class WebCalls: IWebCalls
{
public string DoWork()
{
return "{\"hello\":\"Chop Wood, Carry Water.\"}";
}
}
and IWebCalls.cs:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public interface IWebCalls
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "DoWork")]
string DoWork();
}
and in my webConfig, i thought i set it up correctly:
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" httpGetUrl=""/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="ServiceBehavior" name="MyProj.WebCalls">
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
bindingConfiguration="LargeWeb" name="LargeWeb" contract="MyProj.webservices.IWebCalls" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="LargeWeb"
maxBufferPoolSize="15000000"
maxReceivedMessageSize="15000000"
maxBufferSize="15000000">
<readerQuotas
maxArrayLength="15000000"
maxBytesPerRead="15000000"
maxDepth="32"
maxNameTableCharCount="15000000"
maxStringContentLength="15000000"
/>
</binding>
</webHttpBinding>
</bindings>
When i run it, it just tells me that it has created a service, but when i try to call: DoWork it gets a 400 error.
Am I doing something wrong with my approach? Im just trying to build up a set of pages such that i can query for data. Originally, i was going to create a GenericHander.asxh for EACH function i wanted to create, but i thought that was too much space. I knew something like this was an option, but it seems that it doesnt work.
You may have decorated the wrong interface ITestService. Instead check the declaration of IWebCalls.
EDIT: Hmm.. I see you updated question ...
I think your services name should probably be name="MyProj.webservices.WebCalls" - but assuming that too is a typo, the only issue I see is that mex endpoint should not be there. mex endpoints are not meant for REST based services.
I am writing a WCF Rest service to return a JSON message. I've been trying to use an example I found on the internet as a guide. Any time I fire up the test client, none of my methods are displayed. Navigating to the Uri while the service is running yields me a "page cannot be displayed" page. Not exactly too sure where to go from here. Any help would be appreciated.
Web Config:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJasonP"
crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<services>
<service name="WcfRestLicense.LicenseService"
behaviorConfiguration="WebServiceBehavior">
<endpoint behaviorConfiguration="jsonBehavior"
binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJasonP"
contract="WcfRestLicense.ILicenseService" />
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
</service>
</services>
<!--<client />-->
<behaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WebServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add
scheme="http"
binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJasonP" />
</protocolMapping>
<serviceHostingEnvironment
aspNetCompatibilityEnabled="false"
multipleSiteBindingsEnabled="true" />
Service method:
public IQueryable<customer> GetCustomerById(string customerId)
{
int custId = Convert.ToInt32(customerId);
return _context.customers.Where(c => c.cust_id == custId);
}
Interface:
[ServiceContract]
public interface ILicenseService
{
[OperationContract]
[WebGet(UriTemplate = "customer/{customerId}/",
RequestFormat= WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
IQueryable<customer> GetCustomerById(string customerId);
}
If by test client you're talking about the WcfTestClient, then it won't work with RESTful services; it's designed to work with SOAP based web services. You can test RESTful services in a browser by passing in the appropriate URI, something like this:
http://<location of your service>/service/1
Where the number 1 would be a customer ID. This is a rough example as a) I don't do a lot with RESTful services and b) I'm not sure what your actual address is.
As far as getting a 404 when you go to the Uri, it sounds like you're looking for the help page. You can enable that in your config file:
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJasonP"
crossDomainScriptAccessEnabled="true"
enableHelp="true" />
</webHttpBinding>
</bindings>
I have been searching around for hours and trying different things to get this to work. I have tried so many articles on stackoverflow and either I am too stupid to get things working or I have some unique and odd configuration that is preventing me from experiencing joy.
I create the WCF service outlined by this tutorial:
http://www.codeproject.com/Articles/97204/Implementing-a-Basic-Hello-World-WCF-Service
It is super basic and has one method and all I want it to do is allow me to consume it with jQuery.AJAX() using json.
I have it hosted in IIS and it works. I can access the WSDL without issues.
I try to consume it with the following code:
$.ajax({
dataType: 'json',
type: 'POST',
contentType: "application/json",
url: "//localhost:546/HelloWorldService.svc/GetMessage",
data: {
name: "Joe"
}
}).done(function(msg){
console.log(msg);
$("#result").append(msg);
});
I always get errors. Based on what I have tried I get 500 errors, 402 errors, errors about incorrect content... All the errors.
I have tried implementing solutions from the following articles. They range from having me change the web.config endpoints (I know I HAVE to change them but nothing I have tried so far works in terms of adding a JSON endpoint) to adding things like
[WebInvoke(Method = "POST", UriTemplate = "json/PostSalesOrderData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
to the interface.
Here are some of the articles I have looked at and tried to smash into my solution to get it working without much success.
Javascript JSON and WCF webservice on Phonegap Android
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'
WCF Services with JSON, JSONP and SOAP End Points
Two endpoint (soap ,json) and one service method
WCF REST Service not accepting JSON in .Net 4
I also went through this tutorial and tried to use what he had to say to get my solution working. Still nothing!
http://www.primordialcode.com/blog/post/passing-json-serialized-objects-wcf-service-jquery
This is how my interface looks
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
[WebInvoke(UriTemplate = "GetMessage", Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
String GetMessage(String name);
}
Can anyone help me experience joy?
Thanks in advance for even looking at my question. If you need more information or I have not provided enough let me know so I can help you help me!
I must be missing something stupid... I know it is not this hard.
EDIT:
Working Web.Config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WebHTTPEndpointBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="MyWebServiceBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"/>
</webHttpBinding>
</bindings>
<services>
<service name="MyWCFServices.HelloWorldService"
behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="MyWebServiceBinding" behaviorConfiguration="WebHTTPEndpointBehavior"
contract="MyWCFServices.IHelloWorldService"/>
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding" address="mex"/>
</service>
</services>
</system.serviceModel>
</configuration>
change this line
data: {
name: "Joe"
}
to
data: JSON.stringify({name: 'Joe'});
EDIT:
Do this to your service. Add WebHttp binding in the config.
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
Hope you know where to add this. If not let me know and I will try to provide some inputs.
EDIT:
Following up on my comment,
<behavior name="myBehaviorName">
<webHttp />
</behavior>
<service name="MyWCFServices.HelloWorldService"
behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="webHttpBinding"
contract="MyWCFServices.IHelloWorldService" behaviorConfiguration="myBehaviorName"/>
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding" address="mex"/>
</service>
I'm coming in late, but you still have some issues you'll need to resolve to get it to work. You need to change the binding for your endpoint to support the HTTP operations.
<bindings>
<webHttpBinding>
<binding name="MyWebServiceBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"/>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="WebHTTPEndpointBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="MyWCFServices.HelloWorldService"
behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="MyWebServiceBinding" behaviorConfiguration="WebHTTPEndpointBehavior"
contract="MyWCFServices.IHelloWorldService"/>
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding" address="mex"/>
</service>
</services>
The maxBufferSize and maxReceivedMessageSize is optional.
EDIT: Oops, forgot to add your behaviorConfiguration in.