svc file and restful web service - c#

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.

Related

WCF Service - Sending long data using localhost IIS

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.

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.

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.

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

WCF XmlSerialization Attribute(POST)

I currently have a WCF service to consume a remote REST service with the following:
[ServiceContract]
[XmlSerializerFormat]
public interface IMyApi
{
[OperationContract]
[WebGet(
ResponseFormat = WebMessage.Xml,
UriTemplate = "RemoteServicePage.jsp")]
MyNewClass Send();
}
The nice part about this is the XmlSerializerFormat attribute. Automatically deserializes a response into the return type of the method(ie, POX response => MyNewClass).
I've been unsuccessful, however, in finding any samples of the reverse. I'd like to post a POX request to a given service. I'm curious if there's a similar way to pass an object to a WCF service which in turn makes the post request to the target.
Any thoughts are greatly appreciated.
UPDATE
Just a clarification of the question:
Is it possible to post an object to a web service via WCF(which handles the serialization)?
UPDATE
I believe Steve touched on what I believe is the right direction below with using the WebInvoke method and attribute RequestFormat to achieve what I'm looking for. I guess I want to point out I'm not hosting a web service that allows for posting, but rather trying to post to an external web service(ie, a remote *.jsp) using WCF.
WCF allows for easy consumption and access of external web services and this is something I'm familiar with. I've never attempted to post a stream or object to an external source however(posting via UriTemplate is straightforward).
I don't understand. Why not just use HttpWebRequest? One of the benefits of doing REST over HTTP is that you get to use standard HTTP libraries. Doing a POST with HttpWebRequest is relatively trivial. Why do you need WCF? If you want to serialize an object into your POST body, then you can do that with either the DataContractSerializer or with the XmlSerializer.
If you really don't like that option, then look at the new Microsoft.Http.HttpClient class that is in the WCF Rest Starter Kit Preview 2. It is a very nice client library and despite its name and packaging, it does not even have a dependency on WCF!
This answer is based on your second update. I only say this because your initial statement of "I currently have a WCF service to consume a remote REST service" makes no sense. You cannot declaratively define a remote interface that you want to consume. The term WebInvoke is not what you are thinking, it is simply a catch all attribute for handing non GET requests. WebGet and WebInvoke both handing incoming requests.
If you want to consume a remote "REST" interface then you need to use HttpWebRequest or the new HttpClient class.
I don't think you understand the meaning of XmlSerializerFormat. It means that the XML Serializer should be used instead of the Data Contract Serializer. Both will serialize to XML.
Use the RequestFormat attribute. For JSON, this would be
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
Good luck

Categories