HTTP POST to a WCF service - wcf

What needs to happen to allow an HTTP POST to a WCF service?
I would like to allow people to not only use SOAP with this service, but they must also be able to HTTP POST to this service and ideally receive an XML response.
I cannot find an easy way to allow a WCF service to accept an HTTP POST.
I am past the HTTP 415 error and need some help with maybe a web.config change regarding endpoints, or an additional attribute above the method (WebInvoke).
Thank you!

In order to talk to a WCF service over the standard HTTP verbs, you need to use the WCF REST components.
In .NET 3.5 SP1, there's the WCF REST Starter Kit that you'll need (it's not part of the basic package).
When you have this, you can define an endpoint in your WCF service with a webHttpBinding and that basically should allow you to define GET, POST, PUT and DELETE operations.
Check out the WCF REST developer center for a great deal of white papers, tutorials, walk throughs and screencasts showing you exactly how to do all of this.
In a nutshell, you would adorn your service method(s) that you want to expose over HTTP REST with WebGet or WebInvoke attributes and a URL template - something like:
[ServiceContract]
public partial class YourService
{
[WebInvoke(Method = "POST", UriTemplate = "yourservice/{id}/save")]
[OperationContract]
SomeReturnType YourMethodCall(string someParam);
...
}
and then, in your web.config (for hosting in IIS) or in app.config you need an endpoint with the right binding:
<endpoint name="webEndpoint"
address="...."
binding="webHttpBinding"
contract="IYourServiceContract" />
You might also need a few extra things in your config - the WCF REST dev center should go into all the details in great depth.

Related

WCF wsdl generation missing WsTrust (RequestSecurityToken)

We have a problem in integrating a wcf service in a web service firewall.
Because the wsdl of the service does not contain the operations for ws-trust (requestsecuritytoken, ..).
How can I force WCF to include all details in its wsdl?
Or do I have to construct the wsdl myself?
Details:
Binding: WSFederationHttpBinding
MessageVersion: Soap12
Maybe it's because WCF produces multiple files by default and that is not always supported by things other than WCF clients.
Try this blog post about making WCF producing single wsdl. Maybe it will help.

Exposing meta data for a WCF 4.0 Rest Template Service

Probably missing something very basic. I created a WCF 4.0 Rest Service. It works no problems when I'm hitting the url from a browser and I'm getting back what I want.
But now I want to use that service from a client mvc application (it will also be used by other non .net platforms which is why it's a rest service in the first place).
Problem is how do I get a service reference to it so I can start to use it in my c# code? With the new minimal WCF .net 4 config approach and no interface for the service contract, I don't know how to specify a mex endpoint. Ultimately I don't want a mex endpoint in production, just during development. I would love to be able to specify that all my services (around 10 in one application) have endpoints with one tiny piece of config that vs2010 .config transformations just rips out when I publish.
Stop. REST service doesn't use metadata. Metadata (Mex endpoint) are only for SOAP services because WSDL 1.1 (the only version supported by WCF) is able to describe only SOAP service. WADL or WSDL 2.0 is able to describe REST service but non of them is currently supported by WCF.
REST service is consumed by using WebRequest directly or by building ChannelFactory on top of shared contracts. Both methods are described here. Other method is to use HttpClient from REST Starter kit (former API). The problem with Starter kit is that it has never reached RTM (it was replaced by WCF 4). Instead of metadata endpoint WCF 4 REST service offers help page where all operation are described. When using WCF 4 REST template the help page should be already turned on - just add /help sufix to address of your service. Here is another article about building REST clients.

WCF (svc) Service but client wants to connect as if it was ".asmx"

I have this scenario. Client requested us to have a WebService. I created a WCF Service. After we sent them our url to the web service description, client says
As it is we cannot consume a WCF
service, can you publish it a web
service?
Now i am wondering, they are asking me for a asmx... right?
Is there any way that i can "offer" my WCF service as an asmx service so i don't have to rewrite the whole thing?
my first "solution" is to have an .asmx file calling my .svc files directly... i don't know. I havent tried but i am heading on that direction.
Any ideas would be highly appreciated.
Tony
It is completely do-able. Just use an endpoint that exposes the service using basicHttpBinding or wsHttpBinding. The "file extension" of the URL doesn't make any difference to the client, only the content of the rerquest/response.
Here's a reference to another SO question:
REST / SOAP endpoints for a WCF service
It's very much possible.Follow the steps mentioned below and you'll be able to expose WCF service as ASMX endpoint.
Add new web service file (.asmx)
Now open the node of web .asmx file and delete .asmx.cs file
Once .cs file is deleted. You will find wcfasasmx.asmx file.
I have WCF class name as Service1(from the basic WCF service) and this class is present in current NameSpace. So I changed class name as mynamespace.Service1
Some changes is code as shown below-
In web.config in Tag add following code
<system.web>
<webServices>
<conformanceWarnings>
<remove name='BasicProfile1_1'/>
</conformanceWarnings>
</webServices>
</system.web>
Add following 2 attribute on interface(on servicecontract of WCF)
[WebService(Name = "Service1")]
[WebServiceBinding(Name = "Service1", ConformsTo = WsiProfiles.BasicProfile1_1, EmitConformanceClaims = true)]
Add [WebMethod] attribute on each operation contract.
[OperationContract]
[WebMethod]
string GetData(int value);
your service can now be consumed by asmx client too.

WCF BasicHttpBinding Http Post

Is it possible to post to a WCF service hosted in IIS? I know that that this is possible using REST but I really need the WSDL generation and SOAP headers. I need to transition an existing ASMX web services to WCF but I need to also allow my clients to continue to utilize the service via http posts (xml http request via javascript). Is REST my only option?
No, with a SOAP service, you need to use the SOAP methods and actions - you cannot use HTTP POST to "dump" some data on a SOAP service.
If you need HTTP POST, then REST is the way to go.
You can also use REST style WCF
http://msdn.microsoft.com/en-us/netframework/cc950529.aspx

Silverlight, WCF and rewriting response

I am trying to write a WCF service as a proxy for my Silverlight control to do cross-domain requests to an internet REST web service. I just want to make the requests to my service, and it would format and forward the request to the 3rd party web service (replace the hostname and part of the URL), then simply return the response to the caller.
Is my approach correct? The 3rd party server does not have the crossdomain.xml file to allow my call otherwise.
My question is, given my WCF service approach, I would like to rewrite the response body in my service with the response body I retrieved from the 3rd party service, and if possible, rewrite the response header to also be the one I got from that service. This way it minimizes the translation that my web service has to do on the response. Is it possible for me to do this rewrite? if so, how? if not, what are the best practices to implement this?
My interface is right now very primitive, something like this, simply because i don't need anything more than this. The response from the 3rd party service is a JSON response.
[ServiceContract]
interface IMyProxy
{
[OperationContract]
[WebGet(UriTemplate = "relay/{requestOptions}")]
string ForwardRequest(string requestOptions);
}
p.s. I do not need it to work with HTTPS, so no need to worry about the man-in-the-middle issue.
I figured it out, I believe (WebOperationContext).
I am still having an issue that if I duplicate the "Transfer-encoding" of the 3rd part service in my reply, I get an obscure exception down from the guts of the framework (something is "NotFound")... but that's a different story.