Making WCF web service look like a ASMX web service when asked? - c#

I'm currently using Xcelsius to connect to my WCF web service, however it doesn't work. Googling around I see that Xcelsius can't connect to WCF web services but can't seem to find out why.
Assuming the web service gives away what type it is (WCF or ASMX) is it possible to perhaps spoof this? Something similar changing your user agent in a browser.
Edit
Xcelsius is expecting the address to the services WSDL (http://localhost:3951/Service1.svc?wsdl). As for the format that my WCF service is emiting, it's
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
I've also tried using FlatWSDL, however that doesn't seem to change anything.

Simply use basicHttpBinding in your service, and will look like an ASMX service to any consumer.
That still might not be enough, if the consumer is expecting a particular format. You would then need to duplicate the expected format.

Related

Accept WCF request in WebAPI

I need to enable my webAPI REST service to accept a request in the format of:
www.someURL.com/OldService.svc
I am working on an existing application that used to use WCF. These methods do not have to return anything but a 200 response. We need the REST service to handle this call so we can retire the old WCF service, but systems will fail if we don't support this WCF request.
Has anybody done this before?
edit:
Is it possible to do this with just adding a new route?
You can add a WCF service (.svc) to a Web API project by simply adding a New Item and selecting Web, it will then show up in the list as WCF Service.
Maybe you could use the Route attribute(using System.Web.Http) for the old service? I've used this for route names like [Route("SomeRoute")] but I'm not 100% sure if the .svc extension will interfere with anything.
[Route("OldService.svc")]
[HttpPost]
public HttpResponseMessage NewData(Data SomeData)
{}

C# Web Service client that will fail if WSDL is not available or invalid

My client hosts a few web services and has ASP.NET Web pages that will demo the web service and acts as a quick check to verify that the web service is up by the client. The problem is that the WSDL might be missing or invalid, but the Web Service will still work.
What I'd like to add to the ASP.NET web service client is a way to verify that the WSDL is there and valid, but have no idea where to start. Any suggestions would be greatly appreciated! Code behind is C#
I'm not entiery sure what you mean with verify that the WSDL is valid. Only thing I can suggest is, use an HttpWebRequest on the specific URI and see what response you get then either throw an exception based on specific status codes like 404 for example or handle it in a different way.
You can fetch the status code value
var request = (HttpWebRequest) WebRequest.Create("http://example.com/service.wsdl");
using (var r = (HttpWebResponse) request.GetResponse())
{
var result = r.StatusCode.ToString() == 200.ToString() ? "Success" : "Service not found";
Debug.WriteLine(result);
}
Hope this helps, goodluck.
Edit: if you know what services you're going to be testing you can simply add them as service reference in your client project and try to do an RPC on the service methods to see if it's available. Pictures below show to add a service reference.
Your scenario doesn't completely make sense. You could make a request to the service WSDL endpoint, http://something/service.asmx?wsdl and you could confirm that what comes back is a WSDL file. You could even validate it against the WSDL and XSD schemas.
This wouldn't tell you whether it was a WSDL that represented the service. It could conceivably be a WSDL for some totally different service.
Maybe more importantly, have you had problems where the service was available but the WSDL was not? In that case, rather than create diagnostics, I recommend that you fix the problem.

Can I generate a service reference automatically for a REST WCF service?

The ONLY argument I can see for SOAP WCF over REST (json) wcf is the fact that once my service is created I can add a a reference in visual studio and I get a load of strongly typed classes ready for me and a client class that I can call all my webmethod through. It even sets up the web.config as far as I remember.
However when I expose a REST (json) service I still get a WSDL. So Im wondering is there still a way to build my references automatically?
Not using WCF tools. Unlike with SOAP (which has an established protocol for describing services - WSDL), REST doesn't. WADL is one such protocol, but it isn't too widespread and WCF does not support it. You still get a WSDL, because WCF will describe everything it can from the service. However, the WSDL won't have a <wsdl:port> element, which would describe the REST endpoint, which is why you get the WSDL, but cannot generate a reference to it.
The post at http://blogs.msdn.com/b/carlosfigueira/archive/2012/03/26/mixing-add-service-reference-and-wcf-web-http-a-k-a-rest-endpoint-does-not-work.aspx has a lot more info on this issue.
Very old question, newer answer.
today using openapi (swagger) I can achieve this by using swagger inspector doing samples i can document my rest services as well as create a spec yml/json file allowing for validations and acceptance criteria as well as automated clients for java,python,c#,ruby,javascript and others I'm sure
I would like top elaborate:
Although it is true you cannot get a WSDL add service reference with a JSON REST WCF service, what I do is create two met data hooks:
is the operations returning JSON
is a single XML op returning a class wrapper which includes all the service classes I allow, I call it Discover:
i.e.
public class Discover
{
public Manager Manager {get;}
public Employee Emp {get;}
....
}
[OperationContract]
public Discover DiscoverDTOs()
You can, indirectly. While the client generated by Visual Studio won't work, that client implements an interface, also generated, that you can use like this:
WebChannelFactory<IService> factory = new WebChannelFactory<IService>(new Uri(endpointAddress));
IService proxy = factory.CreateChannel();
int result = proxy.Operation(1, 2, 3);
WebChannelFactory has another overload which accepts a WebHttpBinding, you can configure based on the service configuration, or you can make this configuration manually in your app.config file.

Different proxy class when adding WCF service as web reference and service reference

There is a web service written with WCF that I'm adding as reference. Proxy class generator works flawlessly when I add it to as service reference but it generates a slightly different class when I add it as web reference using service.svc?WSDL. Here's are the differences:
//service reference
public partial class TestServicesClient : ... // correct class name
{
public int TestMethod(string serviceID, int dealID) // correct method signature
{ ... }
}
//web reference
public partial class TestServices: ... //different class name
{
public void TestMethod(string serviceID, int dealID, bool dealIDSpecified, out int TestMethodResult, out bool TestMethodResultSpecified) // different method signature
{ ... }
}
I tried using wsdl.exe for generating web reference class, didn't help.
What is wrong here?
Nothing is wrong here. Service Reference is the "new" way to use a WCF Service, it removes overhead such as the "Specified"-parameter, "Result"-parameter and "Result Specificed"-parameter.
You can still use other properties/methods to check if a parameter is specified or if there is a result. But before WCF, it changed the method signature.
You use a Service Reference and a Web Reference a bit different and that's just the way it is.
Here's some additional reading:
Difference between web reference and service reference?
The Difference Between “Add Web Reference” and “Add Service Reference”
And to quote from a reply to "What is the difference between WCF service and web service"
WCF "web services" are part of a much
broader spectrum of remote
communication enabled through WCF. You
will get a much higher degree of
flexibility and portability doing
things in WCF than through traditional
ASMX because WCF is designed, from the
ground up, to summarize all of the
different distributed programming
infrastructures offered by MS. An
endpoint in WCF can be communicated
with just as easily over SOAP/XML as
it can over TCP/binary and to change
this medium is simply a configuration
file mod. In theory this reduces the
amount of new code needed when porting
or changing business needs, targets,
etc.
ASMX is older than WCF, and anything
ASMX can do so can WCF (and more).
Basically you can see WCF as trying to
logically group together all the
different ways of getting two apps to
communicate in the world of MS; ASMX
was just one of these many ways and so
is now grouped under the WCF umbrella
of capabilities.
There are few difference between the service reference and web reference. WCF serializes using datacontract serializer. So add XmlSerializeFormat attribute to the serivcecontract and then add web reference. Your extra parameters will be removed. But then its like using wcf like web service only. One more retriction is that you need to use web bindings only like http.

how to submit datetime parameter to a WCF REST service

I have a WCF Data service operation :
[WebGet]
public bool isContractUpToDate(string contractId, string lastmodifiedDate);
but I don't know how to call this service from a .NET client application and how I can call this operation from Internet Explorer.
I'm looking for some examples.
We can access RESTful WCF browser services like this
http://localhost:8080/Service/isContractUpToDate/{contractId}/{lastmodifiedDate}
But I think we can't specify DateTime datatype, as per my understanding it should be string only.
I have found this series to be extremely helpful and rich with examples on how to implement WCF REST services (including query strings and filters as well as calling from client code).
I finally found an answer to my question.
to invoke the operation from the browser, I use :
http://localhost:8080/service/ctrService.svc/isContractUpToDate?contractId='1'&lastmodifieddate='2012/02/04 00:00:00'
and to do it from a .NET client, I use :
IEnumerable<bool> resp = service.Execute<bool>(new Uri("http://localhost:8080/pricingservice/PricingDataService.svc/isContractUpToDate?contractId='1'&" +"lastmodifieddate='"+DateTime.Now.ToString()+"'"));
Console.WriteLine("is contract uptodate ? " + resp.First());
This works for me:
?startDate=2014-04-11T14:45:00&endDate=2014-05-31T23:59:59
I'm using this string to send url parameters to a REST service hosted in ASP.NET application.
This is how you can call it according to MSDN
http://services.odata.org/Northwind/Northwind.svc/Customers('ALFKI')/Orders?$filter=ShippedDate gt datetime'1997-09-22T00:00:00'
datetime'1997-09-22T00:00:00

Categories