WCF Service - Sending long data using localhost IIS - c#

I have a WCF which is used by both web application(C#.NET) and mobile app as below.
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, UriTemplate =
#"createEmp?EMPId={EMPId}&empNONo={empNo}&subXML= {subXML}&Comments=
{Comments}", RequestFormat = WebMessageFormat.Json)]
Employee createEmp(int EMPId, string empNo, string subXML, string
Comments);
XML data which I am sending has long data.
And is working fine when I have published the service to azure but service is not being called when I publish the service to IIS.
PS: I know that service is not being called as I have log inside service implementation, so whenever service is consumed I will get a log.

Related

405 Method Not Allowed (WCF Web Service) after Second Request

I know 405 is a CORS error, but my website is on the same server as the WCF web service.
jQuery .ajax() POST Request throws 405 (Method Not Allowed) on RESTful WCF
My website/service appears to have a different problem. The first call to the Web Service succeeds. It is only when the user makes a change to the data and the web service is called for the second time that the 405 error is called. This happens on localhost and when I publish to another server.
Here is the service:
[OperationContract]
[WebInvoke(UriTemplate = "MyService", Method = "POST", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
ServiceResponse MyService(SimpleRequest request);
Thanks,
Matt
Make sure that you are sending right JSON. If it works first time, it should also work second time as well. Try changing BodyStyle:
BodyStyle = WebMessageBodyStyle.Bare
See this answer if it helps.

svc file and restful web service

I have a restful service living on our company server IIS 7.5. The url is http://11.52.36.251/MessagingServices/IData.svc. I used Restsharp/Restclient to test it from client side.
When I put the url in the browser and hit "ENTER", the wsdl link was generated.
But I heard that restful service didn't have a wsdl, but why I saw it?
I checked the code inside the service, there are
public interface IData
{
[OperationContract]
[WebGet(BodyStyle=WebMessageBodyStyle.Bare,
ResponseFormat=WebMessageFormat.Json,
UriTemplate="/api")]
void GetApi();
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/GetIData?iId={iId}&facilityId={internalId}")]
IDTO GetIData(string iId, string internalId);
}
Is it a fake restful service or other reason?
A restful service should be able to document itself strictly through its API, or simple readable HTML document. However, any service developed in WCF will have a WSDL by default, so just because a service has a WSDL doesn't mean its not RESTful. If it truly is RESTful, the service developer should disable the mex endpoint to keep its WSDL from being published, thus assuring clients are using the service in the intended way.

WCF Client does not follow contract UriTemplates

I have defined a function in the ServiceContract of my WCF service as followed:
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/test")]
CommandResponse Test();
And on the other end, at the client app I added the WCF service via "Add service reference" in visual studio. When calling the service test function using:
var test = m_ServiceClient.Test();
I am getting an error saying:
Operation 'GetUser' of contract... specifies multiple request body parameters to be serialized ...
The GetUser() is an other function I have in the service contract (also a GET function but with parameters in the UriTemplate).
My guess is that the client is calling the function with it's parameters as it should but the request is going to the wrong UriTemplate (or with no template at all and it just jumps to some kind of default).
Any special instructions I have to follow to let the client know about the functions UriTemplates ?
I've search all over and could not find a single page that helps with this issue...
The WebInvoke/WebGet attributes are used when you expose your service via WebHttpBinding which is for consuming your WCF service in REST style. In order to access the method via SOAP add [OperationContract] attribute and expose a endpoint via basicHttpBinding.
If you would want to access the service in a REST style then you should use the HttpWebRequest class to create your request rather than adding a Add Service Reference.
For achieving both i.e accessing the service via SOAP and REST just add [OperationContract] along with the WebInvoke attribute and expose another endpoint element with basicHttpBinding
For the client to recognize the UriTemplates and pass each method to it's own Url in the format defined by the template I copied the ServiceContract Interface to the client and then created a channel to the service base url
WebChannelFactory<IServiceContract> cf = new WebChannelFactory<IServiceContract>(new Uri("http://...."));
var service = cf.CreateChannel();
The resulting 'service' is a usable interface that works directly with the web service.

How to POST JSON HTTP Request to RESTful API made by WCF

I am trying to make Android consume a simple WCF webservice made by my self. Here is my WCF Service definition:
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/UploadCallLog2")]
[OperationContract]
String UploadCallLog2(String number, String cached_name, String duration, String date, String line_number);
I have two questions:
1. And what should my HTTP Request Look Like? What kind of Content type should I choose when add the request header?
2. Is there the correct way to put input param in body as JSON and the webservice can auto detect the value of input parameters?
Fred
I would suggest using an MVC project for this over a WCF service.
The controller can return a JsonResult, the MVC library contains Json helper methods to serialize and deserialize the json objects into c# objects if the properties match.
Also this then allows the MVC based service to be called from any client that has support for XmlHttpRequest using ContentType as "application/json". You can use either HttpGet or HttpPost depending on your request type and security of data under SSL as well. You can also take advantage of the authentication model to secure your methods.
This just makes for a really clean implementation that doesn't have any complex server and client configurations. More information regarding similar approaches is the new Web API in MVC 4.

WCF Rest WebService working good locally but return HTTP 400 externaly

I am building a WFC Rest web service.
This service containe POST operation who work correctly when I consume it locally. but when I tried to consume it from external IP but it return a HTTP 400.
This is the web service Interface :
[OperationContract(Name = "Login")]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "Login")]
string Login(Stream data);
Do you have any idea about this ?
Thank you.
Try enabling Tracing on your service and inspect the trace logs to see where your request is failing. To enable tracing follow this link

Categories